Salesforce ServiceNow Integration

Ram
4 min readMar 2, 2024

--

Integration of Salesforce with ServiceNow can streamline business processes by facilitating data flow and collaboration between the two platforms. Let's take an example, a company uses Salesforce as its CRM platform to manage customer data, including accounts and contacts. Additionally, they use ServiceNow as their IT Service Management (ITSM) platform to handle support tickets and incidents. Recognizing the need for enhanced efficiency and data consistency, the Organization decides to establish an outbound integration from Salesforce to ServiceNow to synchronize account information.

Here’s a high-level overview of how you can achieve outbound integration using Rest API to sync the Account Info from Salesforce to ServiceNow.

  1. Register an Application in the ServiceNow Application Registry (will update the Redirect URL in the following steps) and note down the generated Client ID and Client Secret

2. Log in to Salesforce using administrator credentials.

3. Navigate to Setup and select Auth. Providers (enter “Auth” in the quick find box)

4. Click New, select OpenID Connect provider type, and fill the form as follows (Replace {SNOW_INST} with your SNOW instance and enter Client Id and Secrets copied in Step 1)

5. Go to Service Now, and update the Redirect URL in the Application created in Step 1.

https://<INST>.my.salesforce.com/services/authcallback/{AuthProviderName}

6. Go to Setup and select Named Credentials (enter “Named ” in the quick find box)

7. Click New, and fill the page as follows (Replace {SNOW_INST} with your SNOW instance and select the Authentication Provider you created in Step 4)

7. On Save, you will be redirected to SNOW authentication and then the authorization page. Log in to ServiceNow and click the “Allow” button to authorize access to Salesforce.

8. On Successful authorization, you will be redirected back to the same “Named Credential” page and you can see the “Authentication Status” as Authenticated.

9. Create a Scripted REST API in ServiceNow with a POST HTTP method to handle bulk insert/update of Core Companies and note down the Resource Path.

(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var requestBody = request.body;
var requestData = requestBody.data;
var records = [];
for(var i=0; i<requestData.length; i++){
var gr = new GlideRecord('core_company');
if(requestData[i].sys_id != '') {
gr.addQuery('sys_id', requestData[i].sys_id);
gr.query();
gr.next();
} else {
gr.initialize();
}
gr.name = requestData[i].name;
gr.website = requestData[i].website;
gr.phone = requestData[i].phone;
gr.street = requestData[i].street;
gr.city = requestData[i].city;
gr.state = requestData[i].state;
gr.country = requestData[i].country;
gr.zip = requestData[i].zip;
var responseBody = {};
if(requestData[i].sys_id != '') {
var compSysID = gr.update();
responseBody.sys_id = compSysID;
} else {
var newSysID = gr.insert();
responseBody.sys_id = newSysID;
}
responseBody.name = requestData[i].name;
records.push(responseBody);
}
response.setStatus(201);
return records;
})(request, response);

10. In Salesforce, create one External Field ServiceNOW_ID__c on Account to store the ServiceNow ID to pass same to update the Company details.

11. Now Create an Apex class ServiceNowHelper(Replace Resource_Path with the Url copied in Step 9) to connect to the ServiceNow Rest API and update the Account record with ServiceNow Id.

public class ServiceNowHelper {

public static boolean isSyncRunnning = false;

public static void syncAccountsWithSNOW(List<Account> accounts) {
if(!isSyncRunnning) {
List<SNOWCompany> companies = new List<SNOWCompany>();
Map<String, Id> companyIdMap = new Map<String, Id>();
for(Account accnt : accounts) {
SNOWCompany company = new SNOWCompany();
company.name = accnt.Name;
company.website = accnt.website;
company.phone = accnt.phone;
company.street = accnt.BillingStreet;
company.city = accnt.BillingCity;
company.state = accnt.BillingState;
company.country = accnt.BillingCountry;
company.zip = accnt.BillingPostalCode;
company.sys_id = accnt.ServiceNOW_ID__c;
companies.add(company);
companyIdMap.put(accnt.Name, accnt.Id);
}
createAccounts(JSON.Serialize(companies), companyIdMap);
}
}

@future(callout=true)
public static void createAccounts(String payload, Map<String, Id> companyMap) {
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('callout:ServiceNowAPI/{Resource_Path}');
request.setMethod('POST');
request.setHeader('Content-Type', 'application/json');
request.setBody(payload);
HttpResponse response = http.send(request);
SNOWCompanyResponse result = (SNOWCompanyResponse) JSON.deserialize(response.getBody(), SNOWCompanyResponse.class);
List<Account> accounts = new List<Account>();
for(SNOWCompany resp : result.result) {
accounts.add(new Account(Id=companyMap.get(resp.name), ServiceNOW_ID__c=resp.sys_id));
}
isSyncRunnning = true;
Database.update(accounts);
}

public class SNOWCompany {
public String name;
public String website;
public String phone;
public String street;
public String city;
public String state;
public String country;
public String zip;
public String sys_id;
}

public class SNOWCompanyResponse {
public List<SNOWCompany> result;
}

}

12. Create an Account Trigger with After Insert and After Update events and call the above helper class.

trigger AccountTrigger on Account (after insert, after update) {
ServiceNowHelper.syncAccountsWithSNOW(Trigger.new);
}

13. Create an Account Record in Salesforce and Check if the record corresponding to the Account you created in Salesforce has been created or updated in ServiceNow.

Salesforce
Service Now

--

--