Beginner scripting question: How to use "startsWith" in a User Criteria script

bostonsnow
Kilo Guru

Hello,

Facing a deadline for a go-live and am having issues with the User Criteria for a Service Catalog category.

Basically I just want to show the SC Category to users who have a relationship on their user record to any CI with a name starting in "XYZ". The line of script I currently have to do this is not working (highlighted below). Can anybody help?

------------------------------------------------------------------

answer = checkAccess();

function checkAccess(){
var ci_user = new GlideRecord('sys_user');
ci_user.get(gs.getUserID());
var ci_rel_person = new GlideRecord('cmdb_rel_person');
ci_rel_person.addEncodedQuery(ci.namestartsWithXYZ);
ci_rel_person.addQuery('user', ci_user.sys_id);
ci_rel_person.query();

if (ci_rel_person.next()){
return true;
} else {
return false;
}
return true;
}

------------------------------------------------------------------

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Here you go.

answer = checkAccess();

function checkAccess(){
var ci_user = new GlideRecord('sys_user');
ci_user.get(gs.getUserID());
var ci_rel_person = new GlideRecord('cmdb_rel_person');
ci_rel_person.addQuery('ci.name', 'STARTSWITH', 'XYZ');
ci_rel_person.addQuery('user', ci_user.sys_id);
ci_rel_person.query();

if (ci_rel_person.next()){
return true;
} else {
return false;
}
}

 

- Pradeep Sharma

View solution in original post

7 REPLIES 7

bostonsnow
Kilo Guru

@pradeep sharma or anyone who can help:

I need to make one change to the following addQuery line from the above script so it also includes CI's that start with "ABC". 

ci_rel_person.addQuery('ci.name', 'STARTSWITH', 'XYZ');

Could somebody please help me with the script for this change?

 

Thanks!

Mike

 

Use this row instead.

ci_rel_person.addencodedQuery('ci.nameSTARTSWITHXYZ^ORci.nameSTARTSWITHABC');

Thank you Stewe!! Works perfectly.