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,



I tried to reproduce this on my end and figured out the issue was in line


inc.addQuery('problem_id', pr.getValue('number')); should be inc.addQuery('problem_id.number', pr.getValue('number'));


The below script is working fine now.


Final Script :


var pr = new GlideRecord('problem');


pr.query();


gs.addInfoMessage('Problems');



while (pr.next()) {


        var incArr = [];


        var inc = new GlideRecord('incident');


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


        inc.query();


        gs.addInfoMessage('Incidents ' +inc);


        while (inc.next()) {


                  gs.addInfoMessage('enter while ... ');


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


                  gs.addInfoMessage('exit while ... ' +incArr);


          }


        pr.setWorkflow(false);


        pr.autoSysFields(false);


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


        gs.addInfoMessage('outside while ... ' +incArr);


        pr.update();


}





Please let me know if you have any questions.


Hi Pradeep,



Thanks for your update



The modified script is updating all incident list in the logs, but it is not updated in the problem form reference field. I tried this again by replacing the sting field, instead of reference field. Still there are some issues. An additional thing I observed is that the problem record is getting replicated. There are 4 records created for same problem number.



Thanks,


Rathika.


I will be able to check this by morning 🙂


Hi Pradeep,



The issue is solved now!



I can see the values being added in the Problem list view for String type field. Multiple records were created coz I tried with pr.insert();



Now again after replacing this function, it is working fine.



Thanks,


Rathika.


Hi Tomasi,



I have tried the same even by applying gs.log(), where I am getting null values as below:


Problems [object GlideRecord]



Could you please assist me on modifying the script?



Also, as per your above comments the BR is applied for Incident table. I am wondering if this will work out as the Reference field is added in the Problem form. Please correct me if I am wrong.



Thanks,


Rathika.