- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 09:27 AM
Hello Everyone,
I have written the below script include and the script is not querying the 2nd encodedQuery.
It is not going inside the 2nd encoded query but if I print the encoded query then it is matching with if we copy query from user table.
Query from code: name=Test STRY0322411^manager=a6dfe82fdb34bc901a3efe03f39619aa^location=22e6376d1bce991017fe8550f54bcb41^department=99145e2edb4e0110eb8ea51fd3961913
Query copied from user table: name=Test STRY0322411^manager=a6dfe82fdb34bc901a3efe03f39619aa^location=22e6376d1bce991017fe8550f54bcb41^department=99145e2edb4e0110eb8ea51fd3961913
Can someone tell me what am I doing wrong?
SCRIPT:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 11:44 AM
I'm not sure what you mean by "not going inside the 2nd encoded query". Is it not returning the User record? Or is it throwing an error? I noticed when you are setting the department variable, you are using "Department" as the catalog variable name. They are usually lowercase. Is that a typo? Could that be the issue?
You could add a "gs.info('encodedQuery = ' + user.getEncodedQuery());" right after you set it to see what it ends up looking like. I know you have the "gs.info('Query='+ encodedQuery);", but not 100% the same thing. You'll want to see what the query actually gets set to, although it "should" be the same.
Here's an updated script with a couple gs.info calls thrown in:
var ANUserUtil = Class.create();
ANUserUtil.prototype = {
initialize: function() {},
copyCompany: function() {
var item = new GlideRecord('sc_req_item');
item.addEncodedQuery('cat_item.nameSTARTSWITHCreation of Login (AD) account for contractor^numberSTARTSWITHRITM2075329');
item.query();
while (item.next()) {
gs.info("Found item, will look for User now..."); //*****
var name = item.variables.userfirstname.getDisplayValue() + " " + item.variables.prefix.getDisplayValue() + " " + item.variables.userlastname.getDisplayValue();
var manager = item.variables.userlinemanager;
var company = item.variables.businessunitshortname;
var department = item.variables.Department;
var location = item.variables.userlocation;
var encodedQuery = 'name=' + name + '^manager=' + manager + '^location=' + location + '^department=' + department;
var user = new GlideRecord('sys_user');
user.addEncodedQuery(encodedQuery); //User is active with name,manager, location from the RITM record.
gs.info("encodedQuery = " + user.getEncodedQuery()); //*****
user.query();
if (user.next()) {
gs.info('Anirban company:'+company); //*****
user.company = company;
user.update();
}
}
},
type: 'ANUserUtil'
};
And just for clarity, your comment "User is active with name,manager, location from the RITM record." is not 100% correct. You are NOT actually filtering on Active and you are adding Department into the filter. Comments are good, but should be accurate.
Also, the way you are building the "name" variable may break if you do not have a value in the prefix catalog variable. Depends if it is mandatory and how the User.Name field is calculated. The problem is you may end up with a double space between first and last names which may not match your User.Name calculation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 11:44 AM
I'm not sure what you mean by "not going inside the 2nd encoded query". Is it not returning the User record? Or is it throwing an error? I noticed when you are setting the department variable, you are using "Department" as the catalog variable name. They are usually lowercase. Is that a typo? Could that be the issue?
You could add a "gs.info('encodedQuery = ' + user.getEncodedQuery());" right after you set it to see what it ends up looking like. I know you have the "gs.info('Query='+ encodedQuery);", but not 100% the same thing. You'll want to see what the query actually gets set to, although it "should" be the same.
Here's an updated script with a couple gs.info calls thrown in:
var ANUserUtil = Class.create();
ANUserUtil.prototype = {
initialize: function() {},
copyCompany: function() {
var item = new GlideRecord('sc_req_item');
item.addEncodedQuery('cat_item.nameSTARTSWITHCreation of Login (AD) account for contractor^numberSTARTSWITHRITM2075329');
item.query();
while (item.next()) {
gs.info("Found item, will look for User now..."); //*****
var name = item.variables.userfirstname.getDisplayValue() + " " + item.variables.prefix.getDisplayValue() + " " + item.variables.userlastname.getDisplayValue();
var manager = item.variables.userlinemanager;
var company = item.variables.businessunitshortname;
var department = item.variables.Department;
var location = item.variables.userlocation;
var encodedQuery = 'name=' + name + '^manager=' + manager + '^location=' + location + '^department=' + department;
var user = new GlideRecord('sys_user');
user.addEncodedQuery(encodedQuery); //User is active with name,manager, location from the RITM record.
gs.info("encodedQuery = " + user.getEncodedQuery()); //*****
user.query();
if (user.next()) {
gs.info('Anirban company:'+company); //*****
user.company = company;
user.update();
}
}
},
type: 'ANUserUtil'
};
And just for clarity, your comment "User is active with name,manager, location from the RITM record." is not 100% correct. You are NOT actually filtering on Active and you are adding Department into the filter. Comments are good, but should be accurate.
Also, the way you are building the "name" variable may break if you do not have a value in the prefix catalog variable. Depends if it is mandatory and how the User.Name field is calculated. The problem is you may end up with a double space between first and last names which may not match your User.Name calculation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 11:21 PM
Hi Jim,
Thank you for you reply. I tried individual fields with Query and not encoded query and I found out that name is the problem. It is not able to query the name. E.g. Ricky Martin. The name displaying is exactly same but still the code is not able to do it. For query I am using the below

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-08-2023 11:30 PM
"The name displaying is exactly same but still the code is not able to do it. For query I am using the below"
I don't think this is correct. Like Jim already mentioned, this might end up in having a double space. Just looking at your code, this also will be so if the prefix is empty. It's just by your own design, you choose to have a double empty space while the name field on the sys_user record usually won't have that.
Kind regards,
Mark
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field