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

using below script i need to update ritm closer comments

mani55
Tera Contributor

i am using below script to update closer comments in ritm but it's inserting the dummy record not updating record

 var grval = new GlideRecord('sc_req_item');
            grval.addQuery('sys_id','e03fae4b477eb6189c5e5cbd436d439e');
			grval.query();
            grval.close_notes = 'test';
            grval.update();

 

Any idea on this please let me know

4 REPLIES 4

YashwanthV18760
Giga Guru

Hi @mani55 

The issue with your script is that you are calling grval.close_notes = 'test'; and grval.update(); before checking if the record actually exists. If the query does not return any record, then grval.update() will create a new record instead of updating an existing one.

 

You need to check if the record was found by using grval.next() before updating the field and calling update().

 

Here is the corrected version of your script:

var grval = new GlideRecord('sc_req_item');
grval.addQuery('sys_id', 'e03fae4b477eb6189c5e5cbd436d439e');
grval.query();

if (grval.next()) {
grval.close_notes = 'test';
grval.update();
} else {
gs.info('Record not found');
}

 

Explanation:
grval.query() runs the query.
grval.next() moves the pointer to the first record found (if any).
Only if a record is found, you update the field and call update().
If no record is found, you can log or handle that case accordingly.

This will ensure you update the existing record instead of inserting a new one.

may i know why below script is not working

 

var grval = new GlideRecord('sc_req_item');
grval.addQuery('sys_id', input.ritm_sys_id + '');
grval.close_notes = input.comments;
grval.update();

@mani55 

the reason it's not working is you are querying but not going to the actual record with next() method

update() method will create a dummy record if it's used directly

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Ankur Bawiskar
Tera Patron

@mani55 

you should first check if record is present or not

var grval = new GlideRecord('sc_req_item');
grval.addQuery('sys_id', 'e03fae4b477eb6189c5e5cbd436d439e');
grval.query();
if (grval.next()) {
    grval.close_notes = 'test';
    grval.update();
}

OR

var grval = new GlideRecord('sc_req_item');
if (grval.get('e03fae4b477eb6189c5e5cbd436d439e')) {
    grval.close_notes = 'test';
    grval.update();
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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