Can anyone share the logic to get identity by email

Hi All,

Can anyone share the logic to get identity by email address?

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule created="" id="" language="beanshell" name="Get Identity Attributes By Email">
  <Description>
    Rule to fetch an Identity's attributes by matching email.
  </Description>
  <Source>
    import sailpoint.object.Identity;
    import sailpoint.object.QueryOptions;
    import sailpoint.object.Filter;
    import sailpoint.tools.GeneralException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    import java.util.Collections;

    Map&lt;String, Object&gt; getIdentityAttributesByEmail(String email) throws GeneralException {

        if (email == null || email.trim().isEmpty()) {
            throw new GeneralException("Email cannot be null or empty.");
        }

        // Normalize email for search (adjust if your environment is case sensitive)
        String normalizedEmail = email.trim().toLowerCase();

        Filter emailFilter = Filter.eq("email", normalizedEmail);
        QueryOptions qo = new QueryOptions();
        qo.addFilter(emailFilter);

        // Search for identities matching the email
        List&lt;Identity&gt; identities = context.getObjects(Identity.class, qo);

        if (identities == null || identities.isEmpty()) {
            log.debug("No identity found for email: " + normalizedEmail);
            return Collections.emptyMap();
        }

        // Emails should be unique → take first match
        Identity identity = identities.get(0);

        Map&lt;String, Object&gt; identityAttributes = new HashMap&lt;&gt;();

        // Retrieve all attributes
        for (String attributeName : identity.getAttributes().keySet()) {
            identityAttributes.put(attributeName, identity.getAttribute(attributeName));
        }

        log.debug("Retrieved attributes for Identity: " + identity.getName());
        log.debug("Attributes: " + identityAttributes);

        return identityAttributes;
    }

    // Example usage
    String emailToSearch = "Aaron.Nichols@demoexample2.com"; // Replace with your email
    Map&lt;String, Object&gt; userAttributes = getIdentityAttributesByEmail(emailToSearch);
    return userAttributes;
  </Source>
</Rule>

you can use this rule to get Identity by using EMAIL address

    public Identity getIdentityByEmail(String email) {
      QueryOptions op = new QueryOptions();
      op.add(Filter.ignoreCase(Filter.eq("email", email)));

      List rv = context.getObjects(Identity.class, op);

      if (rv.size() > 1) {
        log.error("Found multiple Identities with the same email "+email);
      }

      if (rv.size() == 1) {
        return (Identity) rv.get(0);
      }

      return null;
    }

Here’s a function we use in a rule library.