Updating sys_user table using workflow

Ramel
Mega Guru

Hi All,

Good day! I am trying to update the sys_user table fields with the value entered from the last completed task. I added a runscript in my workflow that includes the script to update the sys_user table based from the entered value in the task that was previously closed. However, what's happening is it is creating a new record in the sys_user table instead of updating the existing record.

Here's my script:

var gr = new GlideRecord("sn_hr_core_task");

gr.get(workflow.scratchpad.task1); //this is the task where I am getting the value of the 2 fields that I need to update in the sys_user table

gr.query();

if (gr.next()) {

var user = new GlideRecord("sys_user");

user.initialize();

user.u_gpid = gr.u_gpid;

user.email = gr.u_email_address;

user.update();

}

I am using user.update() but still inserting a new record. Any advice what am I I doing incorrectly?

Thanks in advance.

Ramel

8 REPLIES 8

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Ramel,



You need to use addQuery method and then update the record found. Reference.


http://wiki.servicenow.com/index.php?title=GlideRecord#gsc.tab=0


Sample code for your reference.



var rec = new GlideRecord('sys_user');


rec.addQuery('sys_id',f2e1e8314f2d0300f3823879b110c723"); //Here I'm finding the user with sys_id f2e1e8314f2d0300f3823879b110c723


rec.query();


if (rec.next()) {


//If the record is found


rec.active = false; //deactivate the user


rec.update();


}


Hi Pradeep,



Thanks for the response. I am actually at the task in the workflow, that is why in my script I refer first to the sn_hr_core_task table, then if that is true only then I update the details in the sys_user table. In your example it is directly querying the sys_user, but the value i would need will be coming from the task.



Thanks,


Ramel


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Ramel,



That is fine What I mean is that you need to use addQuery method(while doing GlideRecord to sys_user table) to find the exact record and then update it.