Not able to get all accounts from script include JSON

Community Alums
Not applicable

Hi Team,

 

Below is my Script Include:

 getAccountList: function() {
        var account = {};
        var accountList = [];
        var rel = new GlideRecord('sn_customerservice_contact_relationship');
        rel.addQuery('sys_id', this.getParameter('sysparm_sys_id'));
        rel.query();
        if (rel.next()) {

            var acc = new GlideRecord('customer_account');
            acc.addEncodedQuery('account_parent.account_pathSTARTSWITH' + rel.company.account_parent.account_path.getDisplayValue());
            acc.query();
            gs.info('Total Account Path Cos:' + acc.getRowCount());
            while (acc.next()) {
       
                account.name = acc.name.getDisplayValue();
                account.sys_id = acc.sys_id.toString();

            }
 
gs.info('Name of All accounts:'+account.name);  
            accountList.push(account);

        }

        return JSON.stringify(accountList);

    },

    type: 'GetAccountsAjax'
});
 
 
In  line gs.info('Total Account Path Cos:' + acc.getRowCount());  I am getting 3 records. But when I do
 gs.info('Name of All accounts:'+account.name);   This is giving me 3 records which is fine. But When I am pushing its not giving 3 records but 1.
2 ACCEPTED SOLUTIONS

Hi @Community Alums  ,

 

I tried similar code in my PDI, which I shared with you, and it is working fine for me. Please have a look at the code and screenshots

 

Code -

 

 

var accountList = [];

var rel = new GlideRecord('sn_customerservice_contact_relationship');
rel.addQuery('sys_id','76d50803910b3410f87731ea9e8493d0');
rel.query();

if (rel.next()) {

  var acc = new GlideRecord('customer_account');
  acc.addEncodedQuery('nameSTARTSWITHBoxeo');
  acc.query();
  gs.info('Total Account Path Cos:' + acc.getRowCount());

  while (acc.next()) {
    var account = {}; // Create a new object for each record
    account.name = acc.name.getDisplayValue();
    account.sys_id = acc.sys_id.toString();
    accountList.push(account); // Add the object to the array
  }

  //gs.info('Name of All accounts:' + accountList[0].name); // This will print the first record's name (optional)
  gs.print( JSON.stringify(accountList));
}

 

 

 

AstikThombare_0-1714377993964.png

 

AstikThombare_1-1714378058491.png

 

   If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik

 

View solution in original post

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Community Alums ,

 

try :

getAccountList: function() {
    var accountList = [];
    var rel = new GlideRecord('sn_customerservice_contact_relationship');
    rel.addQuery('sys_id', this.getParameter('sysparm_sys_id'));
    rel.query();
    if (rel.next()) {
        var acc = new GlideRecord('customer_account');
        acc.addEncodedQuery('account_parent.account_pathSTARTSWITH' + rel.company.account_parent.account_path.getDisplayValue());
        acc.query();
        gs.info('Total Account Path Count:' + acc.getRowCount());
        while (acc.next()) {
            var account = {};
            account.name = acc.name.getDisplayValue();
            account.sys_id = acc.sys_id.toString();
            gs.info('Name of Account: ' + account.name);
            accountList.push(account);
        }
    }
    return JSON.stringify(accountList);
},
type: 'GetAccountsAjax'

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

View solution in original post

6 REPLIES 6

Astik Thombare
Tera Sage

Hi @Community Alums ,

 

Can you try below code -

 

 

getAccountList: function() {
  var accountList = [];
  var rel = new GlideRecord('sn_customerservice_contact_relationship');
  rel.addQuery('sys_id', this.getParameter('sysparm_sys_id'));
  rel.query();
  if (rel.next()) {

    var acc = new GlideRecord('customer_account');
    acc.addEncodedQuery('account_parent.account_pathSTARTSWITH' + rel.company.account_parent.account_path.getDisplayValue());
    acc.query();
    gs.info('Total Account Path Cos:' + acc.getRowCount());

    while (acc.next()) {
      var account = {};  // Create a new object for each account
      account.name = acc.name.getDisplayValue();
      account.sys_id = acc.sys_id.toString();
      accountList.push(account);
    }
  }

  return JSON.stringify(accountList);

},

type: 'GetAccountsAjax'
});

 

 

 

   If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik

Community Alums
Not applicable

@Astik Thombare I am already doing this if you recheck my code , I am passing the value to JSON so there is no difference with what you have sent.

Aman Kumar S
Kilo Patron

Hi @Community Alums 

You need to push  accountList.push(account); statement within while loop.

Since the above statement is kept outside while, its being executed only once, when the while loop ends executing.

Best Regards
Aman Kumar

Community Alums
Not applicable

@Astik Thombare Now its giving me 3 records but all are same not different as it should should show 3 different records