how can i set mail to a perticular group on some condtion in email script(sys_script_email)

mishra007
Tera Contributor

There is a requirement where i need to write a script with if else condtion , when if condtion is met then all the users of group abc should recieve notification otherwise only requester and its manager

 

3 REPLIES 3

SunilKumar_P
Giga Sage

Maddysunil
Kilo Sage

@mishra007 

Below is sample business rule code , please modify as per your fields

 

 

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

    // Check if the condition is met
    if (conditionIsMet(current)) {
        // If the condition is met, send notification to all users in the group "abc"
        sendNotificationToGroupMembers("abc");
    } else {
        // If the condition is not met, send notification to requester and requester's manager
        sendNotificationToRequesterAndManager(current);
    }

})(current, previous);

// Function to check if the condition is met
function conditionIsMet(current) {
    // Add your logic here to determine if the condition is met
    // For example, you might check some fields on the current record
    return current.someField === "someValue";
}

// Function to send notification to all users in a group
function sendNotificationToGroupMembers(groupName) {
    // Get users in the specified group
    var groupMembers = new GlideRecord('sys_user_grmember');
    groupMembers.addQuery('group', 'IN', groupName);
    groupMembers.query();
    
    while (groupMembers.next()) {
        // Send notification to each user in the group
        sendNotification(groupMembers.user.toString());
    }
}

// Function to send notification to requester and requester's manager
function sendNotificationToRequesterAndManager(current) {
    // Get requester's ID from the current record
    var requesterId = current.requested_for.toString();
    
    // Send notification to requester
    sendNotification(requesterId);
    
    // Get requester's manager ID
    var managerId = getManagerId(requesterId);
    
    // Send notification to requester's manager
    sendNotification(managerId);
}

// Function to get manager's ID
function getManagerId(userId) {
    var user = new GlideRecord('sys_user');
    user.get(userId);
    
    // Assuming manager field is "manager" - update it if your instance uses a different field
    return user.manager.toString();
}

// Function to send the actual notification
function sendNotification(userId) {
    // Add your notification logic here
    gs.info("Sending notification to user: " + userId);
    // ... Add code to send notification
}

 

Kindly mark helpful/accepted if it helps you.

shyamkumar VK
Kilo Patron

@mishra007  ,

Please take a look on the below Link which has similar Solution 

 

https://www.servicenow.com/community/now-platform-forum/how-to-send-email-to-particular-users-in-a-g...

Let me know if that doesn't work !!

 

Regards,

Shyamkumar

 

Please mark this as helpful and accept as a solution if this resolves your Ask.
Regards,

Shyamkumar