Records are not getting inserted
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-28-2017 05:39 PM
Hello,
I have two tables
1. Table1 - Field1(list type - referencing to cmdb_ci)
2. Table2 - Field2(Reference type - cmdb_ci)
Table2 is under Related Lists on Table1
Here, If i have 10 values in the Field1 from cmdb_ci, all those 10 values should get inserted as 10 new records in Table2 on Field2.
Here is the script i wrote - its getting the values under logs. but never getting the records inserted on Table2
ONBEFORE INSERT ON Table1:
function onBefore(current, previous) {
var list = current.Field1.toString();
var array = list.split(",");
for (var i=0; i < array.length; i++) {
var cis = array[i].toString();
gs.addInfoMessage("hi1="+cis);
var x = GlideRecord('table2');
x.Initialize();
var abc = x.u_configuration_item = array[i].toString();
gs.addInfoMessage("hi2="+abc);
var xyz = x.Field2= current.sys_id;
gs.addInfoMessage("hi3="+xyz);
x.insert();
}
}
Can someone help me find the solution?, That would be very helpful.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2017 01:46 AM
Hi Red,
Please modify the Business Rule as of below. I have tested in my instance and is working fine. Please let me know if you face any issue after implementing the same. You must write the After Insert, Update Business rule in Table 1.
(function executeRule(current, previous /*null when async*/) {
if (!current.field1.nil())
addCurrentCI();
if (!previous.field1.nil())
removePreviousCI();
function addCurrentCI() {
//Create Table2 record for this task and current selected CIs in field1
var rec = new GlideRecord('table2');
rec.addQuery('field_name', current.sys_id); //Please pass the field name which refers current record
rec.addQuery('field2', 'IN', current.field1);
rec.query();
if (rec.next())
return;
var ci = current.field1;
var array = ci.split(',');
for (var i=0; i < array.length; i++){
rec.initialize();
rec.field2= array[i];
rec.field_name= current.sys_id; //Please pass the field name which refers current record
rec.insert();
}
}
function removePreviousCI() {
//DeleteTable2 record for this task and and previous CIs in field1
var rec = new GlideRecord('table2');
rec.addQuery('field_name', current.sys_id); //Please pass the field name which refers current record
rec.addQuery('field2', 'IN', previous.field1);
rec.query();
while (rec.next())
rec.deleteRecord();
}
})(current, previous);
I hope this helps. Please mark correct/helpful based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2017 04:23 PM
Thank you For your reply,
I modified the script and ran in my Personal developers instance - Its working fine
When i Run it in my actual company instance its not working.
Any Suggestions on this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2017 04:26 PM
Can you post a screenshot of your business rule?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2017 06:06 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2017 11:45 PM
Hi Red,
What do you mean by not working? Do you mean that the record(s) is getting inserted by not showing properly? Please find below observations from mys side.
1. By your script, I can see that the record will get inserted obviously. But if you did not map with the proper fields it won't show the record with correct mapping. Please ensure the fields against which you are mapping the array[i] and current.sys_id.
2. Also I can see that in your personal instance you have created the Business rule from scratch and is working. Whereas in your target instance (where you actually run the code), you have added the lines in the middle of the existing code. Please ensure that when the Business rule runs in your target instance it goes through the newly added lines and not restricted by any if() or while() loop. If I'm not wrong this might be the reason.
I hope this helps. Please mark correct/helpful based on impact