Script Include

Rosy14
Tera Guru

Hi,

I want to filter a ref field based on another field(Ref). If that field.name contains XXXX, then the 2nd field will show all records where field.comany.name contains XXXX. Below returning all records

function asda_getContactBasedOnAccount() {

    var ContactArray = [];
    var acc = current.select_buying_agent_account.name;
    var gr = new GlideRecord('customer_contact');
    if(acc.toString().indexOf("CASA") >= 0)
    {
        gr.addEncodedQuery("company.nameCONTAINSCASA");
       
    }else{
     gr.addEncodedQuery("company.nameLIKELi & Fung");
    }
   
    gr.query();
    while (gr.next()) {
        ContactArray.push(gr.sys_id.toString());
    }
    return ContactArray;

}
8 REPLIES 8

Anand Kumar P
Giga Patron
Giga Patron

Hi @Rosy14 ,

CONTAIONS AND LIKE are incorrect in above script

 if (acc.indexOf("CASA") >= 0) {
        gr.addQuery("company.name", "CONTAINS", "CASA");
    } else {
        gr.addQuery("company.name", "LIKE", "Li & Fung");
    }

Mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand

Tried this one. not working as well.

Hi @Rosy14 ,

Use below script

function asda_getContactBasedOnAccount() {
var ContactArray = [];
var acc = current.select_buying_agent_account.name;
var gr = new GlideRecord('customer_contact');

if (acc.indexOf("CASA") >= 0) {
gr.addEncodedQuery("company.nameLIKE*CASA*");
} else {
gr.addEncodedQuery("company.nameLIKELi & Fung");
}

gr.query();

while (gr.next()) {
ContactArray.push(gr.sys_id.toString());
}

return ContactArray;
}

 

Thanks,

Anand

Kristen Ankeny
Kilo Sage

Rather than using gliderecord and returning an array of sys_ids, it is better to just return the encoded query you want to apply to the reference field. This prevents the system from hitting the database twice. Also, double check your encoded queries by filtering a list view and using the copy query function in the query breadcrumb.