Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to prevent the same record from being updated at the same time

matsui
Tera Contributor

I found Simultaneous Update Alert script my instance:

// Warn when two users are editing the same record
function onSubmit() {
var updatedOn = gel('onLoad_sys_updated_on').value;
if (!updatedOn)
return;

var gr = new GlideRecord(g_form.getTableName());
if (!gr.get(g_form.getUniqueValue()))
return;

var dbUpdatedOn = gr.sys_updated_on + '';
var dbUpdatedBy = gr.sys_updated_by + '';
if (updatedOn != dbUpdatedOn)
return confirm(new GwtMessage().getMessage("{0} updated this record since you opened it - overwrite with your changes? Additional comments and Work notes will not be overwritten.", dbUpdatedBy));
}

Based the above, it will submit, when user select yes in confirm message.
I don't want to submit, so I changed this script like the following, but it did not worked.
Even if I pushed update button, it didn't happen anything. In other word, I can't save the record.

// Warn when two users are editing the same record
function onSubmit() {
var updatedOn = gel('onLoad_sys_updated_on').value;
if (!updatedOn)
return;

var gr = new GlideRecord(g_form.getTableName());
if (!gr.get(g_form.getUniqueValue()))
return;

var dbUpdatedOn = gr.sys_updated_on + '';
var dbUpdatedBy = gr.sys_updated_by + '';
if (updatedOn != dbUpdatedOn)
confirm(new GwtMessage().getMessage("{0} updated this record since you opened it - overwrite with your changes? Additional comments and Work notes will not be overwritten.", dbUpdatedBy));
return false;
}

What should I do for it?

2件の返信2

Sandeep Rajput
Tera Patron
Tera Patron

@matsui 

return false;

In the last line of your script is not letting the form submit. Remove this line and the form will start submitting.

@Sandeep Rajput 

My explanation was not enough.
When this script is run, I will not be able to submit in all cases (even if I am the only one who has opened the record).
Also,confirm message did not show. I could not understand that reason.