Getting only one value instead of multiple values from script include

Kumar147
Tera Contributor

Hello,

when i used background script i am getting multiple rows and at the same time when i used script include i am getting only one value instead of multiple values, and the field i used on the form is a list collector. script as follows:

 

var relChg = this.getParameter('sysparm_number');
        var rls = new GlideRecord('table_one');
        rls.addQuery('number', relChg);
        rls.query();
        while (rls.next()) {
            var depChg = rls.getValue('u_dependent_change');
            gs.log('dependent task: ' + depChg);
            var cusChg = new GlideRecord('table_two');
            cusChg.addQuery('sys_id', 'IN', depChg);
            cusChg.query();
            while (cusChg.next()) {
                var chgList = [];
                chgList.push(cusChg.getValue('u_number'));
                gs.log('dependent task1: ' + chgList);
                return chgList.join(',');
            }
        }
 
Kumar147_0-1742936387985.png

 

1 ACCEPTED SOLUTION

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Kumar147 ,

 

The problem is at line return chgList.join(',');

this return statement should be out of while loops. In script execution, once return statement read, blocks are automatically terminated. In your script return statement was written in while loops. So, loop terminated at the first iteration, because it reads return statement.

 

Try the same script with return statement after loops.

 

 

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

6 REPLIES 6

Rohit  Singh
Mega Sage

Hi @Kumar147 ,

 

From the code and snap from sys log it determines that there are four records found in table one. Now out of four table one record (dependent change) there is only one record found in table two. That’s why only one record is viable in the log dependent task1 which you are returning.

 

Pls correct me if I have miss understand your e.g. 

 

If my understanding is correct then where is the issue?

 

Hello @Rohit Singh 

from background script i am getting four records

in a row, but from script include i am able to get only one record.

Hi @Kumar147 

Four records from table one? Screenshot which you have shared is from Sys log table. Those values in sys log are captured when you have executed background script?

 

can you make sure in this line of code  rls.addQuery('number', relChg); you are using same sys_id in both background script and script include for testing?

Hi @Rohit Singh 

I am using same sys_id in both background script and script in include. I got four rows in background script. Log is from script include and background script gives in rows