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.

Script Include always returns 'true' even when obviously false

Shane J
Tera Guru

I'm trying to setup an onSubmit Client Script that checks if an Incident has any children records (I don't want the OOB BR that auto-resolves children Incidents to run without some kind of verification).   No matter what I do, my return value is true and I can't determine why:

Here is my Script Include:

var ThisParent = Class.create();

ThisParent.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  checkIncidentChilds: function() {

  var taskID = this.getParameter('sysparm_sys_id');

  var countChilds = new GlideRecord('incident');

  countChilds.addQuery('u_parent_incident',taskID);

  countChilds.addQuery('active', true);

  countChilds.setLimit(1);

  countChilds.query();

  if (countChilds.hasNext()){ //there is at least one record

  return true;

  }

  else {

  return false;

  }

},

type: 'ThisParent'

});

and my Client Script:

function onSubmit() {

    var ga = new GlideAjax('ThisParent');

  ga.addParam('sysparm_name', 'checkIncidentChilds');

  ga.addParam('sysparm_sys_id', g_form.getValue('sys_id'));

  ga.getXMLWait();

  alert(ga.getAnswer());

}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,



here is the updated script: getUniqueValue() method



function onSubmit() {


var ga = new GlideAjax('ThisParent');


ga.addParam('sysparm_name', 'checkIncidentChilds');


ga.addParam('sysparm_sys_id', g_form.getUniqueValue());


var answer =   ga.getXMLWait();



if(answer){


// do nothing


return true;


}


return false;


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

11 REPLIES 11

BALAJI40
Mega Sage

change this line and check,


  ga.addParam('sysparm_sys_id', g_form.getValue('sys_id')); to   ga.addParam('sysparm_sys_id',g_form.getUniqueValue());// it returns the sys_id


Ankur Bawiskar
Tera Patron
Tera Patron

Hi,



Have a check in onSubmit client script and update your client script:



function onSubmit() {


var ga = new GlideAjax('ThisParent');


  ga.addParam('sysparm_name', 'checkIncidentChilds');


  ga.addParam('sysparm_sys_id', g_form.getValue('sys_id'));


var answer =   ga.getXMLWait();



if(answer){


// do nothing


return true;


}


return false;


}



This will stop form submission if answer returning is false.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,



here is the updated script: getUniqueValue() method



function onSubmit() {


var ga = new GlideAjax('ThisParent');


ga.addParam('sysparm_name', 'checkIncidentChilds');


ga.addParam('sysparm_sys_id', g_form.getUniqueValue());


var answer =   ga.getXMLWait();



if(answer){


// do nothing


return true;


}


return false;


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I want to force a confirmation message if the answer is 'true' and update the Incident if they hit OK, and if they cancel, it still saves the record with whatever other updates they made.



Nothing should happen when it's false (because the Incident has no children).