- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 08:12 AM - edited 02-16-2024 09:50 AM
I have created list type field on change form as shown in below image..
I need to add this values into Affected CIs related tab.
I have made changes in business rule sync ci to affected cis as in below image
I facing an error in getting values from list type into script.
Please help me to get values from list type into array values in business rule script.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 10:45 AM
If those are actually sys_ids of records found on the cmdb_ci table, meaning that is the table used as a Reference in this list field, then change your script like this to account for current.u_affected_companies containing a comma-separated list of sys_ids. The addQuery on line 11 should look like this:
rec.addQuery('ci_item', 'IN', previous.u_affected_companies);
Then inside the addCurrentCI function, that all needs to be wrapped in a for loop to process each value:
function addCurrentCI() {
//Create Affected CI record for the Outage(s) associated with this incident and current CI
var ciArr = current.u_affected_companies.split(',');
for (var i=0; i<ciArr.length; i++){
var rec = new GlideRecord('task_ci');
rec.addQuery('task', current.sys_id);
rec.addQuery('ci_item', ciArr[i]);
rec.query();
if (rec.next()) {
//Affected CI already found, so don't add a duplicate
return;
}
rec.initialize();
rec.task = current.sys_id;
rec.ci_item = ciArr[i];
rec.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 10:20 AM
The ci_item field on the task_ci table is a reference to the Configuration Item (cmdb_ci) table, so you need to populate it with a sys_id of a record found on this table, not on the core_company table which is what I would guess you used as the Reference for your new list field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 10:33 AM
I was getting sys ids from list values as
Script: 74d8108297b5311009247076f053af78,ae0b504a97b5311009247076f053affa
I need to push these into array which I am unable to do and unable to split those values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 10:45 AM
If those are actually sys_ids of records found on the cmdb_ci table, meaning that is the table used as a Reference in this list field, then change your script like this to account for current.u_affected_companies containing a comma-separated list of sys_ids. The addQuery on line 11 should look like this:
rec.addQuery('ci_item', 'IN', previous.u_affected_companies);
Then inside the addCurrentCI function, that all needs to be wrapped in a for loop to process each value:
function addCurrentCI() {
//Create Affected CI record for the Outage(s) associated with this incident and current CI
var ciArr = current.u_affected_companies.split(',');
for (var i=0; i<ciArr.length; i++){
var rec = new GlideRecord('task_ci');
rec.addQuery('task', current.sys_id);
rec.addQuery('ci_item', ciArr[i]);
rec.query();
if (rec.next()) {
//Affected CI already found, so don't add a duplicate
return;
}
rec.initialize();
rec.task = current.sys_id;
rec.ci_item = ciArr[i];
rec.insert();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2023 11:26 AM
To delete Previous Records :
Use this query:-
rec.addEncodedQuery('ci_itemIN'+ previous.u_affected_companies);
To create record for each CI :-
Use the below script:
function addCurrentCI() {
var ci = current.u_affected_companies.split(',');
for (var i in ci) {
var rec = new GlideRecord('task_ci');
rec.addQuery("task", current.sys_id);
rec.addQuery("ci_item", ci[i]);
rec.query();
if (rec.next()) {
return;
} else {
var newrec = new GlideRecord('task_ci');
newrec.initialize();
newrec.task = current.sys_id;
newrec.ci_item = ci[i];
newrec.insert();
}
}
}