Script not consistently getting credential from credential store in ServiceNow

Aaron Duncan
Mega Sage

I have a script that pulls in a set of credentials to create a payload for another server. In the workflow, it will sometimes not get the username value from the credential store. I'm unable to replicate it in a sub-prod instance, but it errors out saying it cannot convert a null value to an object.

 

 

 var credentialId = 'sys_idForCredentials';
   var provider = new sn_cc.StandardCredentialsProvider();
    var credentials = provider.getCredentialByID(credentialId);
    var username = credentials.getAttribute("user_name");
    var password = credentials.getAttribute("password");

 

 

Has anyone experienced this and if so, how did you solve for it not always pulling the attribute?

1 ACCEPTED SOLUTION

Tushar
Kilo Sage
Kilo Sage

HI @Aaron Duncan 

 

can you please try below script - 

 

try {
  var credentialId = '1ba91ceb1bffd950b7b35284604bcb12';
  var provider = new sn_cc.StandardCredentialsProvider();
  var credentials = provider.getCredentialByID(credentialId);
  var username = credentials.getAttribute("user_name");
  var password = credentials.getAttribute("password");

  // Proceed with payload creation using username and password
} catch (ex) {
  gs.error("Error retrieving credentials: " + ex.message);
} finally {
  // Close any open connections
}

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

View solution in original post

4 REPLIES 4

Tushar
Kilo Sage
Kilo Sage

HI @Aaron Duncan 

 

can you please try below script - 

 

try {
  var credentialId = '1ba91ceb1bffd950b7b35284604bcb12';
  var provider = new sn_cc.StandardCredentialsProvider();
  var credentials = provider.getCredentialByID(credentialId);
  var username = credentials.getAttribute("user_name");
  var password = credentials.getAttribute("password");

  // Proceed with payload creation using username and password
} catch (ex) {
  gs.error("Error retrieving credentials: " + ex.message);
} finally {
  // Close any open connections
}

 

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar

That's not the solution to the problem!

To avoid the evaluator error, the script can be changed like this:

var credentialId = '1ba91ceb1bffd950b7b35284604bcb12';
var provider = new sn_cc.StandardCredentialsProvider();
var credentials = provider.getCredentialByID(credentialId);

if (credentials != null)

{
var username = credentials.getAttribute("user_name");
var password = credentials.getAttribute("password");

}

However, this does not solve the underlying problem that getCredentialByID() does not deliver an existing credential (e.g. if the script is invoked by a non-admin user?)
So, how to ensure that the credential can be loaded?

That's our current problem, in that the getCredentialByID() doesn't consistently work. Any idea how to resolve that?

This may help to fix the issue:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783632

Not a good solution, but it works!