Display only the records created by the currently logged in user

Ni_a Sy
Tera Contributor

Good day. Can anyone help me to display only the records created by the currently logged in user according to their user group using client script on load? My script

if(g_user.hasRole('x_886430_suggest.acn read only team')){
//I am now lost what I need to write but i am thinking 
.setQuery("createdby=javascript:gs.getUserName();");
}else{

}

I am just new here at serviceNow.

Thank you.

2 ACCEPTED SOLUTIONS

Muhammad Khan
Mega Sage
Mega Sage

Try with before query Business Rule on your required table and in the script field of business rule try something like below.

 

(function executeRule(current, previous /*null when async*/ ) {

    if (gs.getUser().isMemberOf('Group Name')) {
        current.addQuery('sys_created_by', gs.getUser().getName());
    }

})(current, previous);

 

View solution in original post

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

Not sure I got your requirement but do you want to show the incidents which are created by a user and it is assigned to group he is a part of?

 

Please create a on before BR with the below script:-

 

(function executeRule(current, previous /*null when async*/ ) {

current.addEncodedQuery("assignment_groupIN" + gs.getUser().getMyGroups().toArray().join() + "^sys_created_by=" + gs.getUser().getName());

})(current, previous);

 

I see you have mentioned you cannot select the Query checkbox, is your advanced checbox selected as true

 

Saurav11_0-1665247946846.png

 

If it is selected then check the UI policy "Hide Advanced Controls" where the control is and see if it is correctly configured. Also you can configure dictionary of the query checkbox and see if it has been made readonly from the dictionary itself.

 

Please mark answer correct/helpful based on Impact.

View solution in original post

9 REPLIES 9

Muhammad Khan
Mega Sage
Mega Sage

Try with before query Business Rule on your required table and in the script field of business rule try something like below.

 

(function executeRule(current, previous /*null when async*/ ) {

    if (gs.getUser().isMemberOf('Group Name')) {
        current.addQuery('sys_created_by', gs.getUser().getName());
    }

})(current, previous);

 

Hi thank you so much for your reply. I cant  able to check or choose the query button1.png

I am trying to do it at client script onLoad:
function onLoad() {
//Type appropriate comment here, and begin script below
var now_GR = new GlideRecord('x_886430_suggest_suggestion');
if(g_user.hasRole('x_886430_suggest.acn read only team')){
now_GR.getEncodedQuery('sys_created_by', g_user.userID.getName());
//now_GR.query(response);


}
}

 

 

No you cannot do it with client script