It’s easy to create content in WordPress. But what if we need to get data from Salesforce? For example, if I want to show the total registered number of a campaign, what should I do?
So far as I know, there are two different solution.
- Embedded Salesforce page in WordPress
- Get data through Salesforce REST API
Embedded Salesforce Page in WordPress
In Salesforce, you need to:
- Enable the Force.com Sites feature.
- Make the object which need to be viewed in WordPress becomes public.
- Create a Visualforce Page and make it public.
- Test your page in your browser with url like [Domain Name in Site]/[Visualforce Page Name]/?id=[id]. There is no need to login with a Salesforce account.
In WordPress, embed the page in your content:
- install and activate iframe in WordPress
- Add the shortcode to embedded the Visualforce page in content.
Now, you may see the content as normal WordPress content.
Get data through Salesforce REST API
This is a bit complex. In short, we need to make it public in Sites, write an Apex Class for REST, interpret the returned result which is in JSON format, and then add the shortcode in WordPress content.
Write and Test in Sandbox Org
- Active your Sandbox and login.
- Create a Apex Class for @HTTPGet as method doGet() to query for what you want. Then return the result.
- Make the Apex Class and object public.
- Test your code in your browser with url like [Domain Name in Site]/[Name of Apex Class]/[Parameters]
- If it works, then upload the Apex class to production org.
Sample Apex code to get parameters from REST and search for the mobile of given contact ID:
@RestResource(urlMapping='/getContactMobile/*') global with Sharing class RestWebservice { @HttpGet global static String doGet(){ RestRequest req = RestContext.request; String contactId = String.format('SELECT fl_mobile__c FROM obj_contacts__c WHERE Id = \'\'{0}\'\'', new String[] {req.requestURI.substring(req.requestURI.lastIndexOf('/')+1)}); obj_contacts__c result = Database.query(contactId); return result.fl_mobile__c; } }
Upload from Sandbox to Production
- Create deployment connections and Upload to production.
- Download and deploy in production org.
- Make the Apex Class and object public.
- Test your code in your browser with url like [Secure Domain Name in Site]/[Name of Apex Class]/[Parameters]
- If it works, we may use it in WordPress.
Get Salesforce REST in WordPress
- Download and active preVU. Add extra code like $output = json_decode($output)->[field name of your object]; to interpret JSON.
- Insert the short code in your WordPress content.
- Test the above content and see if it works.
Above are quick guides to those who are familiar with Salesforce Administration, Visualforce Page, Apex, and PHP. A more detail step by step shall be available when I have time to finish.
If you are in a rush, leave me a message or take a look at the reference section.
Reference
- Salesforce: Managing Force.com Sites
- WordPress Plugin: frame
- WordPress Plugin: preVU
- Salesforce Community: how do I log into my sandbox instance?
- ForceGenie.com: Calling Salesforce REST webservices without authentication
- ForceGuru: Creating Public Web-Service in Salesforce
- Salesforce a fondo: Cómo crear Apex SOAP Web Services y Apex REST Web Services en Sites públicos en Force.com
- Salesforce: workbench
- Salesforce: Dynamic SOQL
- Daniel Ballinger’s FishOfPrey.com: APEX String.format(); Syntax – escaping single quotes
- Tutorials2Learn: XML Parser using Javascript
- Stack Overflow: XML parsing of a variable string in JavaScript
- Tsung’s Blog: PHP 使用 SimpleXML 來解析 XML 內容、屬性
- Stack Overflow: How to convert array to SimpleXML
- PHP: SimpleXML
- PHP: simplexml_load_string
- sitepoint: SimpleXML and namespaces
- Salesforce: Interact with the Force.com REST API from PHP
- Stack Overflow: What is JSON and why would I use it?
- PHP: json_decode
- 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY: 你不可不知的 JSON 基本介紹
- PHP: str_replace
Reblogged this on Sutoprise Avenue, A SutoCom Source.
LikeLiked by 1 person