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.

How To Use "Basic Auth Credentials" In Scripts?

JDX7913
Tera Guru

What do I type in a business rule or scheduled job to access the credentials on the basic_auth_credentials table?

Right now, for testing purposes, I have the password and username in plain text, but of course, I would like to move that over the basic_auth_credentials table and have the script access the table to get the password and username. 

I tried to look up sample code, but most of what I found on ServiceNow's doc are C#, VB, etc., and not for JavaScript. Moreover, some docs are on basic auth credentials, but it is mainly on how it works and not how to access via JavaScript from a script. 

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hi,

There is an API for this called StandardCredentialsProvider. It allows you to easily and securely get the credentials from a credentials record.

Example use (sys_id is the id of the credential record):

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

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

View solution in original post

7 REPLIES 7

@js20  that id is the sys_id of your credential record.

i think it should be a "new" here: 

var provider = new sn_cc.StandardCredentialsProvider()

Something I found when trying to get @Kieran Anson 's code to work, was that I had to add 'new' in front of 'sn_cc.StandardCredentialsProvider()', as below

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

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