Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script include issue

Rosy14
Kilo Sage

The code is not working. I have to list collector type variable. want to show records based on other.

 

 

var ASDAgetSupplierAccount = Class.create();
ASDAgetSupplierAccount.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    /***
     * This is called in reference qualifiers
     * in one variable(Select Buying Agent User) and a field(Select Buying Agent User).
     * This function retrieves list of contact users
     * based on the list of accounts selected
     */
    getContactUsers: function(companyName) {

        if (companyName == '') {
            return;
        }

        var companyString = companyName.toString();

        var flagCasa = 0, flagLi = 0;
        var contacts = [];
        if(companyString.indexOf('casa') >=0 || companyString.indexOf('CASA') >=0 || companyString.indexOf('Casa') >=0)
          flagCasa = 1;
        if(companyString.indexOf('li & fung')>=0 || companyString.indexOf('Li & Fung')>=0 || companyString.indexOf('LI & FUNG')>=0)
            flagLi = 1;
		
        if(flagCasa) {

            var contactRec = new GlideRecord('account_relationship');
            contactRec.addEncodedQuery("to_companyLIKEcasa^ORto_companyLIKECasa^ORto_companyLIKECASA");
            contactRec.query();
            while (contactRec.next()) {
                contacts.push(contactRec.getValue('sys_id'));
            }
        }
		
		if(flagLi) {

            contactRec = new GlideRecord('account_relationship');
            contactRec.addEncodedQuery("to_companyLIKEli & fung^ORto_companyLIKELi & Fung^ORto_companyLIKELI & FUNG");
            contactRec.query();
            while (contactRec.next()) {
                contacts.push(contactRec.getValue('sys_id'));
            }
        }

		
        return 'sys_idIN' + contacts.join(',');
    },

    type: 'ASDAgetSupplierAccount'
});

 

 

4 REPLIES 4

Shubham_Jain
Mega Sage
Mega Sage

@Rosy14 

 

var ASDAgetSupplierAccount = Class.create();
ASDAgetSupplierAccount.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

/***
* This is called in reference qualifiers
* in one variable(Select Buying Agent User) and a field(Select Buying Agent User).
* This function retrieves a list of contact users
* based on the list of accounts selected
*/
getContactUsers: function(companyName) {

if (!companyName) {
return ''; // Return empty if no company is provided
}

var companyString = companyName.toString().toLowerCase(); // Lowercase for easier comparison
var contacts = [];

// Query for "casa" companies
if (companyString.indexOf('casa') >= 0) {
var casaRec = new GlideRecord('account_relationship');
casaRec.addEncodedQuery("to_companyLIKEcasa"); // Simplified query
casaRec.query();
while (casaRec.next()) {
contacts.push(casaRec.getValue('sys_id')); // Push sys_ids
}
}

// Query for "li & fung" companies
if (companyString.indexOf('li & fung') >= 0) {
var liRec = new GlideRecord('account_relationship');
liRec.addEncodedQuery("to_companyLIKEli & fung");
liRec.query();
while (liRec.next()) {
contacts.push(liRec.getValue('sys_id'));
}
}

// If contacts were found, return the sys_id query, otherwise return an empty result
return contacts.length > 0 ? 'sys_idIN' + contacts.join(',') : '';
},

type: 'ASDAgetSupplierAccount'
});

 

 

✔️ If this solves your issue, please mark it as Correct.


✔️ If you found it helpful, please mark it as Helpful.



Shubham Jain


It is not filtering the records

Aniket Chavan
Tera Sage
Tera Sage

Hello @Rosy14 ,

Please give a try to the updated script below, and let me know how it works for you.

var ASDAgetSupplierAccount = Class.create();
ASDAgetSupplierAccount.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    getContactUsers: function(companyName) {
        // Ensure companyName is not empty
        if (!companyName) {
            return '';
        }

        var companyString = companyName.toString().toLowerCase(); // Normalize to lowercase
        var contacts = [];
        var encodedQuery = '';

        // Determine if company is 'casa' or 'li & fung'
        if (companyString.indexOf('casa') >= 0) {
            encodedQuery = "to_companyLIKEcasa";
        }
        if (companyString.indexOf('li & fung') >= 0) {
            if (encodedQuery) {
                encodedQuery += "^OR";
            }
            encodedQuery += "to_companyLIKEli & fung";
        }

        // Perform the query if there is a valid company filter
        if (encodedQuery) {
            var contactRec = new GlideRecord('account_relationship');
            contactRec.addEncodedQuery(encodedQuery);
            contactRec.query();
            while (contactRec.next()) {
                contacts.push(contactRec.getValue('sys_id'));
            }
        }

        // Return the sys_id of contacts as a reference qualifier query
        if (contacts.length > 0) {
            return 'sys_idIN' + contacts.join(',');
        } else {
            return ''; // Return empty string if no contacts found
        }
    },

    type: 'ASDAgetSupplierAccount'
});

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.


Regards,
Aniket

not working still