Help Integrating ServiceNow with Salesforce

sih7933
Tera Contributor

hey all, 

i was attempting to integrate ServiceNow x Salesforce : Cases Created in Salesforce to be created as Incidents in ServiceNow, i tried numerous guides, forums, and YouTube Tutorials. they were of no help , i keep getting stuck at obtain OAuth on my ServiceNow instance. please help me to integrate it from Scratch

Thank You

Syed 

6 REPLIES 6

Amit Pandey
Kilo Sage

Hi @sih7933 

 

You need to setup REST message with endpoints, authentication profile etc. and trigger it with the help of Business Rules. Alternatively, you can pass the endpoints, authentication etc in business rule itself. Sharing an outline of business rule with you. I have used client_credentials as grant type.

 

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

    try {
        var client_id = 'Enter your client id';
        var client_secret = 'Enter your client secret';
        var grant_type = 'client_credentials';
        var token_endpoint = 'https://*****prodtest.sandbox.my.salesforce.com/services/oauth2/token';
        var token_grant_url = token_endpoint + "?grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;

        var token_request = new sn_ws.RESTMessageV2();
        token_request.setEndpoint(token_grant_url);
        token_request.setHttpMethod("post");

        var token_response = token_request.execute();
        var response_body = token_response.getBody();
        var response_obj = JSON.parse(response_body);
        var access_token = response_obj.access_token;

        gs.info('Token request response: ' + response_body);
        gs.info('Access token: ' + access_token);

        // Use the access token to make API calls
        var api_request = new sn_ws.RESTMessageV2();
        api_request.setEndpoint('https://*****prodtest.sandbox.lightning.force.com/services/data/v58.0/sobjects/case');
        api_request.setHttpMethod('post');
        api_request.setRequestHeader('Authorization', 'Bearer ' + access_token);
        api_request.setRequestHeader('Accept', 'application/json');
        api_request.setRequestHeader('Content-Type', 'application/json');

        var restObj = {
            "Contact__c": current.contact.name + '',
            "ServiceNow_SysID__c": current.sys_id + '',
            "Priority__c": current.priority.getDisplayValue() + '',
            "Subcategory__c": current.subcategory + '',
            "Number__c": current.number + '',
            "Assignment_Group__c": current.assignment_group.name + '',
            "Assigned_to__c": current.assigned_to.name + '',
            "Channel__c": current.contact_type.getDisplayValue() + '',
            "Short_Description__c": current.short_description.toString().replace(/\v|\r|\0/g, "") + '',
            "Description__c": current.description.toString().replace(/\v|\r|\0/g, "") + ''
        };
        var restStr = JSON.stringify(restObj);

        api_request.setRequestBody(restStr);

        gs.info('API Request Endpoint: ' + api_request.getEndpoint());
        gs.info('API Request Headers: ' + api_request.getRequestHeaders());

        var api_response = api_request.execute();
        var api_response_body = api_response.getBody();
		
		var httpStatus = api_request.getStatusCode();
		if((httpStatus != 200) && (httpStatus != 201)){
			gs.eventQueue("httpStatusFailed", current, current.number, api_response_body);
		}

        gs.info('API Response Object: ' + api_response);
        gs.info('API Response Body: ' + api_response_body);
        gs.info(current.number + "\nSalesforce Case Create : HTTP Status : " + api_response.getStatusCode() + "\nRequest Body : " + api_request.getRequestBody() + "\nResponse Body : " + api_response_body);
    } catch (error) {
        gs.error('An error occurred: ' + error);
    }


})(current, previous);

Please mark my answer helpful and correct.

Hi @Amit Pandey Can we not use basic authentication with Salesforce?

Hi @ServN 

 

Basic Authentication is not encouraged by Salesforce. You can create a connected app in salesforce and share the password and security token with ServiceNow. In ServiceNow, you will have enter password+security token in the password field for basic auth user.

 

Alternatively, you can opt for 2.0.

 

Please mark my answer helpful and correct.

 

Regards,

Amit

Thanks @Amit Pandey will follow your approach. Was able to generate the access and refresh token successfully.