- 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 06:10 AM
Just so I am clear... you are doing this from an onSubmit() client script.
Q: Where is the client script bring triggered from? (Which UI action?) Save, Update, one of your custom UI actions?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 06:15 AM
Save
Thanks,
Jagarnath

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2016 06:23 AM
Thanks. Here's what I'd do with the client script...
Warning: Untested code!!!
I changed it to just check for records, and wait for the response. Confirm/alert seem to work better (IMO) with getXMLWait().
If they say no, you abort the submit. If they say yes, Save does what save is going to do and the business rule does what the business rule is going to do (detect the change state changed and react to it by closing the child tasks.)
The system will refresh the form, go back to the list, or whatever the UI action tells it to do.
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 response = ga.getAnswer();
var answer=response.responseXML.documentElement.getAttribute("answer");
if (!confirm("You have open tasks associated with this change.Your tasks will also be closed"))
return false;
}
}
- 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