Email to multiple users using multiple templates

I am trying to trigger email as part of my workflow to multiple users using a set of specific templates(newA, newB, newC) that I have defined in EmailTemplates already to different user(launcher, abc.com,cde.com). The email trigger does not work. Below is my code.

import org.apache.commons.logging.Log;
        import org.apache.log4j.Logger;
        import org.apache.log4j.Level;
        import java.util.HashMap;
        import java.util.Map;
        import java.util.List;
        import java.util.ArrayList;
        import sailpoint.api.SailPointContext;
        import sailpoint.object.EmailOptions;
        import sailpoint.object.EmailTemplate;
        import sailpoint.object.Identity;

        Identity launcher_obj = context.getObjectByName(Identity.class, launcher);
        String launcherDisplayName = (String)launcher_obj.getDisplayName();
        List UserList = usr;
        Map vars = new HashMap();
        vars.put("userList", UserList);
        vars.put("launcherDisplayName", launcherDisplayName);
        Map<String, String> recipientsWithTemplates = new HashMap<>();
        recipientsWithTemplates.put("launcher", "NewA");
        recipientsWithTemplates.put("abc.com", "NewB");
        recipientsWithTemplates.put("cde.com", "NewC");
		 void sendNotificationEmail(SailPointContext context, String emailAddress, String templateName, 
        Map variables, String recipientName) {
        try {
        EmailTemplate template = context.getObjectByName(EmailTemplate.class, templateName);
        EmailOptions options = new EmailOptions();
        options.setTo(emailAddress);
        options.setVariables(variables);
        context.sendEmailNotification(template, options);
        log.debug("Mail has been successfully sent to " + recipientName + 
        " using template " + templateName);
        } catch (Exception e) {
        log.error("Failed to send email to " + recipientName + 
        " using template " + templateName, e);
        }
        }
        for (Map.Entry<String, String> entry : recipientsWithTemplates.entrySet()) {
        String recipient = entry.getKey();
        String templateName = entry.getValue();
        String recipientEmail;
        if ("launcher".equals(recipient)) {
        recipientEmail = (String)launcher_obj.getEmail();
        } else {
        Identity recipientObj = context.getObjectByName(Identity.class, recipient);
        recipientEmail = (String)recipientObj.getEmail();
        }
        sendNotificationEmail(context, recipientEmail, templateName, vars, recipient);
        }

hi @ShivangiS
You are almost there—just a couple of tweaks needed:

  1. You’re using email addresses like “abc.com” as Identity names, but getObjectByName expects actual Identity names. To look up users by email, use a QueryOptions filter instead.

  2. No getObjectByEmail method exists in IdentityIQ—use this instead:

QueryOptions qo = new QueryOptions();
qo.addFilter(Filter.eq("email", recipient));
List<Identity> identities = context.getObjects(Identity.class, qo);
  1. Make sure your email templates exist and are named correctly, and don’t forget to initialize your logger (Logger log = Logger.getLogger(…)).

  2. Add some null checks for safety—especially for the template and email address.

@haideralishaik How do I do this using custom object?
Here since I will have to hardcode the values in the main code so instead if I wanted to create custom object

hi @ShivangiS

  1. Create a Custom Object from debug :
  • Use a structure like this in the XML:

<Custom name="EmailTemplateMappings" type="Map">
  <entry key="launcher" value="NewA"/>
  <entry key="[email protected]" value="NewB"/>
  <entry key="[email protected]" value="NewC"/>
</Custom>
  1. Read the Custom Object in Your Code : Replace the hardcoded recipientsWithTemplates map with:

  2. Proceed with Your Existing Logic: Your loop and email sending logic can remain the same. Just make sure to handle cases where the identity is looked up by email using QueryOptions as discussed earlier.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.