sc_task work note is mandatory on closed state

hblair
Kilo Expert

Hi all - I have been looking through the wiki to find some help with this.

I have sc_tasks that I want to make work note mandatory when state changes to "closed complete" or "closed incomplete".

I created a ui_policy called "Make work note mandatory on close state".   But when I clicked on the "Close Task" button, the ui_policy didn't work.

Do I need to create a business rule or ui action around the "Close task" button.     If so, can someone give me some direction on how to do so?

Thanks much!

Hank

7 REPLIES 7

SeanM1
Tera Guru

Hi Hank,



A UI policy gets it done, below is an example of how I have done it in my instance



find_real_file.png


Gurpreet07
Mega Sage

state onChange Client script will work for this if you are changing state using state variable but if you are closing the task using Close Task button then you need a before update business rule having following type of code



Condition: current.state.changesTo(7) && current.work_notes=""                       // 7--->Complete


check the Abort Action check box


Script: gs.addErrorMessage("Work Notes Are Mandatory When Closing Task");         //Or you could specify the message in add message box


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Hank,



You can convert the UI Policy to the data policy and it should work in this case. You will find the related link "Convert UI policy to data policy" in the UI policy you have created.


Data Policy - ServiceNow Wiki



Please let us know if you have any questions.


neetusingh
Giga Guru

As per your requirement, I believe that you may want to make the comments field as of mandatory whenever an ui action (Close Task) being clicked upon. So you may modify the code a bit in to your existing ui button to let both of the server side code (that would make your tasks as closed completed after validating the client side checks i.e making the additional field as of mandatory) and client side code (that would mark the comments field as mandatory if the additional field is empty) execute.



You may try the below solution -



Capture5.PNG


script code -


//Client-side 'onclick' function


function closecatalogTask(){


    if(g_form.getValue('comments') == ''){


          //Remove any existing field message, set comments mandatory, and show a new field message


          g_form.hideFieldMsg('comments');


          g_form.setMandatory('comments', true);


          g_form.showFieldMsg('comments','Comments are mandatory when closing a task.','error');


          return false;   //Abort submission


    }


    //Call the UI Action and skip the 'onclick' function


    gsftSubmit(null, g_form.getFormElement(), 'close_task'); //MUST call the 'Action name' set in this UI Action


}




//Code that runs without 'onclick'


//Ensure call to server-side function with no browser errors


if(typeof window == 'undefined')


    closeTask();




function closeTask(){


    //Set the 'State' to 'Active', update and reload the record


    current.state = 3;


    current.update();


    gs.addInfoMessage('Task ' + current.number + ' has been closed.');


    action.setRedirectURL(current);


}