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 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);


Hi Tomasi,



Thanks for your great help



It is working perfectly now. I would prefer running the BR as this output is accepted for my project rather than the hierarchical lists. However, both are useful and the hierarchy method can be implemented for lists in the future projects as well.



Thanks,


Rathika.


Hi Thomas I'm new to servicenow,I have a requirement where on the incident form for open tickets once the short description is given I should  get a pop up box which should give list of knowledge articles with similar short description by searching for keywords.can you please suggest me in detail what need to be done?

Hope I get response quickly