Get all variables on a task form via script

nickmayo
Kilo Contributor

Hey all,

I'm trying to do something that may or may not be simple.

Basically, we have mandatory fields on our Catalog Tasks for users to populate. Where we're at however, users will pass tasks between each other or try to assign them before populating the mandatory tasks. The problem we run into is once you go to a task, you can assign and save the record without filling out those mandatory tasks. I'm trying to avoid making a UI policy for every mandatory task we have that says "be non mandatory unless the task state changes to Closed Complete".

So my question is: Is there a way to grab all mandatory variables off a Catalog Task form VIA Client script? Or at least grab all variables regardless of mandatory?

Or even (hopefully?) simpler, is there a way to modify this code to grab all the variables in the map, then check if they are mandatory? If so, set them non mandatory? I've been banging my head against this for awhile and a scalable solution would be perfect so we don't have to create a new UI policy everytime we need a new variable.

function onLoad(){

    try{

          //Get the 'Variables' section

          var ve = $('variable_map').up('table');

          //Disable all elements within with a class of 'cat_item_option'

          ve.select('.cat_item_option', '.slushselectmtm', '.questionsetreference').each(function(elmt){

        //CURRENTLY this will make ALL variables read only. I would like something like the following

        //if(elmt.mandatory = true){

        //         g_form.setMandatory(false);

        //}

                elmt.disabled = true;

          });

 

    catch(e){}

}

15 REPLIES 15

Ahh.. I do not think this will work with a display business rule. Since


display business rule is triggerred before a record is actually displayed.


I will recommend to use a glideajax in a client script and put the server


side script in a script include.



On Monday, July 25, 2016, nickmayo <community-no-reply@servicenow.com>


Try it and let me know if you have any questions.


So I've got it to return the variables, but it's still only pulling ones thats are actually marked as mandatory vs ones changed to mandatory via UI policy.



I put it in an onChange script instead, but even if I run it after the page loads it doesn't pull the ones affected by the UI policy. Any suggestions? The code is running fine, but I think I just need to see if there is a way to check if a UI policy is affecting the variables. Below is my code so far:



Client Script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue == 3) {


  var ga = new GlideAjax('mandatoryVariables');


  ga.addParam('sysparm_name','MandatoryVariables');


  ga.addParam('sysparm_value', g_form.getValue('request_item')); //send RITM number


  ga.getXML(ajaxResponse);


  }


}




function ajaxResponse(serverResponse) {


  var result = serverResponse.responseXML.getElementsByTagName("result");


  var message = result[0].getAttribute("message");


  if(message){


  alert(message);}



  var output = "";


  var variables = serverResponse.responseXML.getElementsByTagName("variable");


  for (i=0; i < variables.length ; i++){


  //g_form.setMandatory(variables[i],false);


  var name = variables[i].getAttribute("name");


  output += name + "\n" ;


  }


  alert(output);


}



Script include:


var mandatoryVariables = Class.create();


mandatoryVariables.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  MandatoryVariables: function(){


  var result = this.newItem("result");


  result.setAttribute("message", "returning mandatory variables");


  var set = new GlideappVariablePoolQuestionSet();


  set.setRequestID(this.getParameter('sysparm_value'));


  set.load();


  var vs = set.getFlatQuestions();


  for (var i = 0; i < vs.size(); i++) {


  if(vs.get(i).mandatory==true){


  this._addVariable(vs.get(i).getName());


  }


  }


  },



  _addVariable : function(name) {


  var vars = this.newItem("variable");


  vars.setAttribute("name", name);


  },




  type: 'mandatoryVariables'


});


nickmayo
Kilo Contributor

Bump - does anyone have any ideas here?



We need this functionality. I've found other threads where folks are adding UI actions and making all variables non mandatory, but I need only a couple marked as non mandatory. UI policies are not an option here.


Do you want to make everything as non-mandatory or only a specific variables?