The CreatorCon Call for Content is officially open! Get started here.

Advance User Criteria Scripting

vidhya_mouli
Giga Sage
// Load the user record from customer_contact table
var user = gs.getUserID();
var userGR = new GlideRecord('customer_contact');
userGR.get(user);

var parentAccountName = userGR.account.account_parent.name + '';
var targetAccountName = 'Test Super Parent 001';

var answer = (parentAccountName == targetAccountName);

 

Not sure why this script is not working.

 

1 ACCEPTED SOLUTION

vidhya_mouli
Giga Sage

This script worked

var answer = false;
targetAccountName = "Test Super Parent 001";

var userGR = new GlideRecord('customer_contact');
userGR.addQuery("sys_id", user_id);
userGR.addQuery("account.account_parent.name",targetAccountName);
userGR.query();

answer = userGR.hasNext();

 

The issue was caching. Everytime before I impersonate someone, I had to do cache.do and then impersonate to make it work.

View solution in original post

6 REPLIES 6

Anand2799
Tera Guru

Hi @vidhya_mouli ,

 

Do not use gs.getUserID(); in user criteria script instead use user_id to get the current user.

 

var parentAccountName = '';
var targetAccountName = 'Test Super Parent 001';
var answer = false;

var userGR = new GlideRecord('customer_contact');
userGR.addQuery('user', user_id); //verify correct field to query data 
userGR.query();
if(userGR.next()){
 parentAccountName = userGR.account.account_parent.name + '';
}

var answer = (parentAccountName == targetAccountName);

 

Thanks

Anand 

I tried using user_id initially and that did not work.

Ankur Bawiskar
Tera Patron
Tera Patron

@vidhya_mouli 

are you sure that user has contact record?

try this

// Load the user record from customer_contact table
var user = user_id;
var userGR = new GlideRecord('customer_contact');
if (userGR.get(user)) {

    var parentAccountName = userGR.account.account_parent.name + '';
    var targetAccountName = 'Test Super Parent 001';

    answer = (parentAccountName == targetAccountName);

} else {
    answer = false;
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Yes. i tried running the script as a BG and it return true. But not in the UC.