Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Access RESTApi from inside ServiceNow Script

Ankit Bansal
Kilo Contributor

Hello,

I am trying to access CMDB using ServiceNow given RESTApi from Script include.
Password i can read of current user using below code

+++++++++++++++++++++++++++
var usr = new GlideRecord('sys_user');
var tmp = usr.get(gs.getUserID());
gs.info("Password outside: {0}", usr.user_password);
+++++++++++++++++++++++++++

But its 1 way Encrypted password so we can't decrypted . and when we use in REST APi call

+++++++++++++++++++++++++++++++
var request = new sn_ws.RESTMessageV2();
request.setEndpoint('https://<InstanceName>.service-now.com/api/now/table/<tableName>');
request.setHttpMethod('POST');
var user = gs.getUserName();
var password = usr.user_password;
request.setBasicAuth(user,password);
request.setRequestHeader("Accept","application/json");
request.setRequestHeader('Content-Type','application/json');
request.setRequestBody(<JSON>);
var response = request.execute();
+++++++++++++++++++++++++++++++

Is there any way to authenticate current user and get password?  

6 REPLIES 6

Chris M3
Tera Guru

Now, I'm not sure if this will work, because I can't think of any reason why you would want to make an API call from the server side when you can just do a GlideRecord query, but I did something similar from the Client Side using the Users Token.

 

So, on the Client side, I have this:

headers: {
   'X-UserToken' : window.g_ck
}

On the server side, something like this may work... (Skip your username/password lines all together)

var session = gs.getSession();
var token = session.getSessionToken();
sm.setRequestHeader('X-UserToken', token);

It's just a guess.  I would really question the requirement that the call from your script include needs to go through the REST API, instead of a simple GlideRecord.

I agree that we can also use directly GlideRecord to insert data in REST but since data to ServiceNow is in json so preferred existing ServiceNow REST API