Help With Email Notification Advanced Condition Scripting

Rick Forristall
Tera Guru

Good day,

I have a notification that needs to be sent when a field is updated. This field is updated two ways. (1) By the marketing team inside a form view and (2) by an inbound email action when the customer replies to an email sent by ServiceNow.

When the field is updated by method 2 I need to ensure only the Marketing folks get notified. So in the advanced Condition script I'm trying to do this:

1. When the field is updated and

2. If the person who updated the field is NOT in the GICA Marketing group.

The problem is whenever the user replies and the field gets updated both the user AND marketing gets notified.

Here's the script I'm using - can anyone help?

I think the problem I'm having is related to trying to see if the updated by field is in the group.

Thanks in advance for any assistance,

Rick Forristall

Goodwill of Central Arizona

======================= SCRIPT BELOW ==========================

// only send this email notification to Marketing if the updated by person is NOT in marketing

if (current.u_customer_communications.changes() && !isGroupMember(current.sys_updated_by, 'GICA Marketing') ) {

      answer = true;

} else {

      answer = false;

}

function isGroupMember(user, groupName) {

      var good_user   = false;

      var good_group = false;

      // does the user exist?

      var usrGr = new GlideRecord('sys_user');

      usrGr.addQuery('user_name', user);

      usrGr.query();

      if (usrGr.next() ) {

              userID = usrGr.sys_id + '';

              good_user = true;

      }

     

      // does the group exist?

      var grpGr = new GlideRecord('sys_user_group');

      grpGr.addQuery('name', groupName);

      grpGr.query();

      if (grpGr.next() ) {

              groupID = grpGr.sys_id + '';

              good_group = true;

      }

     

      if (good_user && good_group) {

              //Now see if they are a member of the group

              var grpMbr = new GlideRecord('sys_user_grmember');

              grpMbr.addQuery('user', userID);

              grpMbr.addQuery('group', groupID);

              gr.query();

              if (gr.next()) {

                      return true;

              } else {

                      return false;

              }

             

      } else {

              return false;

      }

}

1 ACCEPTED SOLUTION

Gurpreet07
Mega Sage

In last GlideRecord you are using variable grMbr but you made queries gr.query() and   gr.next()   .


use grMbr.query() and grMbr.next().



Also according to me you just need to use the third glide record because sys_user_grmember table will have the record only if user and group exists in their respective tables.


View solution in original post

6 REPLIES 6

Gurpreet07
Mega Sage

In last GlideRecord you are using variable grMbr but you made queries gr.query() and   gr.next()   .


use grMbr.query() and grMbr.next().



Also according to me you just need to use the third glide record because sys_user_grmember table will have the record only if user and group exists in their respective tables.


Harish Murikina
Tera Guru

Hi Richard,



                        My suggestion on this, Create new email notification but use same template.


                        From inbound action fire the event.



Regards,


Harish.


Harish Murikinati


Thank you for your reply. I like your idea, but I wanted to keep all the information in one location -- the notifications.



Thanks again,



Rick Forristall


Goodwill of Central Arizona


Kalaiarasan Pus
Giga Sage

This 2 line equivalent code should do the job for you



var currentUser = gs.getUser().getUserByID(current.sys_updated_by);


return (currentUser.isMemberOf('GICA Marketing'));



Please click on the Correct Answer and/or Helpful Answer buttons if the response helps. This helps other users to see that a valid response was received.