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.

Accessing basic auth credentials from scheduled job

samadam
Kilo Sage

I am trying to access basic auth credentials I created from a scheduled job to be able to access rest end point. I tried to do the following but it is returning null.

var provider = new sn_cc.StandardCredentialsProvider()
var credential = provider.getCredentialByID('41cc709cdb144c101bdb5a75dc961990');

var userName = credential.getAttribute("user_name");
var password = credential.getAttribute("password");

When I use the basic auth from rest message it works, so wondering if this is the right way or is there anything else I need to set up.

5 REPLIES 5

Amit Verma
Kilo Patron
Kilo Patron

Hi @samadam 

 

I tried running the script shared by you on my PDI and it is working as expected both in Global Scope and in a scoped application. I will suggest you to check if you are passing the right sys_id of the credential record. Moreover, check if your Fix Script scope has access to the credential record or not.

 

Also, you have missed a ; in the first line of your code, 

var provider = new sn_cc.StandardCredentialsProvider()

Please add that as well.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

I checked that and both are in the same scope. Get this error "Cannot convert null to an object."

@samadam 

 

Can you please validate the sys_id of the credential record again ? I validated this scenario by passing wrong sys_id and I am able to get the same error.

AmitVerma_0-1728403645569.png

 

Copy the sys_id of your credentials by going to discovery_credentials table -> right click on your credential record -> Click copy sys_id

AmitVerma_1-1728403710503.png

 


Please mark this response as correct and helpful if it assisted you with your question.

The basic auth profile I created is in the "sys_auth_profile_basic" table. Do I need to create a credentials table in the table you mentioned for this to work?