- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2024 07:25 AM
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2024 08:50 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2024 04:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2024 07:30 AM - edited 05-09-2024 07:30 AM
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);
}
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2024 07:50 AM - edited 05-09-2024 07:52 AM
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); } } },
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2024 08:21 AM
@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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-09-2024 08:25 AM
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()
},