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

Nishant8
Giga Sage
Giga Sage

Hello @Tanya10, you can convert your UI policy to Data Policy and verify. Keep the logic same since as you mentioned that it works fine when user changes the state manually.

Under the Data Policy Rule keep your field as mandatory.

 

Regards,

Nishant

Tanya10
Tera Contributor

I want to prevent this behavior -when user clicks on that UI Action button. It should not close complete the task if the  mandatory field is not filled in.
Manually change of state from drop down is taken care by UI policy

Hello @Tanya10, Yes, Data Policy should help you in this case. You can write a simple Data policy as below ( replace 'your field'  with actual field). Please let me know if you face any problem while using it - I can help you customize as per your requirement.

- Data Policy configuration:

Nishant8_0-1758778188484.png

- Data Policy Rule

Nishant8_1-1758778248642.png

 

Regards,

Nishant

Tanya10
Tera Contributor

Hi,
In my case it's catalog item variable not a field on the form