Business Rule: gr.next returns false but works fine in 'Scripts Background'

andy_dufresne
Tera Expert

Hello Gurus,

There is no other business rule that is interfering with it.

When I debugged it the gr.next() was returning false

(function executeRule(current, previous /*null when async*/) {

id=current.sys_id;

var gr = new GlideRecord('contact');

//var eq = 'document='+ id +'^type=sys_user';

var eq = "type=sys_user^document="+ id;

gr.addEncodedQuery(eq);

gr.query();

while (gr.next()){

current.u_mojusersemailaddressholder += gr.user.email +',';

//gs.print(gr.user.email);

}

})(current, previous);

If I run in scripts background, gr.next is true.

What am I doring wrong?

Thanks,

12 REPLIES 12

Chuck Tomasi
Tera Patron

Hi Andy,



A couple tweaks to try (in bold.)



(function executeRule(current, previous /*null when async*/) {


        var id=current.getValue('sys_id');


        var gr = new GlideRecord('contact');


        //var eq = 'document='+ id +'^type=sys_user';


        gr.addQuery('document', id);


        gr.addQuery('type', 'sys_user');


        gr.query();


            gs.log(gr.getRowCount() + ' records found');



        while (gr.next()) {


                  current.u_mojusersemailaddressholder += gr.user.email +',';


                  //gs.print(gr.user.email);


        }


})(current, previous);


Thanks for quick response, Chuck.   the gs.log returned 0 records found.


What happens when you use gs.log() to output the value of "id"?



Another option, to validate the query... go to contact.list (the list of contact records) and build yourself a query where document is a specific value and type is sys_user. Copy the Query from the breadcrumbs and paste it here.



If any of that didn't make sense, refer to this:


Video: Scripting Complex Queries Quickly


If I have the following in business rule:


(function executeRule(current, previous /*null when async*/) {



var id=current.getValue('sys_id');


      var gr = new GlideRecord('contact');


      //var eq = 'document='+ id +'^type=sys_user';


      gr.addQuery('document', id);


      gr.addQuery('type', 'sys_user');


      gr.query();



gs.log('getRowCount is : ' + gr.getRowCount());


gs.log('the id value is:' + id);


gs.log('the gr.next is :' + gr.next());




while (gr.next()){


current.u_mojusersemailaddressholder += gr.user.email +',';


}



//}



})(current, previous);


the log:


find_real_file.png


The same thing with slight modification gives the following in the scripts background 😞



id='48b787efdbcc03406a53ffa9bf9619c0';



var gr = new GlideRecord('contact');


        //var eq = 'document='+ id +'^type=sys_user';


        gr.addQuery('document', id);


        gr.addQuery('type', 'sys_user');


        gr.query();




        gs.print('getRowCount is : ' + gr.getRowCount());


        gs.print('the id value is:' + id);


        gs.print('the gr.next is :' + gr.next());




        while (gr.next()){


          gs.print(gr.user.email);


        }



Gives this ...


find_real_file.png