Make catalog task variables mandatory on "Close Complete"

SC10
Kilo Guru

I'd like to force a variable to be mandatory on my catalog task to be mandatory when the catalog task form is being submitted AND state = closed complete (3).

I tried doing this with a business rule, and it sort of works.. but the "Close Complete" UI action bypasses it as it forces a redirect and this apparently doesn't work in conjunction with .setAbortAction.Trying to look for some alternatives as I would like the entire page to stop loading/redirecting when using the Close Complete button, or setting to Closed Complete manually, and make the variable mandatory:

  1. (function executeRule(current, previous /*null when async*/) {  
  2.   if(current.variables.offboard_desktoprecoverassets == '' && current.state == "3"){  
  3.        
  4.   current.state=previous.state;  
  5.   current.setAbortAction(true);  
  6.   gs.addInfoMessage('NOTICE: Please select an option for desktop recovery below completing the task.');  
  7.   gs.setRedirect(current);  
  8.   }  
  9.  
  10.  
  11. })(current, previous);  
1 ACCEPTED SOLUTION

You can write a onSubmit client script and put this script in there


function onSubmit() {


  var ritm= g_form.getReferenec("request_item");


  if((g_form.getValue('state')==3) && (ritm.cat_item=='Your cat item sys_id')){


  if(g_form.getValue('variables.<variable_name>')==''){


  g_form.setMandatory('variables.<variable_name>',true);


  return false;


  }


  }


}


View solution in original post

24 REPLIES 24

current.state = 3;


current.update();



Thank you


Use both client side and server side in the Ui action.Check the client checkbox in the Ui action and put runClientCode(); in the onClick of UI action and give the Ui action an action name. Copy this script in there


Client & Server Code in One UI Action - ServiceNow Guru



function runClientCode(){


  var ritm= g_form.getReference("request_item");


  if(ritm.cat_item=='Your cat item sys_id'){


  if(g_form.getValue('variables.<variable_name>')==''){


  g_form.setMandatory('variables.<variable_name>',true);


  return false;   //Abort submission


  }


  }


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


  gsftSubmit(null, g_form.getFormElement(), '<button_action_name>'); //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')


  runBusRuleCode();


//Server-side function


function runBusRuleCode(){


  current.state = 3;


  current.update();


}


A question regarding this change to the UI action, would this not impact the Close Complete UI action on every catalog task? This variable and it being mandatory is Closed Complete state is isolated to my one catalog item workflow and the task it is creating.


Well then you need to create a new Ui action with the same name and copy this script in there and put this in the Ui action conditions


current.request_item.cat_item=='sys_id of catalog item'



For the existing UI action, put this condition in there. So that it will only be visible for other catalog items


current.request_item.cat_item!='sys_id of catalog item'


Doing that right now, but instead of checking the cat_item on the catalog task, I am looking for the wf_activity sys_id, as this catalog task is going to be one of many within my catalog item workflow.



Added in the condition, but it doesn't seem to be quite working as I had planned. Do you know by chance if wf_activity == '<sys_id>' can be used in a UI action condition?