Show only records where the users company is the same as a field in the records

ServiceLater1
Giga Contributor

I have a table with records for contracts. When a user is trying to view the list of records, I want to automatically show them all records where one field matches the company name of the users who is currently logged in.

What is the best way to achieve this and can you please point to any good documentation?

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use query BR on your contracts table

Condition:

gs.getSession().isInteractive() && !gs.hasRole('admin')

Script:

I assume your company field is reference to core_company table

current.addQuery('companyField', gs.getUser().getCompanyID()); // give here the company field name

Regards
Ankur

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

View solution in original post

6 REPLIES 6

AnirudhKumar
Mega Sage
Mega Sage

Create a Read ACL for this.

You will have to use the script field in the ACL to write the company logic.

Open any existing Read ACL with some script in it and you will get an idea.

ServiceLater1
Giga Contributor

So to clarify, I do have a read ACL and the user is able to see only the contracts where the two fields match.

But when he calls up the list, it initially by default doesn't show him anything until he uses the filter and searches for contracts from his company, then it displays a list.

 

But I would like the list to automatically filter the records based on his company name and the company name in the record as long as the user has a specific role. Is this possible and if yes is there documentation on how to achieve this?

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you can use query BR on your contracts table

Condition:

gs.getSession().isInteractive() && !gs.hasRole('admin')

Script:

I assume your company field is reference to core_company table

current.addQuery('companyField', gs.getUser().getCompanyID()); // give here the company field name

Regards
Ankur

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

Thank you, I ended up creating a query business rule as you suggested. Was not aware you could specify a query BR, so cool to learn something new.