- 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-15-2016 05:12 AM
Hi Rathika,
Yes, this can be done, but I'm suspicious if a client script is the correct answer here. Can you help me understand the business object or use case? Since there is a related list, it would appear that the problem is already created and has related incidents.
Can you help me understand why you want to do this with a client script vs. a business rule?
Also, help me understand why the need for a list field (you called it a reference field which only contains one sys_id value), when there is a related list available with the same information.
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2016 05:36 AM
Hi Tomasi,
I created the new field as this could contain list of values referring incidents. This is mandatory requirement as I need to add this in the Problem list view. This can only be done when I create a new field in the form.
I am not sure how to proceed either by Client script or business rule and under which conditions to apply in case of a BR. Hence, I started this with CS, so that this will run while the form loads, as the values are already present under related list.
Please suggest if this is not appropriate or any other available options to implement this.
Thanks,
Rathika.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-15-2016 05:47 AM
Hi Rathika,
Thank you for your response and clarification of the field. Be aware, when there are many entries on your related list, and the list field has lots of content, it could impact the user experience of your list layout.
That being said, let's assume your list field on the problem is called 'u_incidents' and the field incident.problem points to the parent problem. Adjust fields below as necessary.
First, let's update all existing problems with their related incidents for historical purposes. Keep in mind, this may take a while depending on how many you have. Run this script in Scripts - Background (untested)
var pr = new GlideRecord('problem');
pr.query();
while (pr.next()) {
var incArr = [];
var inc = new GlideRecord('incident');
inc.addQuery('problem', pr.getValue('sys_id'));
inc.query();
while (inc.next()) {
incArr.push(inc.getValue('sys_id'));
}
pr.setWorkflow(false);
pr.autoSysFields(false);
pr.u_incidents = incList.join(',');
pr.update();
}
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()
Script:
// Standard function header here
var pr = new GlideRecord('problem');
if (pr.get(current.problem)) {
var incArr = [];
var inc = new GlideRecord('incident');
inc.addQuery('problem', current.getValue('problem'));
inc.query();
while (inc.next()) {
incArr.push(inc.getValue('sys_id'));
}
pr.u_incidents = incList.join(',');
pr.update();
}
// Standard function footer here
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 12:11 AM
Hello Tomasi,
Thanks for your help!
I have tried to run the background script, in my case modified as below:
var pr = new GlideRecord('problem');
pr.query();
gs.addInfoMessage('Problems');
while (pr.next()) {
var incArr = [];
var inc = new GlideRecord('incident');
inc.addQuery('problem_id', 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();
}
The infoMessages are printed, but this script is not getting executed. I am not able to encounter the exact issue, as the problem list has not got any updates.
Please require your suggestions...
Thanks,
Rathika.