We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Why does my GlideRecord update keep creating new record?

sharmi2
Kilo Contributor

Hi

I have an approval workflow (that works fine). If the workflow is approved there's a step to either  update  the status as "approved" in exciting record which i opened. My problem is that, whenever I execute the glideRecord update() function it creates a new record instead updating the existing record. Here's a cut down version of the code:

code:

var gr= new GlideRecord('u_os_storage');
 gr.addQuery('sc_req_item',current.sys_id);
 gr.query();
while(gr.next()){
gr.setDisplayValue('u_status',"Approved");
gr.update();
}

can someone help me how to solve this.

 

3 REPLIES 3

Tony Chatfield1
Kilo Patron

Hi, if the glide query has not returned an object\valid record then the update() function will insert a new record.

You don't make it clear which table you are running the BR on, but I would suspect current.sys_id is a task or an approval record?
And your query probabaly needs to be a dot walk to another table ('u_os_storage') via a reference field?
something along the lines of
current.myCiField.sys_id or current.myTaskField.myCiField.sys_id

Perhaps add some logging\debugging to confirm that current.sys_id is the value you are expecting?

 

Indrajit
Mega Guru

Hey sharmi,

Try  following code, might help you.

var gr= new GlideRecord('u_os_storage');
gr.addQuery('sys_id',current.sys_id);
gr.query();
if(gr.next()){
gr.setDisplayValue('u_status',"Approved");
gr.update();
}
 
Mark Correct and Helpful if it useful.
 
Regards,
Indrajit.

 

Prithviraj Howa
Kilo Expert

Hello,

If you are getting problem in these part of code:

while(gr.next()){
gr.setDisplayValue('u_status',"Approved");
gr.update();
}

 

then use, if(gr.next())

 

Please mark it as Correct and Helpful, if applicable. Thanks!

Warm Regards,

Prithviraj Howal

SNOW Developer