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);
    }
}