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

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Your GlideRecord wil only give one row as you are checking on the sys_id ,

Try this

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
var totalAcc = 0;
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()) {
totalAcc =totalAcc  +1;
//Get Row Count here will always give 1 opnly as yoyur current glide Record gets 1 row only. so you need to keep adding these
                gs.info('total Accounts:' + totalAcc);
                gs.info('total Accounts:' + gr.name);
 
}
}
},

 

-Anurag

Robbie
Kilo Patron
Kilo Patron

Hi @Ankur20,

 

What number are you trying to print out here? The number of 'accounts' or to be more accurate - number of sys id's that are passed into the Script Include (Eg 2 using your example), OR, the total number of customer accounts?

 

Either way, your code will always provide 1 as your are specifically only querying on one via each iteration of the for loop.

Comment out the appropriate code line in the below snippet to resolve your issue.

 

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

 

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

        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++;
                gs.info('total Accounts:' + accountCounter);// Using the accountCounter
                gs.info('total Accounts:' + accountsGR.getRowCount());// Using the total of records in the customer_account table - accountsGR variable
                gs.info('total Accounts:' + gr.name);
 
}
}
},

Community Alums
Not applicable

@Anurag Tripathi Actually I want to get the name of the accounts of all the accounts selected 

 gs.info('total Accounts:' + gr.name); --> This is just giving 1 account

Use this

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
var totalAcc = 0;
var accountNames = [];
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()) {
totalAcc =totalAcc  +1;
//Get Row Count here will always give 1 opnly as yoyur current glide Record gets 1 row only. so you need to keep adding these
accountNames.push(''+gr.name);
                gs.info('total Accounts:' + totalAcc);
                gs.info('total Accounts:' + gr.name);
 
}
}
//accountNames  this array here will have the Account ames. You can loop through them or print it using toString()
},
-Anurag