- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 05:07 AM
Hi ,
I have written an onsubmit client script to close the open change tasks if the state of the change request is either closed skipped or closed failed.
Here is my client script code:
function onSubmit() {
//Type appropriate comment here, and begin script below
var state = g_form.getValue('state');
var close_comments = g_form.getValue('close_notes');
if (state == 7 || state== 4)
{
var ga= new GlideAjax("Checkforopentasks");
ga.addParam('sysparm_name','checkopentasks');
ga.addParam('sysparm_id',g_form.getUniqueValue());
ga.getXML(ServerResponse);
//var ok=confirm("You have open tasks associated with this change.Your tasks will also be closed");
}
function ServerResponse(response){
var answer=response.responseXML.documentElement.getAttribute("answer");
if(answer){
var ok=confirm("You have open tasks associated with this change.Your tasks will also be closed");
if(ok==true){
var ga1= new GlideAjax("Checkforopentasks");
ga1.addParam('sysparm_name','closeskippedtasks');
ga1.addParam('sysparm_id',g_form.getUniqueValue());
ga1.addParam('sysparm_state',g_form.getValue('state'));
ga1.getXMLWait();
var result_start = ga1.getAnswer();
if(result_start){
reloadWindow(window);
GlideList2.get(g_form.getTableName() + '.change_task.change_request').setFilterAndRefresh('');
}
}
else { return false;}
}
else { return false;}
}
}
My script include:
var Checkforopentasks = Class.create();
Checkforopentasks.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkopentasks: function(){
var change_id=this.getParameter('sysparm_id')+ "";
//var state=this.getParameter('sysparm_state');
//var close_notes=this.getParameter('sysparm_closure_note');
var gr= new GlideRecord('change_task');
gr.addQuery('change_request', change_id);
gr.addEncodedQuery('stateIN-5,1,2');
gr.query();
if(gr.next()){
// build new response xml element for result
/*var result = { favorites: []};
result.favorites.push({ 'key': 'present', 'value':"yes"});
result.favorites.push({ 'key': 'state', 'value': state });
result.favorites.push({ 'key': 'closure_notes', 'value':close_notes});*/
return "present";
//return new JSON().encode(result);
}
},
closeskippedtasks : function(){
var state=this.getParameter('sysparm_state');
var change_id=this.getParameter('sysparm_id')+ "";
var gr= new GlideRecord('change_task');
gr.addQuery('change_request', change_id);
gr.addEncodedQuery('stateIN-5,1,2');
gr.query();
while(gr.next()){
gr.state=state;
gr.update();
}
//var thisNumber = current.number.getDisplayValue();
return 'done';
},
type: 'Checkforopentasks'
});
The problem is the related list is not getting refreshed after the update has done.
Thanks,
Jagarnath
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2016 04:26 AM
Hi Chuck,
Later i realized of some best practice and onsubmit client script should go hands in hands with Synchronous Glide Ajax calls . I had that problem because i was using both Asynchronous and synchronous glide ajax calls in my onsubmit client script. Just edited it.
function onSubmit() {
//Type appropriate comment here, and begin script below
var state = g_form.getValue('state');
var close_comments = g_form.getValue('close_notes');
if (state == 7 || state== 4)
{
var ga= new GlideAjax("Checkforopentasks");
ga.addParam('sysparm_name','checkopentasks');
ga.addParam('sysparm_id',g_form.getUniqueValue());
ga.getXMLWait();
var result = ga.getAnswer();
if(result){
var ok=confirm("You have open tasks associated with this change.Your tasks will also be closed");
if(ok==true){
var ga1= new GlideAjax("Checkforopentasks");
ga1.addParam('sysparm_name','closeskippedtasks');
ga1.addParam('sysparm_id',g_form.getUniqueValue());
ga1.addParam('sysparm_state',g_form.getValue('state'));
ga1.getXMLWait();
var result_start = ga1.getAnswer();
//alert(result_start);
if(result_start){
reloadWindow(window);
GlideList2.get(g_form.getTableName() + '.change_task.change_request').setFilterAndRefresh('');
}
}
else { return false;}
}
else { return false;}
}
}
Now it works like a charm.
Thanks,
Jagarnath

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 05:16 AM
Hi Jagarmath,
My recommendation would be to use a business rule for this since it is happening on Submit anyway. Much shorter and it will refresh the related lists.
An AFTER business rule would be best.
Condition: State | changes to | Closed skipped OR
State | changes to | Closed failed
Script (check Advanced):
var task = new GlideRecord('change_task');
task.addQuery('change_request', current.sys_id);
task.query();
while (task.next()) {
task.state = current.state;
task.update();
}
Business Rules - ServiceNow Wiki
Business Rules Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 05:46 AM
Hi Chuck,
I am going to trigger pop up for the user confirmation , That's the reason i am doing it via onsubmit client script.
Thanks,
Jagarnath

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 05:52 AM
Perfect! Do the record count the way you are and if they say "No/Cancel" on that confirm box, just return false.
Then use a business rule to close the tasks if the state changes to 7 or 4.
Best of both worlds. Let the business rule do what it does best - react to database changes.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 06:07 AM
Hi Chuck,
Everything works for me, but my form does not get refreshed . Everytime i need to update manually.
I used the below code in glideajax response function , but it does not work either.
- reloadWindow(window);
- GlideList2.get(g_form.getTableName() + '.change_task.change_request').setFilterAndRefresh('');
Thanks,
Jagarnath