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

Harshit
Tera Contributor

Sure.



On closure of the task have the below activities added in the workflow -


find_real_file.png



1. "IF" activity can have the below sample script -


answer = ifScript();



function ifScript() {



  if(current.variables.MY_VARIABLE_NAME == "") { //can add a check on a catalog task field as well


  return 'no';


  }


  else


  return 'yes';


}



2. "Run Script" to print the message saying the following fields are mandatory on task closure -


errorMessage();



function errorMessage() {



  var mandatory_fields = new Array();


  if(current.variables.MY_VARIABLE_NAME == "")


  mandatory_fields.push("MY VARIABLE");



  var wk = "Following mandatory fields are not filled in: ";


  for (i in mandatory_fields) {


  if (i < (mandatory_fields.length - 1)) {


  wk += mandatory_fields[i] + ", ";


  } else {


  wk += mandatory_fields[i];


  }


  }


  gs.addErrorMessage(wk);


}



3. "Rollback To" activity to open the same task again.



Hope this helps!!



Best regards,


Harshit


Sharique Azim
Mega Sage

Hi Tahir ,



You can modify the OOB UI action or make a replica of the OOB and make the OOB inactive,   in the client part set the field   mandatory .


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Look at the out of the box "Resolve" UI action on the incident table.   It checks to make sure a few fields are populated and if not prevents save.   You should be able to leverage that UI action as an example of what you need to do for your use case.


Hi Micheal.



This is exactly what I did before posting, but it falls over at the last hurdle. Hitting close task does for a moment make the field that i need populating mandatory (red asterisks shows up) but it closes anyway! Below is the script that I used...



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


}



Any thoughts as to why this could be?



Kindest Regards


Vamsi Saladi
Tera Contributor

Just made few changes to your existing code.



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


}


}



if email has already value then no need to make field mandatory or check anything.