Unique Key violation message when updating request short description

ccorscadden
Kilo Contributor

I'm trying to update the short description on my request with the requested item's catalog item display value with an on after business rule. The script works and the short description is updated but I keep getting a message like Unique key violation detected by database (duplicate entry '81135af74f57124029de98701310c7a0' for key primary). I'm not sure why this message is appearing and what is relates to.

My business code like like the following

var request = new GlideRecord("sc_request");

var item = new GlideRecord("sc_cat_item");

request.get(current.request);

item.get(current.cat_item);

request.short_description = item.getDisplayValue();

request.update();


1 ACCEPTED SOLUTION

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Chris,



The error you are getting is a database error and it means that you are trying to do an insert for a sys_id that already exists. This usually happens if you have a before BR with current.update() which would trigger an insert already.



Regards,


View solution in original post

8 REPLIES 8

srinivasthelu
Tera Guru

HI Chris,



Add gets in if condition and check if you have any before business rules with current.update().



var request = new GlideRecord("sc_request");


var item = new GlideRecord("sc_cat_item");


if(request.get(current.request)){


if(item.get(current.cat_item))


{


request.short_description = item.getDisplayValue();


request.update();


}


}



Mark this answer as helpful/correct if it does so


sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Chris,



The error you are getting is a database error and it means that you are trying to do an insert for a sys_id that already exists. This usually happens if you have a before BR with current.update() which would trigger an insert already.



Regards,


Thanks Sergiu



What you described was exactly what was happening, my update() was inserting the record and else where code was running to insert the request record with the same sysid causing the message to appear.



I ended up using action tabs "set field values" to update the request record's short description this accomplished without causing that error message to appear to the user.    


Midhun1
Giga Guru

hi,



Use setWorkflow(false) before update()