Only Update records using Record producer

Sujitha2
Mega Guru

Hi,

I am trying to use record producer on sys_user table to update fields like email/dob etc. But the record producer that I have created is not updating properly. Instead its creating a new record. Used this 'current.setAbortAction(true)' but this is not working. Instead getting an error message ->  Error: Unique Key violation detected by database ((conn=11872) Duplicate entry '844b673e87d29d50fea1ebd73cbb35fe' for key 'user_name')

Can someone help me on this? Attaching below script which I used on Script section of Record producer.

 

var grUser = new GlideRecord('sys_user');
grUser.addEncodedQuery('sys_id=' + current.user_id);
grUser.query();
if(grUser.next()){
	grUser.email= producer.email;
        grUser.u_date_of_birth = producer.u_date_of_birth; 
	grUser.u_date_of_joining = producer.u_date_of_joining;
	grUser.update();
	gs.addInfoMessage('User record updated successfully');
        current.setAbortAction(true);
}

 

  Thanks in Advance

1 ACCEPTED SOLUTION

Sujitha2
Mega Guru

I found it myself, I made a mistake on the query. It is supposed to be producer.user_id and not current.user_id

var grUser = new GlideRecord('sys_user');
grUser.addEncodedQuery('sys_id=' + producer.user_id);
grUser.query();
if(grUser.next()){
	grUser.email= producer.email;
        grUser.u_date_of_birth = producer.u_date_of_birth; 
	grUser.u_date_of_joining = producer.u_date_of_joining;
	grUser.update();
	gs.addInfoMessage('User record updated successfully');
        current.setAbortAction(true);
}

 

View solution in original post

7 REPLIES 7

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

 

Record producers per default will always create a record. I'd suggest you use a normal catalog item and execute the script in a flow instead. Then just auto-close the created request and requested item within the flow as well.

 

Regards

Fabian

 

Hi @Fabian Kunzke,

Here the expectation is to get data from Users and update them. We are not expecting any REQ & RITM to be created as they are unnecessary. Anyways thanks for your idea.

Rahul1
Giga Expert

Hi Sujitha,
Can you please recheck for the variable name in record producer. Are they starting with "u_"?
Regards,
Rahul

Hi @Rahul1,

Yes Rahul I am using custom fields. Since Date of Birth & Date of Joining as custom fields that I created in User table. And I have named them as the same, they start with "u_"