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

Mine does...I figured someone would notice that though.  


Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Think this line is wrong in the script include:



  countChilds.addQuery('u_parent_incident',taskID);



My guess you are using the OOB field named parent_incident and not u_parent_incident.



Since that field doesnt exist, it ignores that line and the only addQuery you have is active is true. Which I guess makes that it will always at least find 1 record and return true.


If you only want active records, there is even a OOB query for that like this: countChilds.addActiveQuery();




I would also use the g_form.getUniqueValue() as mention before when setting the sys_id.



//Göran