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

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ankit,

What is your requirement here?

You are accessing table within ServiceNow via REST Table API call rather than querying that.

Regards

Ankur

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

yes, we are access REST API from Script Include inside ServiceNow not from outside. But need to pass user / password. how to pass that?

Hi Ankit,

Since password field on user table is one way encrypted; you cannot decrypt it and hence cannot use the value in API.

Why not go with system property and store username and password in it and fetch that in script include and use those.

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

But I was thinking if i am running script include on same ServiceNow instance and with same user. So i should be able to use that user and access RESTApi. because this is not from outside ServiceNow.