Regarding explanation of a block of code
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:45 PM
Can someone make me understand this below piece of code?
This is added in the Advanced Conditions for Email notification for Requests.
All inputs are appreciated!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:49 PM - edited 01-18-2024 11:05 PM
Hello @Appu2 ,
if (
current.comments.changes() &&
current.sys_updated_by == current.requested_for.user_name &&
previous.state != '3' &&
previous.state != '4' &&
previous.state != '7'
) {
//gs.log('testing true Notification'+current.state +","+ previous.state);
true;
}
else if (
current.comments.changes() &&
current.sys_updated_by == current.requested_for.user_name &&
(current.state.changesFrom(3) || current.state.changesFrom(4) || current.state.changesFrom(7)) &&
current.state.changesTo(2)
) {
//gs.log('testing false Notification'+current.state.changesFrom(6) +","+current.state.changesTo(2));
false;
}
- First if statement:
if (
current.comments.changes() &&
current.sys_updated_by == current.requested_for.user_name &&
previous.state != '3' &&
previous.state != '4' &&
previous.state != '7'
) {
true;
}
- This block checks if there are changes in the comments field.
- It checks if the user who updated the record (current.sys_updated_by) is the same as the user specified in the requested_for field (current.requested_for.user_name).
- It checks if the previous state (previous.state) is not '3', '4', or '7'.
- If all these conditions are true, it returns true.
2. Second else if statement:
else if (
current.comments.changes() &&
current.sys_updated_by == current.requested_for.user_name &&
(current.state.changesFrom(3) || current.state.changesFrom(4) || current.state.changesFrom(7)) &&
current.state.changesTo(2)
) {
false;
}
- This block also checks if there are changes in the comments field.
- It checks if the user who updated the record is the same as the user specified in the requested_for field.
- It checks if the state changed from either '3', '4', or '7' (current.state.changesFrom(3) || current.state.changesFrom(4) || current.state.changesFrom(7)).
- Additionally, it checks if the state changed to '2' (current.state.changesTo(2)).
- If all these conditions are true, it returns false.
3.Comments:
- The code contains commented-out gs.log statements, which are likely for debugging purposes. These statements would log information about the current and previous states when the conditions are met.
In summary, the code seems to be a part of an Email notification logic for Requests in ServiceNow. It evaluates different conditions based on changes in the record's fields and states to determine whether to return true or false. The specific conditions suggest that it's handling notifications for specific state transitions and user interactions.
Let me know your views on this and Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks,
Aniket

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:55 PM - edited 01-18-2024 10:57 PM
Hi @Appu2
below response
1. The first condition `current.comments.changes()` checks if there have been any changes in the "comments" field of the current record.
2. The second condition `current.sys_updated_by == current.requested_for.user_name` checks if the user who updated the record is the same as the user who is requested for the record.
3. The third condition `previous.state!= '3' && previous.state!= '4' && previous.state!= '7'` checks if the previous state of the record is not '3', '4', or '7'.
if(current.comments.changes() && current.sys_updated_by == current.requested_for.user_name && previous.state!= '3' && previous.state!= '4' && previous.state!= '7' )
{
//gs.log('testing true Notification'+current.state +","+ previous.state);
true;
}
now the else part
4. If the first condition is true, but the third condition is false, the code moves to the "else if" block.
5. The fourth condition `(current.state.changesFrom(3)|| current.state.changesFrom(4) || current.state.changesFrom(7))` checks if the current state of the record has changed from '3', '4', or '7'.
6. The fifth condition `current.state.changesTo(2)` checks if the current state of the record has changed to '2'.
else if(current.comments.changes() && current.sys_updated_by == current.requested_for.user_name &&( current.state.changesFrom(3)|| current.state.changesFrom(4) || current.state.changesFrom(7)) && current.state.changesTo(2))
{
//gs.log('testing false Notification'+current.state.changesFrom(6) +","+current.state.changesTo(2));
false;
}
Harish