Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Creating User Criteria Advanced Script issues

Kim R
Kilo Explorer

Hello - 

I am trying to setup some User Criteria on Knowledge Pages that basically will let a user who's company is active and marked as a customers along with their user is active. I have tried the below, but when applied it blocked all users with this setting. Can anyone help me fix the below code? 

 

if (gs.getUser().getCompanyRecord().u_active_status == 'Active'
&& gs.getUser().getCompanyRecord().customer == true
&& gs.getUser().active == true)
answer = true;

1 ACCEPTED SOLUTION

Not applicable

Hi Kreding,

This issue is usually faced when running the script outside a function without creating an avenue for evaluating to false:

https://community.servicenow.com/community?id=community_question&sys_id=69b8c70edb78af4423f4a345ca96...

 

In your case, please try the below script:

checkCondition();

function checkCondition(){

var user = new GlideRecord('sys_user');
user.get(gs.getUserID());

if (user.company.u_active_status == 'Active' && user.company.customer == true && user.active == true)
{
return true;
}

else{
return false;
}

}

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron

Hi,

 

I think it should be as below.

if (gs.getUser().getCompanyRecord().getValue('u_active_status') == 'Active'
&& gs.getUser().getCompanyRecord().getValue('customer') == true
&& gs.getUser().getRecord().getValue('active')== true)
answer = true;

For some reason it didn't work with Get Value. I appreciate the response. 

Not applicable

Hi Kreding,

This issue is usually faced when running the script outside a function without creating an avenue for evaluating to false:

https://community.servicenow.com/community?id=community_question&sys_id=69b8c70edb78af4423f4a345ca96...

 

In your case, please try the below script:

checkCondition();

function checkCondition(){

var user = new GlideRecord('sys_user');
user.get(gs.getUserID());

if (user.company.u_active_status == 'Active' && user.company.customer == true && user.active == true)
{
return true;
}

else{
return false;
}

}

Rajesh Kannan G
ServiceNow Employee

Do not use the session API gs.getUserID in User Criteria script instead use the pre-defined variable user_id.

find_real_file.png

https://docs.servicenow.com/bundle/newyork-it-service-management/page/product/service-catalog-manage...

 

var userGR = new GlideRecord('sys_user');
userGR.get(user_id);
if(userGR.company.u_active_status == 'Active' 
   && userGR.company.customer == true 
   && userGR.active = true)
  answer = true;
else
  answer = false;

Mark as Correct Answer & Helpful if this solves your issue or Helpful if this is useful to you.

Regards,

Rajesh