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.

Duplicate records on insert or update

zacks
Tera Contributor

Hello,

I am using a run script to update or insert a new record. However, I observed that my script is creating multiple records. Sometimes, it creates like 40 or so records with only 1 being the correct record that. My code is below.

///

var grp = new GlideRecord('proc_po');

grp.addQuery('number',current.sys_id); //Check if number & sys_id exists

if(grp.next()){ //check if records exists

grp.setValue('vendor', current.variables.u_vendor); //vendor

grp.vendor_account = current.variables.u_vendor_account;   //

                              grp.update();

}else {

grp.initialize();

grp.short_description = current.cat_item.name;

grp.setValue('vendor', current.variables.u_vendor); //vendor

grp.setValue('product_catalog', current.cat_item.name); //set Item Requested

grp.setValue('ship_to', current.variables.u_ship_to);

grp.setValue('requested_by', current.request.requested_for); //set Requested by

  grp.insert();

///

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Zack,



Code seems to be fine you can try using something as below



///


var grp = new GlideRecord('proc_po');


grp.addQuery('number',current.sys_id); //Check if number & sys_id exists


if(grp.next())


{ //check if records exists


gs.log('In loop for group update');


grp.setValue('vendor', current.variables.u_vendor); //vendor


grp.vendor_account = current.variables.u_vendor_account;   //


                              grp.update();


}


else


{


gs.log('In loop for creationi of grup');


var grp1 = new GlideRecord('proc_po');


grp1.initialize();


grp1.short_description = current.cat_item.name;


grp1.setValue('vendor', current.variables.u_vendor); //vendor


grp1.setValue('product_catalog', current.cat_item.name); //set Item Requested


grp1.setValue('ship_to', current.variables.u_ship_to);


grp1.setValue('requested_by', current.request.requested_for); //set Requested by


  grp.insert();


}


///



Have added appropriate logs for update kindly use that to check & get better understanding.


Thanks a lot Jaspal,



It didn't quite work as I expected. I still have multiple records created.


Did you get any chance to monitor the logs.


Thanks Jaspal.



Yes, the log shows the same number of instance of record creation.