- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2021 04:21 AM
Hello!
I created a custom list field in the Change Request form. The list field uses the cmdb_ci table.
The requirement is for every CI added to the Affected CIs related list, it should be copied over to the custom list field.
Also, when an Affected CI is removed from the related list, it should also be removed from the list field.
I have created a BR but it does not work. I am running it against the task_ci table
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord("change_request");
gr.get(current.ci_item);
gr.setValue('u_ci_affected', current.sys_id);
gr.update();
})(current, previous);
Please help!
Thank you.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2021 06:56 AM
The change request record is going to match the Affected CI Task field, not the CI. I assume this is the BR running after Insert on task_ci with no Filter Conditions. What you need to do is see if the task on the affected CI record matches a change request. If it does, then add the CI to existing value of the list field, which is just a comma-separated list of sys_ids, so your script would look like this.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord("change_request");
if(gr.get(current.task)){
var listArr = gr.u_ci_affected.toString().split(',');//creates an array of current List field values
listArr.push(current.ci_item.toString());//add the newly added Affected CI to the List field values
gr.setValue('u_ci_affected', listArr.join(',');
gr.update();
}
})(current, previous);
You'll want another Business Rule running after Delete on task_ci with no Filter Conditions. That script would look like this.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord("change_request");
if(gr.get(current.task)){
var listArr = gr.u_ci_affected.toString().split(',');//creates an array of current List field values
for(var i=0;i<listArr.length;i++){
if(listArr[i] == current.ci_item.toString()){//if this element in the array is the CI to be removed
listArr.splice([i], 1);//remove the affected CI being deleted from the array
gr.setValue('u_ci_affected', listArr.join(',');
gr.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2021 05:54 AM
Hello!
I have modified my BR and am able to update the description but not the list field.
I'm pretty sure I need to use an array and ArrayUtil() to make this work but do not know how to use it in the script.
(function executeRule(current, previous /*null when async*/ ) {
var gr2 = new GlideRecord("cmdb_ci");
gr2.addQuery('sys_id', current.ci_item);
gr2.query();
if (gr2.next()) {
var gr = new GlideRecord("change_request");
gr.get(current.task);
gr.setValue('u_ci_affected', gr2.sys_id);
gr.setValue('description', gr2.sys_id);
gr.update();
}
})(current, previous);
Please help!
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2021 06:56 AM
The change request record is going to match the Affected CI Task field, not the CI. I assume this is the BR running after Insert on task_ci with no Filter Conditions. What you need to do is see if the task on the affected CI record matches a change request. If it does, then add the CI to existing value of the list field, which is just a comma-separated list of sys_ids, so your script would look like this.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord("change_request");
if(gr.get(current.task)){
var listArr = gr.u_ci_affected.toString().split(',');//creates an array of current List field values
listArr.push(current.ci_item.toString());//add the newly added Affected CI to the List field values
gr.setValue('u_ci_affected', listArr.join(',');
gr.update();
}
})(current, previous);
You'll want another Business Rule running after Delete on task_ci with no Filter Conditions. That script would look like this.
(function executeRule(current, previous /*null when async*/ ) {
var gr = new GlideRecord("change_request");
if(gr.get(current.task)){
var listArr = gr.u_ci_affected.toString().split(',');//creates an array of current List field values
for(var i=0;i<listArr.length;i++){
if(listArr[i] == current.ci_item.toString()){//if this element in the array is the CI to be removed
listArr.splice([i], 1);//remove the affected CI being deleted from the array
gr.setValue('u_ci_affected', listArr.join(',');
gr.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2021 07:10 AM
This worked! Thank you so much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2021 07:44 AM
You are welcome.