Unique Key violation deteceted by database ((conn=1035582) Duplicate entry '2fba04741b64c2504e3ba756

i_sachin_
Tera Contributor

I'm currently facing this issue in my ServiceNow instance. I have a Business Rule that successfully creates a work order from an incident. However, despite the creation of the work order, I'm consistently encountering the following error message. 
Error Message: Unique Key violation deteceted by database ((conn=1035582) Duplicate entry '2fba04741b64c2504e3ba756

 

Here is the code I'm using to create work order from incident:

when to run condition : after insert and update

 

(function executeRule(current, previous /*null when async*/ ) {
   
        var WO = new GlideRecord('wm_order');
        WO.initialize();
        WO.caller = current.caller_id;
        WO.u_incident = current.number;
        WO.location = current.location;
        WO.priority = current.priority;
        WO.state = current.state;
        WO.assignment_group = current.assignment_group;
        WO.assigned_to = current.assigned_to;
        WO.short_description = current.short_description;
 
 
        var WorkOrderID = WO.insert();
   
})(current, previous);

And also I have one more issue, When attempting to update an existing incident, the script should update the values in the respective work order. However, instead of updating the existing work order, a new work order is created, leading to data redundancy and potential confusion.
1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Hi i_sachin_,

 

Replace the logic with the following:

 

	var WO = new GlideRecord('wm_order');
	// check for exising record.
	WO.addQuery('u_incident', current.number);
	WO.query();
	if (WO.next()) {
		// update existing record
		WO.caller = current.caller_id;
		WO.location = current.location;
		WO.priority = current.priority;
		WO.state = current.state;
		WO.assignment_group = current.assignment_group;
		WO.assigned_to = current.assigned_to;
		WO.short_description = current.short_description;
		var WorkOrderID = WO.update();
	}
	else {
		// none found, so create new record.
		WO.initialize();
		WO.caller = current.caller_id;
		WO.u_incident = current.number;
		WO.location = current.location;
		WO.priority = current.priority;
		WO.state = current.state;
		WO.assignment_group = current.assignment_group;
		WO.assigned_to = current.assigned_to;
		WO.short_description = current.short_description;
		var WorkOrderID = WO.insert();
	}

View solution in original post

8 REPLIES 8

Bert_c1
Kilo Patron

Hi i_sachin_,

 

Replace the logic with the following:

 

	var WO = new GlideRecord('wm_order');
	// check for exising record.
	WO.addQuery('u_incident', current.number);
	WO.query();
	if (WO.next()) {
		// update existing record
		WO.caller = current.caller_id;
		WO.location = current.location;
		WO.priority = current.priority;
		WO.state = current.state;
		WO.assignment_group = current.assignment_group;
		WO.assigned_to = current.assigned_to;
		WO.short_description = current.short_description;
		var WorkOrderID = WO.update();
	}
	else {
		// none found, so create new record.
		WO.initialize();
		WO.caller = current.caller_id;
		WO.u_incident = current.number;
		WO.location = current.location;
		WO.priority = current.priority;
		WO.state = current.state;
		WO.assignment_group = current.assignment_group;
		WO.assigned_to = current.assigned_to;
		WO.short_description = current.short_description;
		var WorkOrderID = WO.insert();
	}

Hey @Bert_c1, thanks for your response. When I'm updating, the new incident is not being created, but I still get this error message:

Error Message: Unique Key violation detected by database ((conn=1130883) Duplicate entry '7fa18d421ba486607247a8e22a4bcb39' for key 'PRIMARY') while trying to create a new work order from the incident.


I have seen in other community posts that we have to remove the current.update() code in the business rule associated with the table, but I don't have any business rules like that. Can you help with this part?

Hey @Bert_c1, thanks for your response. When I'm updating, the new incident is not being created, but I still get this error message:

"Error Message: Unique Key violation detected by database ((conn=1130883) Duplicate entry '7fa18d421ba486607247a8e22a4bcb39' for key 'PRIMARY') while trying to create a new work order from the incident."

I have seen in other community posts that we have to remove the current.update() code in the business rule associated with the table, but I don't have any business rules like that. Can you help with this part.

Hey @Bert_c1 actually, the code you provided is working perfectly. When I first tried to run your code, I made a small mistake in the code, but now it's working. Thank you for your time!