How can we connect to salesforce via REST API without losing connection?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2022 03:27 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-17-2022 05:36 AM
Have you verified token liefspan field while setting up oauth ? maybe its too little ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2022 05:35 AM
Thank you kindly for your answer.
Our token is still active as the image shows
, 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;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-24-2022 06:13 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2022 01:49 AM
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.