The CreatorCon Call for Content is officially open! Get started here.

How can we connect to salesforce via REST API without losing connection?

Javier Marquina
Kilo Explorer

We are trying to do an update on Salesforce through REST API + Business Rule, so we have created a REST message (Salesforce REST), with a HTTP Method (Default GET).
If we manually force getOauthToken (click on the link that says "get Oauth token"), and we execute the business rule (which has a part of Javascript code we've developed), it works perfectly. The problem lies on the following attempts to connect again after some minutes, that throws the error attached to this case.
We would like to connect instantly through Javascript so this business rule can be executed everytime we close a ServiceNow ticket.

7 REPLIES 7

Ravi9
ServiceNow Employee
ServiceNow Employee

Have you verified token liefspan field while setting up oauth ? maybe its too little ?

Thank you kindly for your answer.

 

Our token is still active as the image shows find_real_file.png
, but the connection doesn't work.
We must click on "getoAuthToken" manually if we want to reconnect again.
So it works on the first attempt, but the next ones stop working.

 

We are getting a message related to the access token, (User Not Authenticated. Could not retrieve a new access token with the refresh token. invalid_request, Missing parameters: access_token), but we do not know how to attach the access
token so Salesforce let us connect.

 

Our code:

try {
var r = new sn_ws.RESTMessageV2('Salesforce REST', 'Default GET');

r.setHttpMethod('PATCH');
r.setRequestHeader("Content-Type","Application/json");
r.setEndpoint('Salesforce link' + current.u_id_sf);
r.setRequestBody("{\"status\" : \"Closed\"}");

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}

Pls refer this https://docs.servicenow.com/bundle/sandiego-platform-administration/page/administer/security/concept/c_OAuthClientAPIs.html

Javier Marquina
Kilo Explorer

Hi Ravy,

We have tried this code but we get a new error:

[{"message":"INVALID_HEADER_TYPE","errorCode":"INVALID_AUTH_HEADER"}]

 

try { 
		
	var tokenRequest =new GlideOAuthClientRequest();
	tokenRequest.setGrantType("password");
	tokenRequest.setUserName("User");
	tokenRequest.setPassword("Pass");
	tokenRequest.setScope(null);

	var oAuthClient =new GlideOAuthClient();
	var tokenResponse = oAuthClient.requestToken("TestClient", tokenRequest);
	
	var r = new sn_ws.RESTMessageV2('Salesforce REST', 'Default GET');
	r.setRequestHeader("Authorization", "OAuth2 " + tokenResponse);
	r.setAuthenticationProfile("Salesforce UAT default_profile");
	r.setHttpMethod('PATCH');
	r.setRequestHeader("Content-Type","Application/json");
	r.setEndpoint('https:[SalesforceURL]/services/data/v53.0/sobjects/Task/' + current.u_id_sf);
	gs.debug('https://[SalesforceURL]/services/data/v53.0/sobjects/Task/' + current.u_id_sf);
	r.setRequestBody("{\"status\" : \"Closed\"}");

	var response = r.execute();
	var responseBody = response.getBody();
	var httpStatus = response.getStatusCode();
}
catch(ex) {
	var message = ex.message;
}

 

 

Could you provide any suggestions with this error?

Thank you Ravy.