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 09:06 AM - edited 09-21-2023 09:09 AM
That didn't work - next I tried the script below because we made it server side not client and we removed the onclick - so we don't need the functions anymore.
// 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();
}
Still no approval records got deleted.
Maybe I'm using the wrong delete method so it's not deleting the records it finds?
Now the state didn't change either so now I'm not even getting the state back to new.
I checked my change states - they are
// -5 is New
// -4 is Asses
// -3 is Authorize
// -2 is Scheduled
// -1 is Implement
// -0 is Review
// 1 is (not used) but is open or starting phase before save and new
// 2 is (not used) but is Work in Progress phase for other task types
// 3 is Closed Complete
//4 is Canceled
Thanks for the help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 10:57 AM
I went back to my original script and removed the delete items and put it as a client script. The state changes to new so I setup a business rule to do the deletes when state changes to new and because it's a business rule it was able to delete the approval records. This covered the need. Thanks for trying.