The CreatorCon Call for Content is officially open! Get started here.

Help on validation on variable for "Close Task" button on catalog task record

Tanya10
Tera Contributor

Hello,
I have a requirement where one catalog variable is there which is visible on catalog task. User should not be able to close the task without filling this variable.
I have create ui policy which works fine if the user change the state from drop down it does make mandatory the variable but if user uses "Close Task" button the catalog task gets closed complete and later the the variable become mandatory.
How can we put restriction on "Close Task" button.
Below is the script

function closeTaskCheck() {
    var catalogItem = g_form.getValue('cat_item');
    // Check if this task belongs to a specific catalog item
    if (catalogItem == "xyz") {
        g_form.setMandatory('variables.link', true);

        if (!g_form.getValue('variables.link')) {
            alert("Please provide the link before closing this task.");
            return; // stop here, don't submit
        }
    }

    // Submit to trigger the server-side script
    gsftSubmit(null, g_form.getFormElement(), 'close_catalog_task');
}
if (typeof window == 'undefined') {
    // Authorization check
    if (current.assigned_to.nil() && gs.getUser().isMemberOf(current.assignment_group)) {
        current.assigned_to = gs.getUserID();
        current.state = 3;
        current.update();
    } else if ((gs.getUserID() == current.assigned_to && gs.getUser().isMemberOf(current.assignment_group)) || gs.hasRole("admin")) {
        current.state = 3;
        current.update();
    } else {
        gs.addErrorMessage("You are not authorized to close this task.");
        current.setAbortAction(true);
    }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Tanya10 

I recently shared solution for something similar, check that and enhance

I shared all 3 ways (Client Script, Close Task button, before update BR)

How to make a catalog item variable mandatory only when the Catalog Task state is "Closed Complete" 

sharing it here again

1) normal onChange client script on state field of sc_task and check if state is closed complete then make those mandatory

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == '3') {
        if (g_form.getValue('variables.variableName1') == '')
            g_form.setMandatory('variables.variableName1', true);
    }

}

2) You need to ensure this is handled via the OOTB Close Task UI action as well as user can close the task from button as well

function closeTask() {
    g_form.setValue('state', 3);
    //Call the UI Action and skip the 'onclick' function

    if (g_form.getValue('variables.variableName1') == '') {
        g_form.setMandatory('variables.variableName1', true);
        return;
    }

    gsftSubmit(null, g_form.getFormElement(), 'close_sc_task');
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    updateTask();

function updateTask() {
    current.state = 3;
    current.update();
}

AnkurBawiskar_0-1758783733468.png

 

 

3) before update business rule on sc_task table

State Changes to Close Complete && current.request_item.cat_item.name == 'Your Item  Name'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    if (current.variables.variableName == '') {
        current.setAbortAction(true);
        gs.addErrorMessage('Please add file before closing task');
    }

})(current, previous);

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

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

View solution in original post

8 REPLIES 8

Brad Bowman
Kilo Patron
Kilo Patron

It may be best to replace the UI Policy with an onChange or onSubmit Client Script on the sc_task table when State changes to handle this, but to try with the UI Action approach first, you need to add this criteria to the server code section, so that it only runs if the variable is populated:

if (typeof window == 'undefined') {
    if (current.cat_item.name == 'xyz' && current.variables.link == '') {
        gs.addErrorMessage("The mandatory variable must be populated prior to closing the task..");
        current.setAbortAction(true);
       
    // Authorization check
    } else if (current.assigned_to.nil() && gs.getUser().isMemberOf(current.assignment_group)) {
        current.assigned_to = gs.getUserID();
        current.state = 3;
        current.update();
    } else if ((gs.getUserID() == current.assigned_to && gs.getUser().isMemberOf(current.assignment_group)) ||     gs.hasRole("admin")) {
        current.state = 3;
        current.update();
    } else {
        gs.addErrorMessage("You are not authorized to close this task.");
         current.setAbortAction(true);
    }
}

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Tanya10 

I recently shared solution for something similar, check that and enhance

I shared all 3 ways (Client Script, Close Task button, before update BR)

How to make a catalog item variable mandatory only when the Catalog Task state is "Closed Complete" 

sharing it here again

1) normal onChange client script on state field of sc_task and check if state is closed complete then make those mandatory

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == '3') {
        if (g_form.getValue('variables.variableName1') == '')
            g_form.setMandatory('variables.variableName1', true);
    }

}

2) You need to ensure this is handled via the OOTB Close Task UI action as well as user can close the task from button as well

function closeTask() {
    g_form.setValue('state', 3);
    //Call the UI Action and skip the 'onclick' function

    if (g_form.getValue('variables.variableName1') == '') {
        g_form.setMandatory('variables.variableName1', true);
        return;
    }

    gsftSubmit(null, g_form.getFormElement(), 'close_sc_task');
}
//Code that runs without 'onclick'
//Ensure call to server-side function with no browser errors
if (typeof window == 'undefined')
    updateTask();

function updateTask() {
    current.state = 3;
    current.update();
}

AnkurBawiskar_0-1758783733468.png

 

 

3) before update business rule on sc_task table

State Changes to Close Complete && current.request_item.cat_item.name == 'Your Item  Name'

Script:

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    if (current.variables.variableName == '') {
        current.setAbortAction(true);
        gs.addErrorMessage('Please add file before closing task');
    }

})(current, previous);

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

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

Hi @Ankur Bawiskar 
Thanks it worked

@Tanya10 

Glad to know.

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