How to fetch Related list records using Gliderecord in Client script?

rathikaramamurt
Giga Expert

Hi Experts,

In problem form, there is a related list of its child incidents derived from each problem. I have created a new Reference field "Related Incidents" in the problem form. I need to fetch all incidents for each problem by applying Onload client script querying the incident table, such that all the values in the related list are populated in this field seperated by comma.

Could anyone please me guide on this?

Thanks,

Rathika.

1 ACCEPTED SOLUTION

Hi Rathika,



Here's your business rule for maintaining the u_rel_tasks field if you decide not to go with hierarchical lists. There's one inefficiency to this script in that it runs for as many times as you add or remove incidents from the related list. If you add 4 incidents, it runs 4 times (likely reproducing the same values in many cases.) It's not perfect but it works.



find_real_file.png


find_real_file.png


When: After


Table: Incident


Active: true


Advanced: true


Insert: true


Update: true


Condition: current.problem_id.changes()


Script:


(function executeRule(current, previous /*null when async*/) {



  var parentProblemID = '';


  //This function will be automatically called when this rule is processed.


  var pr = new GlideRecord('problem');



  // If adding it to the list, then use the current.problem_id


  // Otherwise, if it's blank, use the previous value


  if (current.getValue('problem_id'))


          parentProblemID = current.getValue('problem_id');


  else


          parentProblemID = previous.getValue('problem_id');



  if(pr.get(parentProblemID)) {


          gs.log('inside if ... ');


          var incArr = [];


          var inc = new GlideRecord('incident');


          inc.addQuery('problem_id', parentProblemID);


          inc.query();


          while (inc.next()) {


                      incArr.push(inc.getValue('sys_id'));


          }


          pr.u_rel_tasks = incArr.join(',');


          pr.update();


  }



})(current, previous);


View solution in original post

17 REPLIES 17

Hi Tomasi,



The 1st script is executed successfully, by making small changes as mentioned here. Thanks for that.



Just below few concerns about the BR as per your conditions:


*******************


Condns:


To keep the future records up to date, create a BEFORE business rule on incident similar to the following:


Name: Update Problem u_incidents


When: After


Insert: true


Update: true


Advanced: true


Condition: !current.problem.nil()



Queries:


1- It should be applied for Incident table OR Problem table?


2- It will be an Before OR After the process?


3- Based on the condn --> !current.problem.nil()   Here, 'problem' is referring to problem.number form OR incident form's problem field value?



Thanks in Advance,


Rathika.


Hi Ratika



Good questions! See my responses in bold.



1- It should be applied for Incident table OR Problem table? Incident. You want to update the parent problem when an incident is changed. Remember, business rules trigger on database updates. What has been updated? The incident. I should have put the table field in my description. Sorry.


2- It will be an Before OR After the process? Sorry, AFTER. I had it wrong in the text, but right in the field description.


3- Based on the condn --> !current.problem.nil()   Here, 'problem' is referring to problem.number form OR incident form's problem field value? This is the problem field on the incident form. You are only concerned about the incidents that actually have a parent problem. If it doesn't, then there is no parent problem to update - this is not a child incident.


Hi Tomasi,



Thanks for your explanation!



In my instance, For incident form --> Incident number field is called "number" & Problem field in Incident is called "problem_id", Whereas For problem form, Problem number is called "number" and Related tasks field is called "u_rel_tasks".


I have tried to implement this part by modifying the script as below:


************************


function onAfter(current, previous) {


  //This function will be automatically called when this rule is processed.


  var pr = new GlideRecord('problem');


  if(pr.get(current.problem_id)) {


  gs.log('inside if ... ');


  var incArr = [];


  var inc = new GlideRecord('incident');


  inc.addQuery('problem_id.number', current.getValue('number'));


  inc.query();


  gs.log('exit if '+inc.getDisplayValue());


  while (inc.next()) {


  incArr.push(inc.getValue('number'));


  gs.log('inside while '+incArr.getDisplayValue());


  }


  pr.u_rel_tasks = incArr.join(',');


  pr.update();


  gs.log('exit update '+incArr.getDisplayValue());


  }


}



I am wondering if am using correct values in the script, as the code is not executed properly. The logs are updated only with comments, but no values are displayed for any field. Again this script is not working for me.



Please request you to help me on this.



Thanks,


Rathika.


Hi Tomasi,



Have you had any chance to review my last comments? Please take time to look into this if possible. Kindly provide suggestions as this task is critical.



Thanks,


Rathika.


Hi Rathika,



I was just about to start digging in to this again when I thought of the original requirement "Show related incidents on the problem list."



Have you considered using a hierarchical list?



Hierarchical Lists - ServiceNow Wiki