logo

NJP

Learn to Integrate ServiceNow with Salesforce using REST API and OAuth 2.0

New article articles in ServiceNow Community ยท Feb 19, 2025 ยท article

๐Ÿš€ Integrating ServiceNow with Salesforce using REST API and OAuth 2.0

๐ŸŒŸ Are you working with both ServiceNow and Salesforce? Integrating these two platforms can streamline your processes and boost efficiency. Here's a step-by-step guide to help you integrate ServiceNow with Salesforce using REST API and OAuth 2.0 authentication. ๐Ÿ’ผ

๐Ÿ” 1. Understanding the Integration Flow

Before diving into the technical steps, letโ€™s quickly take a look at the flow diagram. The process begins when an incident is created in ServiceNow, triggering a call to Salesforce using REST API and OAuth 2.0 authentication. Once authenticated, ServiceNow sends a POST request to Salesforce to create a case. Salesforce responds with a Case ID, which is then updated back into the ServiceNow incident record. This ensures both systems stay in sync, with accurate updates. ๐Ÿ”„

SelvaArun_2-1740003768484.png

SelvaArun_1-1740003731909.jpeg

๐Ÿ”ง 2. Creating a Connected App in Salesforce

Create a Connected App in Salesforce to allow authentication via OAuth 2.0:

  1. Navigate to Setup in Salesforce.
  2. In the search bar, type โ€œApp Managerโ€.
  3. Click on App Manager, then New Connected App.
  4. Name the app and enable OAuth settings.
  5. Under OAuth Scopes, select permissions like Full access or API access.

Salesforce will generate a Client ID and Client Secret, which we will use to authenticate ServiceNow. ๐Ÿ”‘

๐Ÿ”’ 3. Setting Up OAuth in ServiceNow

Set up OAuth 2.0 authentication in ServiceNow:

  1. Go to Application Registry in ServiceNow.
  2. Click New, then select Connect to an OAuth Provider.
  3. Choose Salesforce from the list of providers or create a custom one.
  4. Enter the Client ID and Client Secret from the Salesforce Connected App.
  5. Set the OAuth endpoint for Salesforce, typically https://login.salesforce.com/services/oauth2/token

SelvaArun_3-1740003810390.jpeg

๐ŸŒ 4. Creating an Outbound REST Message in ServiceNow

Create an Outbound REST Message in ServiceNow to send the POST request to Salesforce when an incident is created:

  1. Navigate to Outbound REST Messages in ServiceNow.
  2. Create a new REST Message and provide the Salesforce API endpoint for creating a case (e.g., https://your%5Finstance.salesforce.com/services/data/vXX.0/sobjects/Case/).
  3. Under HTTP Methods, select POST.
  4. Add necessary headers, including the Accept: application/json and the Content-Type as application/json.
  5. Add the data you want to send in the Body section, like the incidentโ€™s description, priority, and status.

SelvaArun_4-1740003874269.jpeg

SelvaArun_5-1740003913561.jpeg

๐Ÿงช 5. Testing the Outbound REST Message and get the OAuth Token

Test the Outbound REST Message to ensure everything works correctly:

  1. Go to the REST Message you created and get the OAuth Token by clicking on Get OAuth Token button by entering the Salesforce username and password and click Test.
  2. If everything is set up correctly, you should receive a success response from Salesforce, including the Case ID.

SelvaArun_6-1740004037523.jpegSelvaArun_7-1740004052725.jpeg

๐Ÿ›  ๏ธ 6. Automating with a Business Rule

Automate the process so that a case is automatically created when an incident is created in ServiceNow:

  1. Create a Business Rule in ServiceNow that triggers on Incident creation.
  2. Call the Outbound REST Message in the Business Rule to send the incident data to Salesforce and create the case.
  3. Capture the Case ID from the response and update the Incident record with this ID.

SelvaArun_9-1740004115013.png

SelvaArun_8-1740004084547.jpeg

(function executeRule(current, previous /*null when async*/) {

try {

var r = new sn_ws.RESTMessageV2('Salesforce Integration 2.0', 'Create Case');

r.setStringParameterNoEscape('description', current.description );

r.setStringParameterNoEscape('test_case', current.short_description);

r.setStringParameterNoEscape('status', 'New');

r.setStringParameterNoEscape('priority', current.priority);

// Execute the REST message and get the response

var response = r.execute();

var responseBody = response.getBody();

gs.log(responseBody);

var httpStatus = response.getStatusCode();

gs.log(httpStatus);

// Check the response and handle accordingly

if (httpStatus == 201) {

// Parse the response to get the created task ID

var responseObj = JSON.parse(responseBody);

var caseID= responseObj.id;

current.correlation_id = caseID;

gs.addInfoMessage('the case id:'+ caseID);

current.update(); // Store the created task ID in the incident record

// Optionally log or handle success

gs.info('Task created successfully in Salesforce. Task ID: ' + responseObj.id);

} else {

// Handle any non-successful responses

gs.error('Failed to create task in Salesforce Status Code: ' + httpStatus);

}

} catch(ex) {

// Handle any errors in the try block

gs.error('Error occurred while creating task in Azure DevOps: ' + ex.message);

}

})(current, previous);

โœ… 7. Testing the Integration

Test the integration:

  1. Create an incident in ServiceNow.
  2. The Business Rule will trigger, calling the REST Message to Salesforce.
  3. Salesforce will create a case and return the Case ID.
  4. The incident record in ServiceNow will be updated with the Salesforce Case ID in the Correlation ID field.

SelvaArun_10-1740004151528.png

๐ŸŽ‰ Conclusion

By following these steps, you've successfully integrated ServiceNow with Salesforce using REST API and OAuth 2.0. This integration helps ensure incidents in ServiceNow are automatically converted to cases in Salesforce, reducing manual work and improving efficiency. ๐Ÿ”—

For more details, check out the API documentation linked below. If you have any questions, feel free to reach out in the comments!

https://developer.salesforce.com/docs/atlas.en-us.api%5Frest.meta/api%5Frest/dome%5Fsobject%5Fcreate.htm

Also, please check out my YouTube channel for a detailed walkthrough of this integration process:

https://www.youtube.com/watch?v=zm2R2avtezQ

If you believe the solution provided has adequately addressed your query, could you please **mark it as 'Helpful'**. This will help other community members who might have the same question find the answer more easily.

Thank you for your consideration.

Selva Arun

View original source

https://www.servicenow.com/community/itsm-articles/learn-to-integrate-servicenow-with-salesforce-using-rest-api-and/ta-p/3182983