.update() does not work and returns null

Yuriy7
Kilo Contributor

Often fails to update the record fields

var req_parent = new GlideRecord('sc_req_item');

req_parent.get(current.request_item.sys_id)

req_parent.state = 3;

// then I try to update the record

var req_id = req_parent.update();

gs.log("##### For " + req_parent.number + " - success. ID of the updated record: " + req_id);

This code does not work in a small number of cases (maybe 1-5%)

In the log, I see this message:

"##### For RITM4756385 - success.  ID of the updated record: null"

Although in most cases I see the return value here - the sys_id of records.

Who faced something similar? What diagnostics can be done to identify the cause?

20 REPLIES 20

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Did you try adding try catch block for exception handling?

also print last error message while updating. it will tell any business rule which is before update is blocking the update.

try{
var req_parent = new GlideRecord('sc_req_item');

req_parent.get(current.request_item.sys_id)

req_parent.state = 3;

// then I try to update the record

var req_id = req_parent.update();

var errorMessage = req_parent.getLastErrorMessage();

gs.log('Error Message is: ' + errorMessage);

gs.log("##### For " + req_parent.number + " - success. ID of the updated record: " + req_id);
}
catch(ex){

gs.log('Exception is: ' + ex);

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Yuriy7
Kilo Contributor

Yes, I have a similar block.

try {

....

} catch(e) {

gs.log("##### For " + req_parent.number + ' Error ' + e.name + ":" + e.message + "\n" + e.stack);
}

but it does not cause an error - the operation is normally performed, but does not change the record and returns null to me

Jaspal Singh
Mega Patron
Mega Patron

Hi Yuriy,

 

Can you try using

var req_parent = new GlideRecord('sc_req_item');

req_parent.get(current.request_item.sys_id)

req_parent.state = 3;

// then I try to update the record

req_parent.update();

var req_id =req_parent.number;

gs.log("##### For " + req_parent.number + " - success. ID of the updated record: " + req_id);

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct onthe impact of response.

DirkRedeker
Mega Sage
Hi I think, there may be a business rule preventing to set the state to 3 for the records failing. Can you cross check and let me know. BR Dirk