- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 04:39 AM
I am running into a duplicate key issue when trying to generate a proc_po record for each vendor on a order from our catalog.
The script works fine without the for loop, but when added it generates one record correctly with a unique key, then when creating the next record it tries to use the same key as the first record again and i get this duplicate key error.
Unique Key violation detected by database (Duplicate entry '8be27fc5dbf47700483a2c860596195a' for key 'PRIMARY')
Has anyone ran into a similar issue or do you see anything that stands out as wrong in my code below?
Any help would be greatly appreciated.
//find bir
var bir = new GlideRecord('u_bir_comp');
bir.addQuery('u_request',current.sys_id);
bir.query();
//get vendors
var vendors=[];
var rit = new GlideRecord('sc_req_item');
rit.addQuery('request',current.sys_id);
rit.query();
while(rit.next()){
//store unique vendors in 'vendors' array
gs.log(vendors.indexOf(rit.cat_item.vendor.sys_id.toString()));
if(vendors.indexOf(rit.cat_item.vendor.sys_id.toString()) == -1){
vendors.push(rit.cat_item.vendor.sys_id.toString());
}
}
gs.log('po vendors= '+vendors);
for (i=0; i < vendors.length; i++) {
gs.log('starting po for vendor ' + vendors[i]);
//create po header for each unique vendor
var po = new GlideRecord('proc_po');
po.initialize();
//po.sys_id=gs.generateGUID;
po.budget_number= current.u_budget;
po.ship_to= current.u_branch;
po.requested_by= current.opened_by;
po.u_request= current.sys_id;
po.u_total= current.price;
po.u_state='requested';
po.u_attention_to= current.opened_by.getDisplayValue();
po.u_address_1= current.u_address_1;
po.u_address_2= current.u_address_2;
po.u_city= current.u_city;
po.u_state= current.u_state;
po.u_zip_code= current.u_zipcode;
po.init_request= current.sys_id;
po.u_bir= bir.u_number.getDisplayValue();
po.vendor= vendors[i];
po.insert();
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 05:33 AM
8be27fc5dbf47700483a2c860596195a is the sys_id of the first PO that gets generated
In further testing I removed everything except for po.initialize() and po.insert() and it successfully added 2 empty records.
When one by one un-commenting lines i found that po.u_request= current.sys_id was causing the issue
changing the line to a string po.u_request= current.sys_id.toString(); fixed the issue
I am not 100% sure why, but this resolved it
thank you for looking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 05:21 AM
Hello
Did you find what record is the sys_id 8be27fc5dbf47700483a2c860596195a referring to?
Regards
Dhianan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 05:33 AM
8be27fc5dbf47700483a2c860596195a is the sys_id of the first PO that gets generated
In further testing I removed everything except for po.initialize() and po.insert() and it successfully added 2 empty records.
When one by one un-commenting lines i found that po.u_request= current.sys_id was causing the issue
changing the line to a string po.u_request= current.sys_id.toString(); fixed the issue
I am not 100% sure why, but this resolved it
thank you for looking
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2019 05:26 AM
Someone more knowledgeable than me, can perhaps take a look the rest and give their two cents, but I would suggest trying and using the newRecord() method, instead of initialize() and insert(). newRecord() method does the same as initialize, excepts it also assign a unique id to the record:
https://developer.servicenow.com/app.do#!/api_doc?v=jakarta&id=r_GlideRecord-newRecord
Check also this YouTube video for the two methods:
https://www.youtube.com/watch?v=WURlMTc2HFE
Maybe this is not your problem - but perhaps worth a shot?
Also, have you checked where sys_id it spits out, is located? Was it created by you/this script?
Hope this helps