Acl script for accessing the user who has the name in it worknote list.

Vivek Verma
Mega Sage
Mega Sage

answer = current.work_notes_list + "").indexOf(gs.getUserID()) > -1);

Please Explain this code.

1 ACCEPTED SOLUTION

Tushar Sharma2
Kilo Guru

Hello Vivek,

This will check whether work_note contains the name of logged in user.

 current.work_notes will fetch the value of work_notes field, further it will fetch the position of user name using indexOf then it will compare with -1, if user name present in the work notes then it will provide index which will starts with 0. and at the last it will have either true or false which it assigned to Answer variable.

For example:

Worknotes has value "This Note is posted by Vivek ".

current.work_notes has above value and we need to check whether it contained logged in user name, so to check this we call function indexOf("gs.getUserID()") which will fetch the user id and check the same in the current.work_notes.

In this case  current.work_notes_list .indexOf(gs.getUserID()) has 5 because Vivek is in the 6th position  and array starts with 0 hence it will return 5.

Now we are comparing with -1 whether "current.work_notes_list .indexOf(gs.getUserID())" is greater than -1, because it will return 0 if logged in user name is on the first index. 

 

Let me know if you have any query.

Hit Helpful or Correct on the impact of response.
-Tushar

View solution in original post

2 REPLIES 2

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

That doesn't look right to me. I think it should be something like this:

 

answer = (current.work_notes_list.indexOf(gs.getUserID()) > -1);

 

A quick example on my own instance:

var gr = new GlideRecord('incident');
gr.get('9d385017c611228701d22104cc95c371');

answer = (gr.work_notes_list.indexOf(gs.getUserID()) > -1);

gs.print(answer);

 

gives me:

 

*** Script: true

 

As my user is in the list.

 

Tushar Sharma2
Kilo Guru

Hello Vivek,

This will check whether work_note contains the name of logged in user.

 current.work_notes will fetch the value of work_notes field, further it will fetch the position of user name using indexOf then it will compare with -1, if user name present in the work notes then it will provide index which will starts with 0. and at the last it will have either true or false which it assigned to Answer variable.

For example:

Worknotes has value "This Note is posted by Vivek ".

current.work_notes has above value and we need to check whether it contained logged in user name, so to check this we call function indexOf("gs.getUserID()") which will fetch the user id and check the same in the current.work_notes.

In this case  current.work_notes_list .indexOf(gs.getUserID()) has 5 because Vivek is in the 6th position  and array starts with 0 hence it will return 5.

Now we are comparing with -1 whether "current.work_notes_list .indexOf(gs.getUserID())" is greater than -1, because it will return 0 if logged in user name is on the first index. 

 

Let me know if you have any query.

Hit Helpful or Correct on the impact of response.
-Tushar