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

suvro
Mega Sage
Mega Sage

current contains only 1 record that is the current record. If you want to loop use GlideRecord use appropriate queries to filter the records you need then run a loop in it.

https://servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

http://www.servicehow.com/gliderecord-cheat-sheet

Hello Suvro,

Thank You for the reply.

So do I just create a glide record for the "current" table? 

Yes table 1 

kamlesh kjmar
Mega Sage
Mega Sage

Hi @MichalKeluc ,

 

There are couple of issues I can see in your script:

 

1. addEncodedQuery() is a function of GlideRecord() object which can't be used with current, use it with the variable that you have used with GlideRecord() object.

 

2. What is the name of your variable which you are using to store GlideRecord to the other table.

 

3. You are using .next() function with current object, this is not right. .next() function should be used with your gliderecord object and not current.

 

4. In your while() loop you have used _next() which is not a right function, next is used without any _

 

If you rectify above mentioned issues in your script, you should be able to make your code cowk

 

I hope this helps.

 

Regards,

Kamlesh