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

Musab Rasheed
Tera Sage
Tera Sage

Hi @ServiceLater ,

Write a Query business rule like below

current.addQuery('company_name' , gs.getUser().getCompanyID());

if company name is reference then this will work or else tweak above to make it work.

Mark my answer as correct or hit like based on impact.

Please hit like and mark my response as correct if that helps
Regards,
Musab

Adrian Ubeda
Mega Sage
Mega Sage

Hi ServiceLater,

Creating an ACL or using query BR will work for this. Use BR if you want 'security constraint' error doesn't appear.

I let you some useful docs:

Query BR: https://community.servicenow.com/community?id=community_blog&sys_id=240af05ddb010cd44819fb24399619ca

Best practices for creating ACL: https://community.servicenow.com/community?id=community_article&sys_id=ac67a4ccdb36e300f0612183ca961906

If it was helpful, please give positive feedback,
Thanks.

If it was helpful, please give positive feedback! ✔
☆ Community Rising Star 22, 23 & 24 ☆