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

Thank you, I'm no longer getting a null response, but I'm also not getting any count back. My 'alert' isn't producing any response in my client script.


Hi Steve,



as per the below link


http://wiki.servicenow.com/index.php?title=GlideAjax#Using_GlideAjax



Using GlideAjax


  • Initialize GlideAjax with the name of the script include that you want to use.
  • When creating the script include, you must set the name field to be exactly the same as the class name.
  • When creating the script include, you must select the Client callable check box.
  • Specify the parameter sysparm_name. GlideAjax uses sysparm_name to find which function to use.
  • Any extra parameters may be passed in, all of which must begin with sysparm_.
    • Avoid using predefined parameter names.
      • sysparm_name
      • sysparm_function
      • sysparm_value
      • sysparm_type


Please check, you have wrong variable name in group, can you please correct and change the variable name since sysparm_name is already being used.


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


After changing that, please get the same parameter in script include.


I've corrected the scripts as follows, but still I'm getting a return value in my alert that shows the count



Script include:


var getSubCount = Class.create();



getSubCount.prototype =   Object.extendsObject(AbstractAjaxProcessor, {


      getCount: function() {


              var group = this.getParameter('sysparm_group');


              var count = new GlideAggregate('u_eadp_dsr_sub_grouping');


              count.addQuery('u_dsr_group',group);


              count.addAggregate('COUNT');


              count.query();


              if (count.next())


                      return count.getAggregate('COUNT');


      }


});



Client script:


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


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


          return;


    }


  var group = g_form.getValue('u_group_responsible');


  //alert(group);



    var ga = new GlideAjax('getSubCount');


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


ga.addParam('sysparm_group',group);


ga.getXML(HelloWorldParse);


  //alert(HelloWorldParse);




function HelloWorldParse(response) {


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


    alert(response.responseXML.documentElement.getAttribute("answer"));


{


  if(answer>0){


  g_form.setDisplay('u_sub_group_responsible',true);


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


// else if(answer==null){


// g_form.setMandatory('u_sub_group_responsible',false);


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


  else if (answer==0){


  g_form.setMandatory('u_sub_group_responsible',false);


  g_form.setDisplay('u_sub_group_responsible',false);}


}


}


}


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


}