How to insert group members in CC through email script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 07:03 AM
Hi,
How to set corporate analyst group members in CC. I have tried with below script
script:
replaced and tried not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 07:25 AM
Hi Sanju,
Please find below line to add group in CC.
email.addAddress("cc", "group mail id", "group name");
Thanks,
Pooja M
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 07:30 AM
Hello @Talari Balateja
Please refer to the code below and see how it works for you.
- Use the script in the email body like this:
${mail_script:addCorporateAnalystCC}
- Mail Script:
(function runMailScript( /* GlideRecord */ current, /* TemplatePrinter */ template, /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action, /* Optional GlideRecord */ event) { // Fetch group members for the "Corporate Analyst" group var groupMembers = new GlideRecord('sys_user_grmember'); groupMembers.addQuery('group.name', 'Corporate Analyst'); // Group name to be fetched groupMembers.addEncodedQuery('user.emailISNOTEMPTY'); // Ensure user has an email groupMembers.query(); // Loop through each group member and add to CC while (groupMembers.next()) { var userEmail = groupMembers.user.email; // Get user's email address var userName = groupMembers.user.getDisplayValue(); // Get user's display name if (userEmail) { // Add to CC with both email and display name email.addAddress('cc', userEmail, userName); } else { gs.log("User " + userName + " does not have a valid email address."); } } })(current, template, email, email_action, event);
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.
Regards,
Aniket
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 07:30 AM
Your approach to adding the members of the Corporate Analyst group to the CC field is generally correct, but there are a couple of things to check for to ensure it works as expected.
- You can confirm the sys_id by navigating to the sys_user_group table in ServiceNow and locating the correct group.
- Ensure that the users in the group actually have valid email addresses. If a user does not have an email address, groupMembers.user.email will return null.
- verify that the script is retrieving the correct group members, you can add some logs.
- If the sys_id is not working, try using the group name again, but ensure that there are no typos.
(function runMailScript(current, template, email, email_action, event) {
var groupMembers = new GlideRecord('sys_user_grmember');
groupMembers.addQuery('group.name', 'Corporate Analyst');
groupMembers.query();
gs.info("Number of group members found: " + groupMembers.getRowCount());
while (groupMembers.next()) {
gs.info("Adding " + groupMembers.user.email + " to CC");
email.addAddress('cc', groupMembers.user.email);
}
})(current, template, email, email_action, event);
i hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
rajesh