How to give conditions in Ui action

Roshini
Giga Guru

I have a Ui action "cancell" in change task table, but for certain task name"PIR" i need to check if the person who is trying to cancel is part of a particular group.

i am trying to do the below code, but it is not working, can someone please help



if (current.short_description == 'PIR')
{

if (gs.getUser().isMemberOf(gs.getProperty('approverteam')))
{

    return moveToCancelled();

}
else
{
    gs.addErrorMessage("you are not allowed to do this action");
}
}
function moveToCancelled() {
    g_form.setValue('state', 4);
    g_form.setValue('close_code', 'cancelled');
    g_form.setMandatory('close_notes', true);
    gsftSubmit(null, g_form.getFormElement(), "change_task_to_cancelled");
}
if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}
1 ACCEPTED SOLUTION

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Roshini 

Try below code in your ui action script.

 

gs.GetProperty('property_name') and isMemberOf() cannot be used in client part of the code, so you can write aDisplay BR and store the property value to a g_scratchpad variable. You can then use it in ui action script.

 

Display BR logic:

g_scratchpad.approverteam= gs.getUser().isMemberOf(gs.getProperty('approverteam'));

Ui Action code:

function moveToCancelled() {
   var group_member = g_scratchpad.approverteam; //write a display BR to store if user is part of group or not.
    if (g_form.getValue('short_description') == 'PIR' && group_member ) {
        g_form.addErrorMessage("you are not allowed to do this action");
        return false;
    }
    g_form.setValue('state', 4);
    g_form.setValue('close_code', 'cancelled');
    g_form.setMandatory('close_notes', true);
    gsftSubmit(null, g_form.getFormElement(), "change_task_to_cancelled");
}
if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

1 REPLY 1

Voona Rohila
Kilo Patron
Kilo Patron

Hi @Roshini 

Try below code in your ui action script.

 

gs.GetProperty('property_name') and isMemberOf() cannot be used in client part of the code, so you can write aDisplay BR and store the property value to a g_scratchpad variable. You can then use it in ui action script.

 

Display BR logic:

g_scratchpad.approverteam= gs.getUser().isMemberOf(gs.getProperty('approverteam'));

Ui Action code:

function moveToCancelled() {
   var group_member = g_scratchpad.approverteam; //write a display BR to store if user is part of group or not.
    if (g_form.getValue('short_description') == 'PIR' && group_member ) {
        g_form.addErrorMessage("you are not allowed to do this action");
        return false;
    }
    g_form.setValue('state', 4);
    g_form.setValue('close_code', 'cancelled');
    g_form.setMandatory('close_notes', true);
    gsftSubmit(null, g_form.getFormElement(), "change_task_to_cancelled");
}
if (typeof window == 'undefined')
    setRedirect();

function setRedirect() {
    current.update();
    action.setRedirectURL(current);
}

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP