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

Hi,



Are you saying when answer is false you need not stop form submission. then remove return false line of code


Since you want an alert when answer is true you can add following line of code



if(answer){


// do nothing


alert('some message you want to show');


return true;


}


}



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

if (your_return_value==true)



{



var answer = confirm("Do you want to save. Click OK to Proceed.");



if (answer != true) {
// Dont do anything or return false if you dont want to submit

}


else


{


//Set your values here


}


}



Please mark this response as correct or helpful if it assisted you with your question.

Here's my end result, but there's one more issue I'm not sure how to overcome.   Since this is a Client Script, I can only update fields that are shown on the form correct?   That seems to be the case through testing, and it's so rare that I need to use a CS to modify a hidden field I guess I never noticed it before.   Is there anything I can do to update that field instead of



  g_form.setValue('u_resolve_child_incidents', true);




so I can take the form off the field?



Client Script:



function onSubmit() {


  var ts = g_form.getValue('state');



  var ga = new GlideAjax('ThisParent');


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


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


  ga.getXMLWait();


  var firstanswer = ga.getAnswer();


// alert(firstanswer);



  if(firstanswer == 'true'){


  if(ts == 6){


  var answer = confirm("Do you want to resolve the child Incidents as well?. Click OK if you do, otherwise Cancel to continue.");



  if (answer != true) {


  // Dont do anything or return false if you dont want to submit


  }


  else


  {


  g_form.setValue('u_resolve_child_incidents', true);


  }



  return true;


  }


  // return false;


  }


}


SanjivMeher
Mega Patron
Mega Patron

Put some debugging statemenets, and check your system logs.



var ThisParent = Class.create();


ThisParent.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  checkIncidentChilds: function() {



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


gs.log('=============taskID '+taskID);


  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


gs.log('=============In True'');



  return true;


  }


  else {


  return false;


gs.log('=============In False'');


  }


},




type: 'ThisParent'


});



Please mark this response as correct or helpful if it assisted you with your question.

juanfourie
Tera Contributor

In your add query, use parent_incident instead of u_parent_incident, since this is not a custom table, the fields don't have the custom u_ prefix to it.