UI Policy Script based on query

ssturde
Kilo Expert

I have a pair of fields: 'Group' and 'SubGroup'. Both are reference fields to their own respective tables. 'SubGroup' is dependent on 'Group'. Not every 'Group' record has an associated 'SubGroup' record.

When the 'Group' record is selected, I want to display the 'SubGroup' field only if there are 'SubGroup' records associated with the selected 'Group' record.

I created a UI Policy for this and set the condition as the 'Group' field is not empty. For the Condition True script I used the following, but it isn't working. No matter what, it returns the true condition and the 'SubGroup' field is displayed. Any help is appreciated.

function onCondition() {

  var subCount = 0;

  var sub = new GlideRecord('incident');

  sub.addQuery('u_eadp_dsr_sub_grouping.u_dsr_group', g_form.getValue('u_group_responsible'));

  sub.query();

  while(sub.next()){

  subCount++;

  }

  if(subCount>0){

  g_form.setDisplay('u_sub_group_responsible',true);

  g_form.setMandatory('u_sub_group_responsible',true);}

  else{

  g_form.setMandatory('u_sub_group_responsible',false);

  g_form.setDisplay('u_sub_group_responsible',false);

  }

}

1 ACCEPTED SOLUTION

ssturde
Kilo Expert

I couldn't get my Script Include to return anything but a null value. So, I ended up solving this problem a different way.



I added a field on my Parent 'Group' table for sub-group count.



Then I added a business rule on my 'SubGroup' table that updates the count field on the parent whenever a sub group is added or removed:


Business rule:


(Insert/Delete/After):


gatherSiblings();




function gatherSiblings(){


  var subCount = 0;


  var subGroup = new GlideRecord('u_eadp_dsr_sub_grouping');


  subGroup.addQuery('u_dsr_group', current.u_dsr_group);


  subGroup.query();


  while(subGroup.next()){


  subCount++;


gs.log('parent group is: ' + current.u_dsr_group + ' and the sub Group count is: ' + subCount);


  }


  var dsrGroup = new GlideRecord('u_eadp_dsr_grouping');


  if(dsrGroup.get(current.u_dsr_group)){


  dsrGroup.u_subgroup_count = subCount;


  dsrGroup.update();


  }


}



Then to address the field visibility on the incident form, I used the 'get' function in a client script to check the count value of the group record:


Client Script:


onChange


Field name: Group


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


    if (isLoading || newValue == '') {


          return;


    }


  var group = g_form.getValue('u_group_responsible'); // field name on incident form  


// alert(group); // debug alert to check the value pulled in the group variable


var gr = new GlideRecord('u_eadp_dsr_grouping'); // glide record to the parent group table


  gr.get(group); // get the group from the parent group table


  var answer = gr.u_subgroup_count; //grab the sub group count from the parent group record


// alert(gr.u_subgroup_count); //debug alert to validate return value of the count field



  if(answer>0){ //if the count is greater than 0  


  g_form.setDisplay('u_sub_group_responsible',true); // field is visible  


  g_form.setMandatory('u_sub_group_responsible',true);} // field is mandatory


  else if(answer==null){ // if the count is null  


  g_form.setMandatory('u_sub_group_responsible',false); //field is not mandtory  


  g_form.setDisplay('u_sub_group_responsible',false);} //field is hidden


  else if (answer==0){ // if count is 0  


  g_form.setMandatory('u_sub_group_responsible',false); //field is not mandtory  


  g_form.setDisplay('u_sub_group_responsible',false);} //field is hidden


}


View solution in original post

8 REPLIES 8

sachin_namjoshi
Kilo Patron
Kilo Patron

Instead of UI policy, you can write onchange client script to show subgroup based on group values.


Also, please use GlideAjax in client script to display subgroup values based on group.





Regards,


Sachin


Shishir Srivast
Mega Sage

Hello Steve,



As Sachin told, the best way to do GlideAjax based upon the group table, please have the sample code which might be helpful to develop your solution.



Script Include:


var getSubCat = Class.create();


getSubCat.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  subCat : function(){


  var gr = new GlideRecord('<sub category table name>');     //subcategory table name  


  var cat = this.getParameter('sysparm_cat_name');


  gr.addQuery('<category field name>', cat);    


  gr.query();      


  while(gr.next())


  {


  ids.push(gr.sys_id.toString());


  }


  return 'sys_idIN' + ids;


  },


      type: 'getSubCat'


});




onChange Client Script:


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


  if (isLoading || newValue === '') {


  return;


  }


  var Cat = g_form.getValue('category');


  var details = new GlideAjax("getSubCat");


  details.addParam("sysparm_name", "subCat");


  details.addParam("sysparm_cat_name", Cat);


  details.getXML(ajaxResponse);


  function ajaxResponse(serverResponse) {


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


  alert(answer);


  g_form.setValue('subcategory', answer);


  }


}


Thanx for the code base. I've implemented to Script Include and Client Script, but the answer keeps coming back 'null', regardless of which selection is made in the group.



What did I mess up on?



Script include:


//Name DSRSubGroupCount


//Client Callable == true



var getSubCount = Class.create();



getSubCount.prototype =   Object.extendsObject(AbstractAjaxProcessor, {


      getCount: function() {


              var group = this.getParameter('sysparam_name'); // gather parameter from client script


              var count = new GlideAggregate('u_eadp_dsr_sub_grouping'); //add count on the sub table


              count.addQuery('u_dsr_group',group); // subtable field to query using the group variable parameter passed from client script


              count.addAggregate('COUNT'); //aggregate count of subtable records with the group variable as the parent record


              count.query(); //execute query


              if (count.next()) //on each record


                      return count.getAggregate('COUNT'); //add of the answer


      }


});



Client Script:


//type == onChange


//field name == 'Group Responsible'



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


    if (isLoading || newValue == '') {


          return;


    }


    //Type appropriate comment here, and begin script below


    var group = g_form.getValue('u_group_responsible'); // field name on incident form



//Type appropriate comment here, and begin script below


    var ga = new GlideAjax('DSRSubGroupCount'); //name of script include


ga.addParam('sysparm_name','getCount'); // adding parameter of function name in script include


ga.addParam('sysparam_name',group); //passing parameter of the group name being looked up. group name defined in variable above


ga.getXML(HelloWorldParse);




function HelloWorldParse(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


    alert(answer);




  if(answer>0){ //if the count is greater than 0


  g_form.setDisplay('u_sub_group_responsible',true); // field is visible


  g_form.setMandatory('u_sub_group_responsible',true);} // field is mandatory


  else if(answer==null){ // if the count is null


  g_form.setMandatory('u_sub_group_responsible',false); // field is not mandatory


  g_form.setDisplay('u_sub_group_responsible',false);} //field is hidden


  else if (answer==0){ // if count is 0


  g_form.setMandatory('u_sub_group_responsible',false); //field is not mandtory


  g_form.setDisplay('u_sub_group_responsible',false); //field is hidden


}


}}




This needs to be changed?



var ga = new GlideAjax('DSRSubGroupCount'); //name of script include


to


var ga = new GlideAjax('getSubCount '); //name of script include