Condition for button was not working

Fahimas
Tera Contributor

Dear team,

              Iam new to serviceNow, i have an issue that my button is not visible even wheb the condition was true!

my condition is: current.state === 1 && gs.getUserID().isMemberOf(current.assignment_group)

explanation: the button must be visible when the state was new and the loggedin user was a member of the current assignment group.the button was not visible evn when condition become true.i impersonate myself as an member of the assignment grp and state was new but the prblm ws button wasnt visible.

preview: i have checked form button

                i have checked everything from reading other article evnthough itwasnot working

                

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @Fahimas ,

 

 

 

The type of current.state will return an object and when you compare an object with number it will return you false, since you are using AND operator when the initial condition is false it will not test the other conditions. You can use == to compare state value.

 

And instead of getUserID use getUser(). getUser() return entire user object where as getUserID() return only the sys_id of the particular user.

 

current.state == 1 && gs.getUser().isMemberOf(current.assignment_group)

 

 

 

Mark this helpful/ Accept the solution if this helps.

View solution in original post

19 REPLIES 19

Community Alums
Not applicable

Hi @Fahimas ,

 

 

 

The type of current.state will return an object and when you compare an object with number it will return you false, since you are using AND operator when the initial condition is false it will not test the other conditions. You can use == to compare state value.

 

And instead of getUserID use getUser(). getUser() return entire user object where as getUserID() return only the sys_id of the particular user.

 

current.state == 1 && gs.getUser().isMemberOf(current.assignment_group)

 

 

 

Mark this helpful/ Accept the solution if this helps.

Thanks for your help , it worked abundantly. 

i have one request that i also have an issue with onhold button

condition: current.state == 2&&(gs.getUser().isMemberOf(current.assignment_group)||(gs.getUser===current.assigned_to))

code:

//getting field value and setting them mandatory through clientscript
if(typeof Window=='undefined')
{
 g_form.setMandatory('hold_reason');
 g_form.setMandatory('comments');
}
//making update through serverscript
function onHold(){  
    current.state='On Hold';
    current.update();
    action.setRedirectURL();
}
explanation:button must be visible when loggedin user was a member of assignment group or equal to assigned_to  field making hold reason mandatory and setting the state into on hold
issue: button was visible these time but the state was not changed and it does not make the above mentioned field into mandatory
 

Community Alums
Not applicable

Hello @Fahimas,

 

Apologies for delayed response. You can try below code snippet. 

current.state = 3;
current.update();

 

g_form.setMandatory('hold_reason',true);
g_form.setMandatory('comments',true);

 

Mark this helpful/ Accept the solution if this helps.

Hi @Community Alums 

      I have tried them but the state was not changing to on hold . What is problem with these !!

Community Alums
Not applicable

HI @Fahimas ,

 

When setting option items for a choice list, always use the value of the option instead of the text. Using the text may prevent the choice list from updating correctly.

Additionally, the methods setVisible(), setReadOnly(), and setMandatory() accept a boolean as the second argument, which determines the action they perform.