Adding 2 or more tags to a record via UI Action results in Unique Key violation detected error

Jayden4
Tera Contributor

Hi there, 

 

I am needing to add multiple tags via a UI Action, and when I add them one at a time via the UI Action, this works properly. I have issues when I try to add 2 or more:

Jayden4_5-1742800287323.png

 

 

As soon as I try to add 2 or more, I get the error: 

Unique Key violation detected by database ((conn=798) Duplicate entry '47232ec9c310221020917375e401311f' for key 'PRIMARY') (I added 3 below, only the first tag was successful)

 

Jayden4_2-1742800086320.png

 

47232ec9c310221020917375e401311f is the sysID of the record on the sn_compliance_control table that I am trying to add the tags to:

Jayden4_1-1742799964276.png

 

However on the label_entry table, which is the connection between label (tags) and records, the Table_key is the same SysID as the record that has the tag applied and is not editable even for SysAdmin (I am setting this via the UI Action as <variable>.table_key = current.sys_id):

Jayden4_4-1742800204454.png

 

 


I am 90% sure that this is the Primary key that is the source of the error. Is there any way around this?

 

I am needing to add multiple tags via IF statements in a UI Action, for reporting. I'm needing to add up to 12 tags per record, for a lot of records. 

 

My scripts work perfect as long as there is only 1 tagSysId's listed. Does anyone have any ideas on how I can add multiple tags at once to a record?

3 REPLIES 3

Ankur Bawiskar
Tera Patron

@Jayden4 

share the complete script of the UI action?

did you try to give some sleep between the array iteration and insertion?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

Shivalika
Mega Sage

Hello @Jayden4 

 

It's because you are trying to insert exactly same records again. Even I faced this and actually there was current.update() but not in a BR in a different server script. 

 

The issue is usually caused by a business rule that runs before an insert and contains an current.update() operation. If that's the case, removing the current.update() fixes the issue. Check if you have any BRs of this type on that table.

 

Refer this - https://www.servicenow.com/community/developer-forum/i-am-getting-this-unique-key-violation-detected...

 

Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket. 

 

Regards,

 

Shivalika 

 

My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194

 

My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY

Jayden4
Tera Contributor

I worked it out, I was trying to insert the sys_id referencing a field that does not exist:

This is the bare minimum to add x2 or more tags at once in an easy to read format, if you just need to add the same 2 tags:

var addJGS1 = new GlideRecord("label_entry");
	addJGS1.initialize();
	addJGS1.table = "sn_compliance_control";
	addJGS1.label = "ec37ce54c3e0221020917375e40131f3"; //sysid of your tag
	addJGS1.table_key = current.sys_id;
	addJGS1.title = current.name;
	addJGS1.insert();
	

var addJGS2 = new GlideRecord("label_entry");
	addJGS2.initialize();
	addJGS2.table = "sn_compliance_control";
	addJGS2.label = "41efce18c324221020917375e4013114"; // sysid of your tag
	addJGS2.table_key = current.sys_id;
	addJGS2.title = current.name;
	addJGS2.insert();
	
action.setRedirectURL(current);​