How to access a variable on a catalog task?

PranavSuj96
Mega Expert

Hello all,

I am writing an onSubmit script which will not allow the user to close the task if a variable from the catalog item that I display on the catalog task (I display it by selecting it in the workflow using the slushbucket). I tried using g_form.getValue('variables.variable_name'). This seems to not work...

 

Any ideas why? I'll post the script below. Also the onsubmit script is also preventing saving/updating..

Script:

function onSubmit() {

 if(g_form.getActionName('close_task') && g_form.getValue('short_description') == 'Precheck for Server Snapshot' && (g_form.getValue('variables.mc_storage')!='false' || g_form.getValue('variables.mc_storage')!='true')) {
  var val = g_form.getValue('variables.mc_storage');
  alert(val.getDisplayValue());
  return false;
 }
 return true;
}

1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

Based upon what you describe, I am guessing that you want to have specific variables become mandatory only on a particular task, and only when the record becomes closed. If this is true, we can use a UI Policy on the Catalog Task table (not a Catalog UI Policy) to achieve this. I have done this before, so if this is the case and you want more information, let me know.

View solution in original post

34 REPLIES 34

Hey This is what I have now. It seems like the submit is happening before my script include can finish running...

 

 

OnSubmit:

 

function onSubmit() {

alert("STARTING!");
var ga = new GlideAjax('FindVariableValue');
ga.addParam('sysparm_name','findVal');
ga.addParam('sysparm_var','mc_storage');
ga.addParam('sysparm_ritm',g_form.getDisplayBox('request_item').value);
ga.getXML(parse);
alert("1.5");
 
function parse(response) {
 alert('2');
 var answer = response.responseXML.documentElement.getAttribute("answer");
 alert(answer);
}

}

 

 

Script Include:

 

var findVariableValue = Class.create();
findVariableValue.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
 findVal:function() {
  var name = this.getParameter('sysparm_var');
  var ritm = this.getParameter('sysparm_ritm');
  gr = new GlideRecord('sc_req_item');
  gr.addQuery('number',ritm);
  if(gr.hasNext()) {
   return gr.getValue(name);
  }
  
  
 },

    type: 'findVariableValue'
});

 

Any ideas?

Hi PranavSuj96,

Try getXMLWait() instead and see if that helps. The way you get answer back from the server is a bit different. This link is helpful https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/server_scripting/reference/r_ExamplesOfSynchronousGlideAjax.html

Also, depending on the value you get back, "return true" to allow submission or "return false" to abort submission. 

hey Phong, according to my client coding standards I can't use synchronous ajax. There's no work around right? 😞

Hi PranavSuj96,

If that is the case, maybe we need to rethink how we want to tackle this. I re-read your post description and it looks like you want to satisfy some conditions before allowing the user to close out the task? If so, why dont we make the client script an "OnChange" instead of "OnSubmit"?

It goes like this: When the state changes to "Close Complete" (this means the user is trying to close out the task", you run your asynchronous Ajax function to get the value back, then depending on what you get back, we allow the user to close the task out or not. Since this is now an onChange client script, it should work with your Asynchronous Ajax. What do you think?

Thanks,

Phuong