Outbound Rest Message (which uses OAuthToken) not working in the Run script Workflow

ashwryadj
Tera Expert

Outbound Rest Message (which uses OAuthToken) not working in the Run script Workflow


var r = new sn_ws.RESTMessageV2('<Rest Message>, '<method name>');
r.setStringParameterNoEscape('userId', '<userId>');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var json = new global.JSON();
var resp = JSON.parse(responseBody);
 
this code is working in the background script of the instance by the same code is not working in the workflow run script when request is submitted.
 
PLEASE HELP! 
 
1 ACCEPTED SOLUTION

ashwryadj
Tera Expert

It finally worked when the rest call was executed via Business Rule,

I created an asynchronous BR to implement it.

View solution in original post

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

@ashwryadj 

did you check the workflow run script is working with which user's session?

Seems the user who submitted the request is not able to get the OAuth token

check this link

Non-admin users cannot retrieve OAuth token from OAuth Credentials table to make outbound REST call 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

I tried creating ACLs still it's not working it's still giving error code 401 when run from workflow and on the other hand, it's working fine in the background script 

 

Both are currently run in my user's session and I am an Admin.

It's still unclear.

 

Thank you in advance for all your help !! 

Hi Aishwarya ,

 

Check if you have setup Authentication Profile correctly or not . It might be one of the possible causes.

 

and also Background scripts run in the context of an admin user, while workflow scripts may run under a different user or system context. This can affect permissions, token generation, and access to resources.

 

var r = new sn_ws.RESTMessageV2('<Rest Message>', '<method name>');
r.setAuthenticationProfile('oauth2', '<OAuth_Profile_Name>');
r.setStringParameterNoEscape('userId', '<userId>');
var response = r.execute();

Hi @rajeev17 ,

 

I tried doing this, this is not working 😞

Is there any other way ? 

 

Thank you in advance !