Lets Understand first, What is REST API?

REST API allows you to interact with Salesforce Marketing Cloud from outside the Salesforce. That means that you can create / update / delete records in Salesforce Marketing Cloud from outside the salesforce.

API is used when you want to integrate salesforce with your applications, and your application can be in any language like PHP, Java, .Net etc.

For example, Suppose you have a program containing the contact form.And you want to store data to the salesforce platform whenever form is submitted. For that REST API is useful.

Lets Proceed Further with Practical Part :

Pre-requisites – In Salesforce.com

 

  • First you need a Salesforce account. you can create it from Here (https://developer.salesforce.com/signup).
  • You will need to create a application for authorization in salesforce. To create Application, click on Setup (right uper menu) > Create (left side – built menu) > Apps then create a new Connected App.
  • Set the callback URL to: https:///salesforce/oauth_callback (SSL is mandatory)
  • Select at least “Perform requests on your behalf at any time” for OAuth Scope. This will use for other scopes in your application.
  • Need Data Extension In Salesforce Account.

How to Create Data Extension in Salesforce.

Step: 1

Login into your salesforce Account and hover on email studio in navigation bar and click on Email.

Step: 2

Hover over Subscribers  and Click Data Extensions

Subscribers

Step: 3

Click the Create button. You have three options:

Create button

Click Standard Data Extension and click ok

Step: 4

  • Complete the information in the Properties section:
  • Creation Method – The method used to create the data extension. Select Create from New.
  • Name – The name of the data extension. Use this name to identify the data extension.
  • External Key – A unique value that identifies the data extension. Use this value to identify the data extension with an API call.
  • Description – A description of the data extension, optional.
  • Location – To store the data extension in a different folder location, optional, select Change Location
  • Click Next

Step: 5

Retention Settings is optional.

Step: 6

In fields section you can define all fields you want in your data extension.

Now Data Extension is ready to receive data when we push there.

Lets do the code for submiting data to the salesforce.

To create plugin we need to create folder and file in :  project->wp-content->plugins->folder_name->file_name.php

For example : Project->wp-content->plugins->marketing->marketing.php

Marketing.php

Code Snippet:

  <?php
 
    $field_data = array();
 
    $field_data['values']['fname'] = $_POST['fname'];
    $field_data['values']['lname'] = $_POST['lname'];
    $field_data['values']['email'] = $_POST['email'];
    
    $udata[] = $field_data;
 
    $j_field_data = json_encode($udata);
 
    $url = "https://auth.exacttargetapis.com/v1/requestToken";
    $fields = array(
                    'clientId'=>//Enter Client ID,
                    'clientSecret'=> //Enter clientSecret
                    );
    $fields_string = '';
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string,'&');
 
    $result = salesforce($url,$fields_string,0,1);
    $decode_result = json_decode($result);
 
    $token = $decode_result->accessToken;
     
    $expire = $decode_result->expiresIn;
 
    $data_url = //Enter Salesforce url for posting data
 
    $data_result = salesforce($data_url,$j_field_data,1,$token);
    curl_close($ch);
 
    function salesforce($url,$fields_string,$flg,$token){
        $ch = curl_init();
        if ($flg == 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $token ));
            curl_setopt($ch, CURLOPT_HEADER, 1);    
        }
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $result = curl_exec($ch);
         
        return $result;
    }
 
?>
 
Put this code in your php file and get all the values in variables using $_POST.
 
Make sure to map field names with the salesforce fields to submit data successfully.
 
Your Form is now ready with the salesforce integration.

Put this code in your php file and get all the values in variables using $_POST.

Make sure to map field names with the salesforce fields to submit data successfully.

Your Form is now ready with the salesforce integration.

This blog has been written by our Jr. Consultant Nayan Sangtani