Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Way to list only records that value of User_name filed is me.

matsui
Tera Contributor

I have some records.
Those records has User_name filed.
User_name is Reference for sys_user.

 

I want to list only records that value of User_name filed is me.
As an example:
When UserA logged in, it shows only value of User_name filed is UserA
When UserB logged in, it shows only value of User_name filed is UserB

 

I tried to find expected filter, it can't select logged in user name like gs.getUserID().
I think that I can do it using business rule of addEncodedQuery, but I don't have any idea...

 

How I create a business rule to resolve this issue.

 

Regards,

5 REPLIES 5

Bert_c1
Kilo Patron

Hi, you can do this in a business rule like the following defined on the incident table and 'caller_id' field:

 

Screenshot 2023-10-28 104742.png

change the table as desired. Advanced script:

 

restrictIncidents();
function restrictIncidents() {
	var u = gs.getUserID();
	current.addQuery("caller_id", u);
}

change the function name and field ('caller_id') as desired.

matsui
Tera Contributor

@Bert_c1 

 

Thank you. Great! It worked as expected!!
I want to ask additional question.
I want to list User_name filed is me and User_name filed is empty.
In other word, the list screen is showing User_name is me and empty records.
How I can do it ?

 

Regards,

Hi,

There happens to be an OOB Query BR on incident table that will limit the visibility of records. Did you give it a check?

Look in sys_dictionary for your field name. There is no field named 'User_name' in my instance. There are 59 fields with column name = 'user_name', only 4 that are Reference fields to sys_user table.