Make a variable field mandatory when the catalog task STATE changes to 'Closed Complete'

Johnson13
Kilo Guru
 

What will i use to achieve this, is it Catalog client script or Catalog UI Policy?

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron

@Johnson13 

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_0-1773718769785.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 as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

Thank you Ankur, I used the option 1 to achieved my requirement. 

View solution in original post

4 REPLIES 4

AnirudhKumar
Mega Sage

Use a UI Policy at the Catalog item level and use the Applies to checkbox to make the policy run only on Catalog Task

Tanushree Maiti
Kilo Patron

Hi @Johnson13 ,

Use UI policy (recommended)/ Client script on sc_task to make variable field mandatory when the catalog task STATE changes to 'Closed Complete' .

 

In my project we were facing some issue while tried using ui policy ( not able to recall exact reason), we have achieved it by client script. 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Ankur Bawiskar
Tera Patron

@Johnson13 

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_0-1773718769785.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 as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Thank you Ankur, I used the option 1 to achieved my requirement.