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

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar147 

how are you calling it?

share the screenshots etc

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader