Business Rule: gr.next returns false but works fine in 'Scripts Background'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 06:22 AM
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,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 06:26 AM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 06:35 AM
Thanks for quick response, Chuck. the gs.log returned 0 records found.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 06:39 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-25-2017 06:59 AM
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:
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 ...