'Invalid Return'

Josh80
Tera Expert

Hello all

I'm trying to learn scripting...the script below will be used in an approval workflow; the purpose is to look up a custom table/record 'u_customer' and look to see if

a checkbox is marked true.   If so, return 'true', build approval.   What I have below is just resulting in an error with 'invalid return'.

I see where others have solved similar errors by 'wrapping it in a function'.   I'm not sure how to do that here.

Any suggestions?

Thanks

var grM2M = new GlideRecord('u_m2m_change_requests_customer');

grM2M.addQuery('u_change_request', current.sys_id.toString());

grM2M.query();

while(grM2M.next())

{

      var grCustomer = new GlideRecord('u_customer');

      if(grCustomer.get(grM2M.u_customer.toString()))

      {

             

  if(grCustomer.u_exec_approval == true){

                 

  return;

  }

}

}

1 ACCEPTED SOLUTION

Thanks Pradeep.



I got it to work with the following:



answer = [];




var grM2M = new GlideRecord('u_m2m_change_requests_customer');


grM2M.addQuery('u_change_request', current.sys_id.toString());


grM2M.query();




while(grM2M.next())


{


      var grCustomer = new GlideRecord('u_customer');


      if(grCustomer.get(grM2M.u_customer.toString()))


        {


           


  if(grCustomer.u_exec_approval == true){


 



  answer.push('f1a48da94f9d8200f90f01b28110c7e7');





  }


}


}


View solution in original post

6 REPLIES 6

Thanks Pradeep.



I got it to work with the following:



answer = [];




var grM2M = new GlideRecord('u_m2m_change_requests_customer');


grM2M.addQuery('u_change_request', current.sys_id.toString());


grM2M.query();




while(grM2M.next())


{


      var grCustomer = new GlideRecord('u_customer');


      if(grCustomer.get(grM2M.u_customer.toString()))


        {


           


  if(grCustomer.u_exec_approval == true){


 



  answer.push('f1a48da94f9d8200f90f01b28110c7e7');





  }


}


}


ben_greer
ServiceNow Employee
ServiceNow Employee

that's just a basic javascript problem, you can't return unless your inside a function. An example with your code might look like:



var answer = requiresExecApproval() ? [ 'f1a48da94f9d8200f90f01b28110c7e7' ] : [];



function requiresExecApproval() {


  var grM2M = new GlideRecord('u_m2m_change_requests_customer');


  grM2M.addQuery('u_change_request', current.sys_id.toString());


  grM2M.query();




  while(grM2M.next()) {


          var grCustomer = new GlideRecord('u_customer');


          if(grCustomer.get(grM2M.u_customer.toString())) {


              if(grCustomer.u_exec_approval)


                  return true;


      }


  }


return false;


}






the advantage of directly returning inside a while loop is that you can short circuit the loop and skip unneeded iterations.