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

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");

Perfect! Just what I was looking for.

Thanks for the help.

how can I get credential ID? 

'41cc709cdb144c101bdb5a75dc961990'

how to set user_name and password in beginning? or is it existing credentials of some existing user?

 

 

 

 

How to you get id? its user id?