Not able to Fetch Multiple records using GlideRecord at time of checking

Community Alums
Not applicable

Hi Team

 

Below is my script Include function.The requirement is when there is no record in contact Relationship table , this should insert/create the multiple records based on multiple accounts and single contact which it does without any issues.

But when we try to fetch the already existing records for Account , this only returns single record.

 

In other words the Else condition is working fine, its only IF the record is found, it only returns single record and not more.

 

accountAdd: function() {
        var arr = [];
        var accounts = this.getParameter('sysparm_values');
        var contact = this.getParameter('sysparm_contact');
        var role = this.getParameter('sysparm_role');
        var acc = accounts.split(',');
        gs.info('Accounstbefore:' + acc);// This returns equal number of accounts as selected.
        for (var i = 0; i < acc.length; i++) {
            gs.info('Accounst:' + acc[i]);// When the record is created in line  "accrel.insert()"  . This returns proper number of accounts.But if the Contact Relationship is already created.This returns only 1 record.
            var gr = new GlideRecord('customer_account');
            gr.addQuery('sys_id', acc[i]);
            gr.query();

            while (gr.next()) {

                arr.push(gr.sys_id.toString());
                gs.info('Total Account:' + arr.toString());
                var rel = new GlideRecord('sn_customerservice_contact_relationship');
                rel.addQuery('company', arr.toString());
                rel.addQuery('contact', contact);
                rel.query();
                if (rel.next()) {
                    gs.addErrorMessage('Contact Relationship record already exist:' + rel.company.name);// This is just printing 1 account and not more..
                    return rel.company.toString();
 
 
                } else {

                    var accrel = new GlideRecord('sn_customerservice_contact_relationship');
                    accrel.initialize();
                    accrel.company = gr.sys_id.toString();
                    accrel.contact = contact.toString();
                    accrel.u_authority_role = role.toString();
                    accrel.insert();
                    gs.addInfoMessage('Contact Relationship has been created for:' + accrel.company.name);
                }
            }

        }


    },
 
 
Below is the screenshot from Background script. Line no 22 is only returning 1 record
Ankur20_0-1715688562893.png

 

1 REPLY 1

dgarad
Giga Sage

Hi @Community Alums 

 try the below code .

arr.push(gr.sys_id.toString());
                gs.info('Total Account:' + arr.toString());
                var rel = new GlideRecord('sn_customerservice_contact_relationship');
                rel.addQuery('company', arr);
                rel.addQuery('contact', contact);
                rel.query();
while (rel.next()) {
                    gs.addErrorMessage('Contact Relationship record already exist:' + rel.company.name);// This is just printing 1 account and not more..
                    return rel.company.toString();
 
 
                } 

 

If my answer finds you well, helpful, and related to the question asked. Please mark it as correct and helpful.

Thanks
dgarad