Integrating Salesforce to Drupal made easy

Integrating Salesforce to Drupal made Easy

The Salesforce Suite of Drupal modules is an easy way to connect Drupal Application to Salesforce. This module supports integration with Salesforce by synchronizing Drupal entities like users, nodes, files with Salesforce objects. If you want to perform custom operations with salesforce then below is the custom module which provide integration between Salesforce Objects and Drupal Application.

Let’s see how can we do it

STEP1: Pre-requisites – In

  • First you need a Salesforce account. you can create it from Here (
  • 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.

STEP2: Pre-requisites – in your Drupal site

  • Your site needs to be SSL enabled to authorize the remote application using OAUTH.
  • Download salesforce partner libraries(attached partner libraries) and put it in sites/all/libraries/salesforce folder.
  • Create custom module, give a name whatever you want.
  • I’ve created “sf module” with the file names –, sf.module,
  • file contains module detection code.
    	name = sf
    	description = custom module for salesforce integration
    	core = 7.x
    	version = "7.x-1.3"
    	project = "salesforce"
  • is the important file which calls the salesforce libraries for login authentication access and fetching resulting data from salesforce object. i’ve also created salesforce username, password and security token from admin driven (this all are necessary to provide connectivity between salesforce app and your drupal application).
    	$USERNAME = variable_get('salesforce_user_name', FALSE);
    	$PASSWORD = variable_get('salesforce_password', FALSE);
    	$SECURITY_TOKEN = variable_get('security_token', FALSE); 
    	 * calls libraries for authentication.
    	function sf_salesforce_Login(){		
    		$salsesforce_libraries_url = libraries_get_path('salesforce');
    		require_once ($salsesforce_libraries_url.'/soapclient/SforcePartnerClient.php');	
    		$mySforceConnection = new SforcePartnerClient();
    		$mySforceConnection->login($USERNAME, $PASSWORD.$SECURITY_TOKEN);
    		return $mySforceConnection;
    	 * fetch data from salesforce.
    	function sf_fetch_result($query, $mySforceConnection){
    		$response = $mySforceConnection->query($query);	
    		foreach($response as $record){
    			$result[] = new SObject($record);
    			$result = Null;
    		return $result;
  • The sf.module contains admin configuration for salesforce authentication and other salesforce object related activities.
    	 * configuration form for salesforce username, passworda and security token.
    	function sf_admin() {
    		$form = array();
    		$form['salesforce'] = array(
    			'#type' => 'fieldset',
    			'#title' => t('Salesforce configurations'),
    			'#weight' => 1,
    			'#collapsible' => TRUE,
    			'#collapsed' => FALSE
    		$form['salesforce']['salesforce_user_name'] = array(
    			'#type' => 'textfield',
    			'#title' => t('User Name'),
    			'#default_value' => variable_get('salesforce_user_name'),
    			'#description' => t("Enter Salesforce User name."),
    		$form['salesforce']['salesforce_password'] = array(
    			'#type' => 'textfield',
    			'#title' => t('Password'),
    			'#default_value' => variable_get('salesforce_password'),
    			'#description' => t("Enter Salesforce Password."),
    		$form['salesforce']['security_token'] = array(
    			'#type' => 'textfield',
    			'#title' => t('SECURITY TOKEN'),
    			'#default_value' => variable_get('security_token'),
    			'#description' => t("Enter Salesforce Redirect Url."),
    		return system_settings_form($form);
    	 * configuration form for admin salesforce menu link.
    	function sf_menu() {
    		$items = array();
    		$items['admin/settings/sf'] = array(
    			'title' => 'SF Configurations',
    			'page callback' => 'drupal_get_form',
    			'page arguments' => array('sf_admin'),
    			'access arguments' => array('access administration pages'),
    			'type' => MENU_NORMAL_ITEM,
    		return $items;
    	 * fetching data of contact object from salesforce.
    	function sf_contact_data() {
    		// authenticate from salesforce
    		$mySforceConnection = sf_salesforce_Login();
    		// select query to get data
    		$query_fetch_contact_data = "SELECT Name,Email,Phone,Profile_Pic__c FROM Contact";
    		// fetch data which coming from salesforce
    		$contact_result_data = sf_fetch_result($query_fetch_contact_data, $mySforceConnection);
    			foreach($contact_result_data as $contact_response) {
    				$contact_record = $contact_response->fields;
    				$contact_name = $contact_record->Name;
    				$contact_email = $contact_record->Email;
    				$contact_phone = $contact_record->Phone;
    				$contact_profile_pic = $contact_record->Profile_Pic__c;