Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Records are not getting inserted

vtred
Tera Contributor

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.

10 REPLIES 10

Jon Barnes
Kilo Sage

The first thing I noticed is this line below



var x = GlideRecord('table2');



should be



var x = new GlideRecord('table2');


Also, not sure what these lines are doing, but I would change them as well



var abc = x.u_configuration_item = array[i].toString();


var xyz = x.Field2= current.sys_id;



like this:



x.u_configuration_item = array[i].toString();


x.Field2= current.sys_id;


reginabautista
Kilo Sage

Hi vt, try the below code. I've tested this in my personal dev instance and is working perfectly.



var array= current.u_test_list.toString().split(','); //change to name of your list field


  // var array = list.split(",");


  for (var i=0; i < array.length; i++) {



  var x = new GlideRecord('table2');


  x.Initialize();


  var abc = array[i];


  x.u_configuration_item =abc ;


  gs.addInfoMessage("arrayString="+abc);


  var xyz = current.sys_id;


  x.field2= xyz;


  gs.addInfoMessage("current sysid="+xyz);


  x.insert();


  }


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?