Change - Revert to New - what did I do wrong?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 07:59 AM
Hello out there awesome developers!
I am new to scripting and put together the following script based on a business requirement for a "revert to new" UI Action. They want it to set the state back to new and delete all related approval records (all - meaning all states of approval records not just approved).
So here's what I wrote (pieced together):
function moveToNew() {
g_form.setValue("state", "-5");
gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_new");
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval','current.sys_id');
gr.query();
gr.deletemultiple();
}
if (typeof window == 'undefined')
setRedirect();
function setRedirect() {
current.update();
action.setRedirectURL(current);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 08:02 AM
make the UI action as server side
1) Client checkbox - uncheck
2) Onclick field - clear this
3) Script
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.query();
gr.deletemultiple();
action.setRedirectURL(current);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 08:22 AM
That doesn't seem to have worked. All the approval records are still there in the approval section.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 08:52 AM
Hi @Jeffrey Barton ,
In deleteMultiple 'M' is suppose to be capital Jeffrey. I have update the code n pasted below.
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id);
gr.query();
gr.deleteMultiple();
action.setRedirectURL(current);
Please mark my answer helpful & Accepted if it resolves your query.
Thanks,
Danish Bhairagdar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 08:09 AM
Can you try:
function moveToNew() {
// Make sure that "-5" is indeed the correct state value for "new" in your ServiceNow instance.
g_form.setValue("state", "-5");
gsftSubmit(null, g_form.getFormElement(), "state_model_move_to_new");
// Query for related approval records
var gr = new GlideRecord('sysapproval_approver');
gr.addQuery('sysapproval', current.sys_id); // Use 'current' to reference the current record
gr.query();
// Delete all related approval records
while (gr.next()) {
gr.deleteRecord();
}
if (typeof window == 'undefined') {
setRedirect();
}
function setRedirect() {
current.update();
action.setRedirectURL(current);
}