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

symonflores_23
Tera Guru

An attachment field variable is hidden in the catalog item view and only becomes visible in the Catalog Task. The fulfiller is expected to attach a file to this variable before setting the task to "Closed Complete."

 

While we can use a Catalog UI Policy to make the field mandatory, the problem is that the attachment variable becomes mandatory regardless of the task’s state. This prevents assigning a user to the Catalog Task, as the system requires the attachment before updating the record.

 

How can we configure it so that the attachment variable is only mandatory when the Catalog Task state is set to "Closed Complete"?

 

To clarify, I want to apply a behavior where the mandatory requirement of the catalog item's attachment variable is enforced only when the Catalog Task’s state is "Closed Complete." The attachment variable is hidden from catalog item view.

 

symonflores_23_1-1753436171410.png 

symonflores_23_2-1753436220317.png

 

 

 

1 ACCEPTED SOLUTION

J Siva
Tera Sage

Hi @symonflores_23 
You can achieve this via either client acript or UI policy in the catalog task (sc_task) table.
UI policy sample:

JSiva_0-1753437450704.png
Modify the highlighted variable name as required.

JSiva_1-1753437478299.png


Regards
Siva

View solution in original post

7 REPLIES 7

Chaitanya ILCR
Kilo Patron

Hi @symonflores_23 ,

 

the screenshots that you have pasted are not visible 

 

 

you can create an onChange client script on sc_task table on State field 

 

and when new value is close complete set the variable mandatory

if(newValue = ="close_complete choice backend value")

 g_form.setMandatory('your_attachment_variable_name',true)

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

G Ponsekar
Mega Guru

Hi @symonflores_23 ,

 

Screenshot is not visible. can you upload as attachments

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@symonflores_23 

3 ways to handle

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_1-1753437318381.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

@symonflores_23 

Thank you for marking my response as helpful.

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