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,



The filter string seems to be fine however the returned query may not work in here. In current scenario, the encoded query will be something like below...


companyINsys_idINcom1,com2,com3                                                               (value returned from include in bold)


But it should be like below


companyINcom1,com2,com3                                                                                        


Hi Gurpreet,



What do I need to do to the script includes in order to get the right vale returned?   Below is a different script includes that the implementation partner created so it will only return parent and child companies if the user is not from CDL and if the user is from CDL then return all companies - realised this one will be better to use, just need to know what I need to amend in order to return the value as expected for the encoded query.



getCompCDL : function(user) {


  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 'sys_idIN'+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 'sys_idIN'+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 'sys_idIN'+sysid.join(',');


  }


},


Hi Sam,



Simply change the return statements(2 occurrences) in above script include.


return 'sys_idIN'+sysid;       -->       return sysid;


return 'sys_idIN'+sysid.join(',');         -->           return sysid.join(',');


Seems to be more that 2 occurrences of return statements, Simply remove sys_idIN suffix at each place.


Hi Gurpreet,



I've amended my script includes to the below.   I've added it into the filter and it is filtering the results, but not the results I am expecting (I've changed my users company to Boxeo, and can see that they are the parent of the companies in the below screenshot), but in my filtered results I'm getting other companies logs displaying, but not all incident logs).   How do I go about finding out what results are being returned from the script so I can try and work out where I've gone wrong.



find_real_file.png


find_real_file.png


var CDL_getParentChild = Class.create();


CDL_getParentChild.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCompanyCDL : function(user) {


  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'


});