- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2016 05:08 AM
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.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-17-2016 01:06 PM
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.
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 01:32 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 02:26 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 02:28 AM
I will be able to check this by morning 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 04:08 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 01:16 AM
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.