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. ๐
๐ง 2. Creating a Connected App in Salesforce
Create a Connected App in Salesforce to allow authentication via OAuth 2.0:
- Navigate to Setup in Salesforce.
- In the search bar, type โApp Managerโ.
- Click on App Manager, then New Connected App.
- Name the app and enable OAuth settings.
- 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:
- Go to Application Registry in ServiceNow.
- Click New, then select Connect to an OAuth Provider.
- Choose Salesforce from the list of providers or create a custom one.
- Enter the Client ID and Client Secret from the Salesforce Connected App.
- Set the OAuth endpoint for Salesforce, typically https://login.salesforce.com/services/oauth2/token
๐ 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:
- Navigate to Outbound REST Messages in ServiceNow.
- 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/).
- Under HTTP Methods, select POST.
- Add necessary headers, including the Accept: application/json and the Content-Type as application/json.
- Add the data you want to send in the Body section, like the incidentโs description, priority, and status.
๐งช 5. Testing the Outbound REST Message and get the OAuth Token
Test the Outbound REST Message to ensure everything works correctly:
- 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.
- If everything is set up correctly, you should receive a success response from Salesforce, including the Case ID.
๐ ๏ธ 6. Automating with a Business Rule
Automate the process so that a case is automatically created when an incident is created in ServiceNow:
- Create a Business Rule in ServiceNow that triggers on Incident creation.
- Call the Outbound REST Message in the Business Rule to send the incident data to Salesforce and create the case.
- Capture the Case ID from the response and update the Incident record with this ID.
(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:
- Create an incident in ServiceNow.
- The Business Rule will trigger, calling the REST Message to Salesforce.
- Salesforce will create a case and return the Case ID.
- The incident record in ServiceNow will be updated with the Salesforce Case ID in the Correlation ID field.
๐ 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!
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
https://www.servicenow.com/community/itsm-articles/learn-to-integrate-servicenow-with-salesforce-using-rest-api-and/ta-p/3182983
Selva Arun