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

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @i_sachin_ ,

 

You code only does the insertion but here is no condition or code to check for updates. Follow the code given below by @Bert_c1  it must work i guess !

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Bert_c1
Kilo Patron

hi @i_sachin_,

 

I tested the code, and it creates/updates a record in wm_order table when an incident is created/updated. I don't understand why you mention "the new incident is not being created" unless that is due to some other problem.  Also, you mentioned " we have to remove the current.update() code in the business rule associated with the table". And that is correct when fields on the current record are to be modified in a Before business rule. See:

 

How Business Rules work.

 

Seem that is also a different problem from what you first posted. But we can help with more details.

 

What table is the record with the reported sys_id value if the error for the record being inserted?

Sumanth16
Kilo Patron

Hi @i_sachin_ ,

 

Check for Business Rules which are running while insering the records 

Check enforced uniqueness on the Task table "Number" field and Remove the enforced uniqueness on the task Number field to resolve the issue and add unique check box on incident table only .

 

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

 

Bert_c1
Kilo Patron

Hi @i_sachin_,

 

Change this BR to run 'After', since it is updating/creating a record in the wm_order table based on values from the incident. Insures the values used for the wm_order record are not modified by a subsequent BR on the incident table.  This change will also allow you to better determine what is causing the Unique Key error.