Unique Key violation detected by database - Transform Map

Ayman Lhend1
Giga Guru

Hello,

I'm trying to do an import via a transform map that uses a script to properly import data

In this code below, i'm formatting fields of the column User so that it can fit the ServiceNow standards for Reference type fields (firstName lastName).

But unfortunately i always get the error "Unique Key violation detected by database ((conn=659) Duplicate entry 'bfc6fac0972611103f0f7b771153af05' for key 'PRIMARY')".

I do realize that a lot of people already posted about this error, but i just can't find a solution for my case in the forum, thank you.

AymanLhend1_0-1665403171262.png

N.B : user is a reference field to the system table sys_user

1 ACCEPTED SOLUTION

So after a lot of debugging and headache, i found where the error is coming from.

When i get the first element of the name via nonformattarr[0], somehow JS puts a space just before the beginning of the string, which messes up the whole thing and generates the error.

I just had to remove the space at the beginning of the string with String.trim().

Thank you so much for the help, really appreciate it.

View solution in original post

6 REPLIES 6

Vipul Sethi
Kilo Guru

I would suggest it's better to  search the full name on user table and then pass the sys_id in target.user rather then display value , In the below example i am assuming ur getting first name in nonformattarr[0] and last name in nonformattarr[1], you can also break the query in first name and last name also but in below ex i have used full name

 

var nonformattname = source.u_user;
var nonformattarr =  nonformattname.split(",");
var usr = new GlideRecord("sys_user");
usr.addQuery("name", nonformattarr[0] +" "+nonformattarr[1] );
usr.query();
if(usr.next())
target.user = usr.sys_id;

 

 

This does not generate any errors, but somehow the user field on the target gets populated with the non formatted value of the name (the one we got from source.u_user)

I've tried commenting out the part where you assign a value to target.user, and only get information about the user from the DB, but the error still persists

AymanLhend1_0-1665480569249.png

 

what value you are getting in usr.name in the info message, Also make sure you have not created field map for the same field because you are setting the value of user via transform script.