How to show users only based on logged in user company

Sriram Pusuluri
Tera Contributor

Hi,

 

I want to show only users in a reference variable where company is child or parent.

 

Example.

Case 1:

I have logged in my company will be BTS, I have another Referance varaible(sys_user) in this variable I want to show all the company users.

 

Case 2: 

I have logged in my company will be AB Mauri, I have another Referance varaible(sys_user) in this variable I want to show only "AB Mauri BeNeFr, AB Mauri Brazil, AB Mauri Etten Leur, AB Mauri India, AB Mauri Italy, AB Mauri Malaysia, AB Mauri Mexico, AB Mauri North America, AB Mauri Pakistan, AB Mauri Philippines, AB Mauri Portugal, AB Mauri Spain, AB Mauri Sri Lanka, AB Mauri Turkey, AB Mauri UAE, AB Mauri Vietnam, Cereform, Mauri Products" company users only.

 

Note: All this companies are child to AB Mauri.

 

Case 3: 

 I have logged in my company will be GSC, I have another Referance varaible(sys_user) in this variable I want to show "AB Nutrition, AWorld, ABakeries, Allied Mills, GG, JR, Speedibake, Westmill company users only.

 

Reference Varaible name: Business Users

9 REPLIES 9

@Ankur Bawiskar 

 

If Company is BTS, then need to show all the company users in referance variable.

 

If Company is GSC, then need to show "AB Nutrition, AWorld, ABakeries, Allied Mills, GG, JR, Speedibake, Westmill" Company users only in this case. 

@Sriram Pusuluri 

so it's based on logged in user's company

then do this

I believe I answered your question and you can now enhance the script based on your requirement, developer skills and experience.

javascript:
var query;
var myCompany = gs.getUser().getCompanyID();
if (myCompany == 'BTSCompanySysId')
    query = ''; // means show all users
else if (myCompany == 'GSCCompanySysId')
    query = 'company.nameINAB Nutrition,AWorld,ABakeries,Allied Mills,GG,JR,Speedibake,Westmill';
else if (myCompany == 'AB MauriCompanySysId')
    query = 'company.parentIN' + myCompany;
query;

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

Reference Qualifier:

I have added below code in reference qualifier

javascript:
var query = '';
var myCompany = gs.getUser().getCompanyID();

var BTS = '97540d483be262904b81a89704e45afd';
var ABF_CORP = '2fbf142e2b962a508a51fef56e91bf0f';


if (myCompany == BTS || myCompany == ABF_CORP) {
    query = '';
}

else {
  
    var familyRoot = myCompany;
    var compGR = new GlideRecord('core_company');

    if (compGR.get(myCompany)) {
        while (compGR.parent) {
            compGR.get(compGR.parent);
        }
        familyRoot = compGR.sys_id.toString();
    }

    query = 'company=' + familyRoot + '^ORcompany.parent=' + familyRoot;
}

query;

 

I have logged in with Twining's user, in business contact variable it need to show only twining's child companies, However it is not working

 

@Sriram Pusuluri 

so what debugging did you do from your side?

Also ensure you use : and not : -> ServiceNow converts that automatically when we post that as comment

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Sriram,

It seems every case are different and few having static values.

In that case, Create a onChange client script on Company.

       In script , call Script Include, based on selected company , add all required user <sys_id> in  array result and pass it to client.

    Set that value in user variable.

Note: do not add hardcoded user. instead of that put sys_id of user in sys_properties. Use that sys_properties from script include.

Please mark this response as Helpful and hit Like if it assisted you with your question.
Regards,
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: