We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

User criteria addEncodedQuery

Bradley
Tera Contributor

Good Morning,

I'm writing a script for the user criteria to check if the company = a specific sys ID and grade is either two sys IDs

 

 

var userGR = new GlideRecord('sys_user');  // Create a new GlideRecord object for the sys_user table
userGR.get(user_id);
var encoded = gr.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^u_grade=122a41e01b2998947ca8620e6e4bcb62^ORu_grade=d22a41e01b2998947ca8620e6e4bcb62^ORu_grade=5a2a41e01b2998947ca8620e6e4bcb62^ORu_grade=1e2a41e01b2998947ca8620e6e4bcb62');  // Filter to only show users with the grade 6/SCS and in the company HMRC
if (userGR == encoded)
    answer = true;
else
    answer = false;
Currently not working. Is their a better way to write and get it working?
1 ACCEPTED SOLUTION

@Bradley 

the working code I already shared, did you check that?

var userGR = new GlideRecord('sys_user'); // Create a new GlideRecord object for the sys_user table
userGR.addQuery('sys_id', user_id);
userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^u_grade=122a41e01b2998947ca8620e6e4bcb62^ORu_grade=d22a41e01b2998947ca8620e6e4bcb62^ORu_grade=5a2a41e01b2998947ca8620e6e4bcb62^ORu_grade=1e2a41e01b2998947ca8620e6e4bcb62'); // Filter to only show users with the grade 6/SCS and in the company HMRC
userGR.query();
answer = userGR.hasNext();

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

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

View solution in original post

6 REPLIES 6

_Gaurav
Kilo Sage

Hi @Bradley Can you try the below code and let me know if this works for you?


var userGR = new GlideRecord('sys_user');  // Create a new GlideRecord object for the sys_user table
userGR.get('user_id');
var encoded = userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^u_grade=122a41e01b2998947ca8620e6e4bcb62^ORu_grade=d22a41e01b2998947ca8620e6e4bcb62^ORu_grade=5a2a41e01b2998947ca8620e6e4bcb62^ORu_grade=1e2a41e01b2998947ca8620e6e4bcb62');  // Filter to only show users with the grade 6/SCS and in the company HMRC
if (userGR == encoded)
    return true;
else
return false;
Please mark as a solution and helpful if this resolves your query.
Thanks!

sunil maddheshi
Tera Guru

@Bradley 

Please try with below updated code:

var userGR = new GlideRecord('sys_user');  
userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^' + 
                       'u_grade=122a41e01b2998947ca8620e6e4bcb62^OR' + 
                       'u_grade=d22a41e01b2998947ca8620e6e4bcb62');  
userGR.addQuery('sys_id', user_id);  // Ensure the query applies to the current user
userGR.query();  

var answer = userGR.next();  // If a record is found, answer is true

Please mark correct/helpful if this helps you

Thank you for that. I've tested on my side and still not working sorry

@Bradley  I did a mistake, could you try now

var userGR = new GlideRecord('sys_user');  
userGR.addEncodedQuery('company=e1c9d9a31b15d4d09732dd39cd4bcbbe^' + 
                       'u_grade=122a41e01b2998947ca8620e6e4bcb62^OR' + 
                       'u_grade=d22a41e01b2998947ca8620e6e4bcb62');  
userGR.addQuery('sys_id', gs.getUserID());  // Ensure the query applies to the current user
userGR.query();  
if(userGR.next()){
  var answer=true;
}