How to Get Data in Salesforce from WordPress Without Authentication?


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.

  1. Embedded Salesforce page in WordPress
  2. Get data through Salesforce REST API

Embedded Salesforce Page in WordPress

In Salesforce, you need to:

  1. Enable the Force.com Sites feature.
  2. Make the object which need to be viewed in WordPress becomes public.
  3. Create a Visualforce Page and make it public.
  4. 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:

  1. install and activate iframe in WordPress
  2. 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

  1. Active your Sandbox and login.
  2. Create a Apex Class for @HTTPGet as method doGet() to query for what you want. Then return the result.
  3. Make the Apex Class and object public.
  4. Test your code in your browser with url like [Domain Name in Site]/[Name of Apex Class]/[Parameters]
  5. 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

  1. Create deployment connections and Upload to production.
  2. Download and deploy in production org.
  3. Make the Apex Class and object public.
  4. Test your code in your browser with url like [Secure Domain Name in Site]/[Name of Apex Class]/[Parameters]
  5. If it works, we may use it in WordPress.

Get Salesforce REST in WordPress

  1. Download and active preVU. Add extra code like $output = json_decode($output)->[field name of your object]; to interpret JSON.
  2. Insert the short code in your WordPress content.
  3. 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

  1. Salesforce: Managing Force.com Sites
  2. WordPress Plugin: frame
  3. WordPress Plugin: preVU
  4. Salesforce Community: how do I log into my sandbox instance?
  5. ForceGenie.com: Calling Salesforce REST webservices without authentication
  6. ForceGuru: Creating Public Web-Service in Salesforce
  7. Salesforce a fondo: Cómo crear Apex SOAP Web Services y Apex REST Web Services en Sites públicos en Force.com
  8. Salesforce: workbench
  9. Salesforce: Dynamic SOQL
  10. Daniel Ballinger’s FishOfPrey.com: APEX String.format(); Syntax – escaping single quotes
  11. Tutorials2Learn: XML Parser using Javascript
  12. Stack Overflow: XML parsing of a variable string in JavaScript
  13. Tsung’s Blog: PHP 使用 SimpleXML 來解析 XML 內容、屬性
  14. Stack Overflow: How to convert array to SimpleXML
  15. PHP: SimpleXML
  16. PHP: simplexml_load_string
  17. sitepoint: SimpleXML and namespaces
  18. Salesforce: Interact with the Force.com REST API from PHP
  19. Stack Overflow: What is JSON and why would I use it?
  20. PHP: json_decode
  21. 小惡魔 – 電腦技術 – 工作筆記 – AppleBOY: 你不可不知的 JSON 基本介紹
  22. PHP: str_replace
Advertisements

One thought on “How to Get Data in Salesforce from WordPress Without Authentication?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s