Not able to get exact rowcount.

Community Alums
Not applicable
accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.
        for (var i = 0; i < accounts.length; i++) {

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
            if (gr.next()) {
                gs.info('total Accounts:' + gr.getRowCount());// But there I am just getting 1 record even if we have more than 1
                gs.info('total Accounts:' + gr.name);
 
}
}
},
 
 
 var accounts = this.getParameter('sysparm_values');  --> Here I am getting correct number of sys_ids  but when we do getRowCount()  we get only 1 always.
2 ACCEPTED SOLUTIONS

Robbie
Kilo Patron
Kilo Patron

Hi @Community Alums,

 

Based on your latest comment, use the below script:

 

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
        gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.

        //Querying the customer_account table to get the full list of accounts
        var accountsGR = new GlideRecord('customer_account');
        accountsGR.query();
        //End of customer account query

        //Using a counter based on the passed values
        var accountCounter = 0;
        //End of counter

        var accountNames = [];

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

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
                if (gr.next()) {
                    accountCounter++;
                    accountNames.push(''+gr.name);
               }
        }
        gs.info('total Accounts (From customer_account table):' + accountCounter);// Using the accountCounter
        gs.info('total Accounts (Passed via variable and script):' + accountsGR.getRowCount());// BUsing the total of records in the customer_account table - accountsGR variable
        gs.info('total Account name:' + accountNames.toString());
},

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

View solution in original post

Hi @Community Alums,

 

I'm a little confused - use my latest script which provides what you need. 

The 'Company are' and 'count of Company' need to be outside of the for loop.

 

Here's the script you need as well as a test script you can use in a Background - Script for testing to ensure it provides what you need and expect:

 

Testing Background Script. Type 'Scripts - Background' into the navigation menu. This is a good place to play and check scripts (In non prod of course)

 

//BACKGROUND SCRIPT TESTING
//accountAdd: function() {
        var arr = [];
        //var accounts = this.getParameter('sysparm_values'); // Commented for use in Background Script
        var accounts = ['8e6c108413651200042ab3173244b034', '39201da4d7700200e5982cf65e61039f']; //Hard code known sys_id here to check the script is 
        gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.

        //Querying the customer_account table to get the full list of accounts
        var accountsGR = new GlideRecord('customer_account');
        accountsGR.query();
        //End of customer account query

        //Using a counter based on the passed values
        var accountCounter = 0;
        //End of counter

        var accountNames = [];

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

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
                if (gr.next()) {
                    accountCounter++;
                    accountNames.push(''+gr.name);
               }
        }
        gs.info('total Accounts (From customer_account table):' + accountCounter);// Using the accountCounter
        gs.info('total Accounts (Passed via variable and script):' + accountsGR.getRowCount());// BUsing the total of records in the customer_account table - accountsGR variable
        gs.info('total Account name:' + accountNames.toString());
//},

 

Here the final script to use within your script include:

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values'); // Commented for use in Background Script
        //var accounts = ['8e6c108413651200042ab3173244b034', '39201da4d7700200e5982cf65e61039f']; //Hard code known sys_id here to check the script is 
        gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.

        //Querying the customer_account table to get the full list of accounts
        var accountsGR = new GlideRecord('customer_account');
        accountsGR.query();
        //End of customer account query

        //Using a counter based on the passed values
        var accountCounter = 0;
        //End of counter

        var accountNames = [];

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

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
                if (gr.next()) {
                    accountCounter++;
                    accountNames.push(''+gr.name);
               }
        }
        gs.info('total Accounts (From customer_account table):' + accountCounter);// Using the accountCounter
        gs.info('total Accounts (Passed via variable and script):' + accountsGR.getRowCount());// BUsing the total of records in the customer_account table - accountsGR variable
        gs.info('total Account name:' + accountNames.toString());
},

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

View solution in original post

8 REPLIES 8

Community Alums
Not applicable

Hi @Community Alums ,

Try with while once

 while (gr.next()) {
                gs.info('total Accounts:' + gr.getRowCount());
                gs.info('total Accounts:' + gr.name);
}
 
Please mark my answer correct and helpful if this works for you
Thanks and Regards 
Sarthak

Robbie
Kilo Patron
Kilo Patron

Hi @Community Alums,

 

Based on your latest comment, use the below script:

 

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
        gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.

        //Querying the customer_account table to get the full list of accounts
        var accountsGR = new GlideRecord('customer_account');
        accountsGR.query();
        //End of customer account query

        //Using a counter based on the passed values
        var accountCounter = 0;
        //End of counter

        var accountNames = [];

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

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
                if (gr.next()) {
                    accountCounter++;
                    accountNames.push(''+gr.name);
               }
        }
        gs.info('total Accounts (From customer_account table):' + accountCounter);// Using the accountCounter
        gs.info('total Accounts (Passed via variable and script):' + accountsGR.getRowCount());// BUsing the total of records in the customer_account table - accountsGR variable
        gs.info('total Account name:' + accountNames.toString());
},

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie

Community Alums
Not applicable

@Robbie I am still getting 1 row count.

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
         var acc = accounts.split(',');
        var totalAcc = 0;
        gs.info('Accounts:' + acc);// Here I am getting 2 sys_ids therefore below I should get 2 records but its iterating just once. even after using the counter
        for (var i = 0; i < acc.length; i++) {

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', acc[i]);
            gr.query();
            gs.info("Total count is:" + gr.getRowCount());// only 1 count is coming
            if (gr.next()) {
                // gs.info("I am twice");
                totalAcc = totalAcc + 1;
                arr.push('' + gr.name);
                gs.info('Company are:' + arr.toString());// Only 1 Account name is getting printed
                gs.info('count of Company are:' + totalAcc); //Only 1 Count is coming
 }
 }
},

Hi @Community Alums,

 

I'm a little confused - use my latest script which provides what you need. 

The 'Company are' and 'count of Company' need to be outside of the for loop.

 

Here's the script you need as well as a test script you can use in a Background - Script for testing to ensure it provides what you need and expect:

 

Testing Background Script. Type 'Scripts - Background' into the navigation menu. This is a good place to play and check scripts (In non prod of course)

 

//BACKGROUND SCRIPT TESTING
//accountAdd: function() {
        var arr = [];
        //var accounts = this.getParameter('sysparm_values'); // Commented for use in Background Script
        var accounts = ['8e6c108413651200042ab3173244b034', '39201da4d7700200e5982cf65e61039f']; //Hard code known sys_id here to check the script is 
        gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.

        //Querying the customer_account table to get the full list of accounts
        var accountsGR = new GlideRecord('customer_account');
        accountsGR.query();
        //End of customer account query

        //Using a counter based on the passed values
        var accountCounter = 0;
        //End of counter

        var accountNames = [];

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

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
                if (gr.next()) {
                    accountCounter++;
                    accountNames.push(''+gr.name);
               }
        }
        gs.info('total Accounts (From customer_account table):' + accountCounter);// Using the accountCounter
        gs.info('total Accounts (Passed via variable and script):' + accountsGR.getRowCount());// BUsing the total of records in the customer_account table - accountsGR variable
        gs.info('total Account name:' + accountNames.toString());
//},

 

Here the final script to use within your script include:

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values'); // Commented for use in Background Script
        //var accounts = ['8e6c108413651200042ab3173244b034', '39201da4d7700200e5982cf65e61039f']; //Hard code known sys_id here to check the script is 
        gs.info('Accounts:'+accounts);// Here I am getting the proper number of sys_ids. If we select 2 accounts we get 2 and so on.

        //Querying the customer_account table to get the full list of accounts
        var accountsGR = new GlideRecord('customer_account');
        accountsGR.query();
        //End of customer account query

        //Using a counter based on the passed values
        var accountCounter = 0;
        //End of counter

        var accountNames = [];

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

            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', accounts[i]);
            gr.query();
           
                if (gr.next()) {
                    accountCounter++;
                    accountNames.push(''+gr.name);
               }
        }
        gs.info('total Accounts (From customer_account table):' + accountCounter);// Using the accountCounter
        gs.info('total Accounts (Passed via variable and script):' + accountsGR.getRowCount());// BUsing the total of records in the customer_account table - accountsGR variable
        gs.info('total Account name:' + accountNames.toString());
},

 

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.

 

Thanks, Robbie