Encoded Query string for company and child companies

Sam Ogden
Tera Guru

Hi All,

I have the below query for a simple list widget filter on the Service Portal to show all incidents that have been raised by the users company.   However I need this list to also show any incidents that have been created by a child company if the logged in users company is a parent company, how can I achieve this?

company=javascript:gs.getUser().getCompanyID()^active=true^EQ

Any help is greatly appreciated.

1 ACCEPTED SOLUTION

Hi Sam,



I am able to retrieve the result in background script. Is the script include is in some scoped application ?


gs.print(new CDL_getParentChild().getCompanyCDL(gs.getUserID()))


find_real_file.png


Give a try with small change in the SI... it should work without parameter as well.


Filter:


companyINjavascript:newCDL_getParentChild().getCompanyCDL()^active=true^EQ^ORDERBYDESCopened_at



Script Include:


var CDL_getParentChild = Class.create();


CDL_getParentChild.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCompanyCDL : function(user) {


  if(!user){


  user=gs.getUserID();


  }


  var sysid=[];


  var company,com;


  var usr = new GlideRecord('sys_user');


  usr.addQuery('sys_id',user);


  usr.query();


  if(usr.next())


    {


    company = usr.company;


    com = usr.email;


  }


  if( company != '' ) {


    if(com.indexOf('@cdl.co.uk')>= 0)


      {


      var compani = new GlideRecord('core_company');


      compani.query();


      while(compani.next()) {


        sysid.push(compani.sys_id.toString());


      }


      return sysid;


    }  


    var company1 = new GlideRecord('core_company');


    company1.addQuery('parent',company);


    company1.query();


    while(company1.next()) {


      sysid.push(company1.sys_id.toString());


    }


    sysid.push(company.sys_id.toString());


    return sysid.join(',');


  } else {


    var company2 = new GlideRecord('core_company');


    company2.addEncodedQuery('vendor=false^customer=false^NQcustomer=true');


    company2.query();


    while(company2.next()) {


      sysid.push(company2.sys_id.toString());


    }


    return sysid.join(',');


  }


},


type:'CDL_getParentChild'


});


View solution in original post

23 REPLIES 23

Gurpreet07
Mega Sage

Hi Sam,



Now you filter is working fine but if you are not getting proper results then its something to do with the logic in script include. Add some log statements before each return statement and then check logs.


gs.log('Test Return string company ids : '+sysid);



You can also check the return value by calling the function in background script.


gs.print(new CDL_getParentChild().getCompanyCDL(gs.getUserID()));


For above script include, your filter string should be like below.


companyINjavascript:new CDL_getParentChild().getCompanyCDL(gs.getUserID())^active=true^EQ^ORDERBYDESCopened_at



According to above script include if your email contains "@cdl.co.uk'" then you are returning a different list of companies.


Hi Gurpreet,



I've just tried your filter below however I get no results:


companyINjavascript:new CDL_getParentChild().getCompanyCDL(gs.getUserID())^active=true^EQ^ORDERBYDESCopened_at



I'd set my filter as below where I am getting some results, but not results that I am expecting:



companyINjavascript:new CDL_getParentChild().getCompanyCDL()^active=true^EQ^ORDERBYDESCopened_at



Just pasted the below into background script but I'm getting the below error:


gs.print(new CDL_getParentChild().getCompanyCDL(gs.getUserID()));


find_real_file.png



Thanks for all your help so far and apologies for all the silly questions.


Hi Sam,



I am able to retrieve the result in background script. Is the script include is in some scoped application ?


gs.print(new CDL_getParentChild().getCompanyCDL(gs.getUserID()))


find_real_file.png


Give a try with small change in the SI... it should work without parameter as well.


Filter:


companyINjavascript:newCDL_getParentChild().getCompanyCDL()^active=true^EQ^ORDERBYDESCopened_at



Script Include:


var CDL_getParentChild = Class.create();


CDL_getParentChild.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCompanyCDL : function(user) {


  if(!user){


  user=gs.getUserID();


  }


  var sysid=[];


  var company,com;


  var usr = new GlideRecord('sys_user');


  usr.addQuery('sys_id',user);


  usr.query();


  if(usr.next())


    {


    company = usr.company;


    com = usr.email;


  }


  if( company != '' ) {


    if(com.indexOf('@cdl.co.uk')>= 0)


      {


      var compani = new GlideRecord('core_company');


      compani.query();


      while(compani.next()) {


        sysid.push(compani.sys_id.toString());


      }


      return sysid;


    }  


    var company1 = new GlideRecord('core_company');


    company1.addQuery('parent',company);


    company1.query();


    while(company1.next()) {


      sysid.push(company1.sys_id.toString());


    }


    sysid.push(company.sys_id.toString());


    return sysid.join(',');


  } else {


    var company2 = new GlideRecord('core_company');


    company2.addEncodedQuery('vendor=false^customer=false^NQcustomer=true');


    company2.query();


    while(company2.next()) {


      sysid.push(company2.sys_id.toString());


    }


    return sysid.join(',');


  }


},


type:'CDL_getParentChild'


});


Hi Gurpreet,



That seems to be working for me now.



Thanks for all your help on this, its really appreciated.