Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Setting Variable to Mandatory on/before Task Closure

Tee1
Giga Contributor

Hi All,

Hopefully someone can help with the following. it sounds simple enough, but I can't get what I want to do to work.

What I need to happen is for a variable on a request task form to become mandatory on or before the 'Close Task' action. I have explored this via UI Policy and got it to work for it to be mandatory. However, the issue I have with that method is the task will need some work completing before variable information is acquired and you cannot save it as the field has already become mandatory.

The method I am now exploring is the onSubmit client script, but I am open to other options...

function onSubmit() {

    var action = g_form.getActionName();

    if (action != 'close_sc_task')

          return;

    var state = '3'; // Closed Complete State

    if (action == 'close_sc_task');

    g_form.setMandatory('business_email', true); // business email is the variable that need to become mandatory

}

What I have seen with the above script is that the business email variable flashes briefly with the mandatory asterisk, but the task closes anyway. Any help would be appreciated.  

Many Thanks...

1 ACCEPTED SOLUTION

I think I used the wrong syntax for the business rule. After Helsinki, they changed the syntax and no longer use onBefore() onAfter() methods. See if you can use this in your Set g_scratchpad for sc_task business rule:


(function executeRule(current, previous /*null when async*/) {


      g_scratchpad.wf_activity = current.wf_activity.getDisplayValue();


})(current, previous);


View solution in original post

28 REPLIES 28

Has Vasmi,



Thank you for your help, but no luck with this i'm afraid. The buiness email field doesn't even flash for a brief second to suggest it has become mandatory as it did before.



Kindest Regards,


i observed a semi coloumn for if condition, i think you dont need it.
And can you check the return value for email and set corresponding condition in "IF". And added a "{ }"
and what is the field type for business_email ?



function onSubmit() {


    var action = g_form.getActionName();


    if (action != 'close_sc_task')


          return;



var email = g_form.getvalue('business_email'); // reading the value


    var state = '3'; // Closed Complete State



    if (action == 'close_sc_task')


{


if(!email) // will work if email field is string,wont work if its a reference, you have to update condition to check undefined


{


    g_form.setMandatory('business_email', true); // business email is the variable that need to become mandatory


        return; // added return here so the task wont be closed


}


}


}


it is a string field, i had tried having a return; at the end already and again i'm afraid it hasn't worked.


Sorry yesterday i just gave my thinking on the reply.


this is working and tested in my instance. nothing else to change. and I also found that this will not work if user right click on title pane and select save as the ui action then triggered will be 'sysverb_update_and_stay'. If you want that to be included then u can check if state is '3'   as   if (action == 'close_sc_task' && g_form.getvalue('state')=='3')



Updated Code


function onSubmit() {


    var action = g_form.getActionName();


    if (action != 'close_sc_task')


          return;



var email = g_form.getValue('business_email'); // reading the value




    if (action == 'close_sc_task')


{


if(email=='') // will work if email field is string,wont work if its a reference, you have to update condition to check undefined


{


    g_form.setMandatory('business_email', true); // business email is the variable that need to become mandatory


        g_form.setValue('state',2);


        alert('please fill email');


        return false; // added return here so the task wont be closed


}


}


}


Did it work mohammed ? any luck with my code snippet?