while(current.next())

MichalKeluc
Tera Contributor

Good day all,

 

I'm currently struggeling with a while cycle, which we need to run trough whole table.

Basically I'm creating a business rule on table 1, and I need it to update table 2. 

The tables are connected trough account name.

 

We managed to fixed almost every other element of the script, but I'm stuck with empty array, because we have it in 

while(current._next()){

var test = current.getDisplayValue('account').toString();
gs.info('MK3_' + test);
arr.push(current.getDisplayValue('account').toString());
}

 

It worked in background script but that was because it wasn't set as "current" but I had a glide record created. 

I was told that I should not create a glide record for a table I'm already working in. 

So my question would be, is there some workaround for this? 

The scenario is that there are 19 records assigned to an account name and 2 of them get filtered by this:

 

var userAssigned = current.getDisplayValue('account').toString();

current.addEncodedQuery('account='+ userAssigned + '^short_descriptionLIKESIMS connected' + '^active=true');

 

We need to go trough the filtered list of records in current table.

 

Thank You in advance for Your responds.

 

Michal Keluc

 

2 ACCEPTED SOLUTIONS

Tried to rectify errors that I saw in your script, try using this :

 

(function executeRule(current, previous /*null when async*/) {
var userAssigned = current.getDisplayValue('account').toString();
var cnt= new GlideRecord('ast_contract');
cnt.addEncodedQuery('account='+ userAssigned + '^short_descriptionLIKESIMS connected' + '^active=true');
cnt.query();
gs.info('MK_'+ userAssigned);
var arr=[];
while(cnt.next()){
var test = cnt.getDisplayValue('account').toString();
gs.info('MK3_' + test);
arr.push(cnt.getDisplayValue('account').toString());
}

gs.info('MK_' + arr.length);
gs.info('MK_' + arr[0]);

for(var i = 0; i < arr.length; i++){

var gr= new GlideRecord('customer_account');
gr.addEncodedQuery('name='+arr[i]);
gr.query();
while(gr.next()){

gr.setValue('u_sims_connected',true);
gr.update();


}

}

})(current, previous);

 

Regards,

Kamlesh

 

View solution in original post

Hi @MichalKeluc ,

 

If you are to pass the sys_id then don't call for the display value. In your script you are using display value. Using below code you can change it to sys_id (here I am assuming account is a reference field):

 

var userAssigned = current.getValue('account');

 

Regards,

Kamlesh

View solution in original post

10 REPLIES 10

You are most welcome ! I am glad it helped.

 

Regards,

Kamlesh