We want to create a unique sAMAccountName

We want to create a unique sAMAccountName using a specific pattern: the first letter of the user’s first name, the first three letters of their last name, followed by three random characters. Could you help with a code snippet to implement this logic?

Hi @satishNG,

You can use cloud rule Account Profile Attribute Generator | SailPoint Developer Community

Find below code:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule language="beanshell" name="Generate Username" type="AttributeGenerator">
  <Description>This will generate a username.</Description>
  <Source><![CDATA[
import sailpoint.tools.GeneralException;
import java.util.Iterator;
import sailpoint.object.*;
import java.util.ArrayList;
import sailpoint.api.*;
import sailpoint.object.*;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public String generateUsername ( String firstName,  String lastName) {

  // Data protection.
  firstName = StringUtils.trimToNull( firstName );
  lastName = StringUtils.trimToNull( lastName );
  // Get the first letter of the first name
   String firstLetter = firstName.substring(0, 1).toLowerCase();
    
   // Get the first three letters of the last name
   String firstThreeLettersLastName = lastName.substring(0, Math.min(3, lastName.length())).toLowerCase();
    
   // Generate three random characters
   String randomChars = generateRandomChars(3);
    
   // Combine to form the sAMAccountName
   String sAMAccountName = firstLetter + firstThreeLettersLastName + randomChars;
  
  if ( isUnique ( sAMAccountName ) )
    return sAMAccountName;
  else
   return generateUsername ( firstName,  lastName);
}

private String generateRandomChars(int length) {
    String chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuilder sb = new StringBuilder(length);
    
    for (int i = 0; i < length; i++) {
        sb.append(chars.charAt(random.nextInt(chars.length())));
    }
    
    return sb.toString();
}

public boolean isUnique ( String sAMAccountName ) throws GeneralException {
  return !idn.accountExistsByDisplayName(application.getName(), sAMAccountName);
}

return generateUsername( identity.getFirstname(), identity.getLastname());

  ]]></Source>
</Rule>
1 Like

Hi satish

import sailpoint.tools.GeneralException;
import sailpoint.object.Identity;
import sailpoint.api.SailPointContext;
import org.apache.commons.lang.StringUtils;
import java.util.Random;

public class UsernameGenerator {

public static String generateUsername(String firstName, String lastName, SailPointContext context, String applicationName) throws GeneralException {
    firstName = StringUtils.trimToNull(firstName);
    lastName = StringUtils.trimToNull(lastName);

    if (firstName == null || lastName == null || firstName.length() < 1 || lastName.length() < 3) {
        throw new GeneralException("Cannot generate username: first or last name too short or null");
    }

    String pattern = firstName.substring(0, 1).toLowerCase() + lastName.substring(0, 3).toLowerCase();

    // Attempt to generate a unique username with retries
    for (int i = 0; i < 10; i++) {
        String candidate = pattern + getRandomChars(3);
        if (isUnique(candidate, context, applicationName)) {
            return candidate;
        }
    }

    throw new GeneralException("Unable to generate a unique username after multiple attempts.");
}

private static boolean isUnique(String username, SailPointContext context, String applicationName) throws GeneralException {
    Identity identity = context.getObjectByName(Identity.class, username);
    return identity == null;
}

private static String getRandomChars(int length) {
    String chars = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random random = new Random();
    StringBuilder result = new StringBuilder();

    for (int i = 0; i < length; i++) {
        result.append(chars.charAt(random.nextInt(chars.length())));
    }

    return result.toString();
}

}

1 Like
  • Validate the Rule:
  • First, convert your rule into XML format.
  • Use the IdentityNow Rule Validator tool (available on Compass) to validate the rule locally.
  • Ensure the validation result is “Validation: SUCCESS.”
  • Prepare Submission Files:
  • After successful validation, take a screenshot of the success message.
  • Save and include the validated XML file.
  • Submit to SailPoint:
  • Share both the screenshot and the XML file with your SailPoint team or professional services contact so they can upload the rule to your tenant.
2 Likes

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