generateUsername AttributeGenerator Rule

The below attributeGenerator Rule was deployed and throw the following error

“java.lang.RuntimeException: sailpoint.tools.GeneralException: Error running rule transform:sailpoint.tools.GeneralException: The application script threw an exception: java.lang.NullPointerException: Null Pointer in Method Invocation BSF info: generateUsername at line: 0 column: columnNo”

Any clue what might the problem be?


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;

int maxIteration = 50; // Maximum number of attempts to generate a unique username
int maxUsernameLength = 20; // Maximum length of the username
String uniqueCounterSeparator = "."; // Separator between the username and the unique counter

public String generateUsername ( String firstName, String lastName, String middleName, int iteration ) {
  // Data protection. Remove spaces and null values from the input names
  firstName = StringUtils.trimToNull( firstName );
  lastName = StringUtils.trimToNull( lastName );
  middleName = StringUtils.trimToNull( middleName );
  
  if ( ( firstName == null ) || ( lastName == null ) ) return null; // Return null if first name or last name is missing
  
  // This will hold the final username
  String username = null;
  
  // Try different patterns based on the iteration number
  switch ( iteration ) {
    case 0: // firstname.lastname
      username = firstName + "." + lastName;
      break;
    case 1: // firstname.middleName[0].lastname
      if ( middleName != null ) { // Only use this pattern if middle name is not null
        username = firstName + "." + middleName.charAt(0) + "." + lastName;
      }
      break;
    case 2: // firstname[0].lastname
      username = firstName.charAt(0) + "." + lastName;
      break;
    case 3: // firstname[0,1].lastname
      if ( firstName.length() > 1 ) { // Only use this pattern if first name has more than one character
        username = firstName.substring(0,2) + "." + lastName;
      }
      break;
    case 4: // firstname.lastname[0]
      if ( lastName.length() > 1 ) { // Only use this pattern if last name has more than one character
        username = firstName + "." + lastName.charAt(0);
      }
      break;
    default: // firstname.lastname+uniqueCounter
      username = firstName + "." + lastName + uniqueCounterSeparator + ( iteration - 4 );
      break;
  }
  
  // Check if the username is unique in the Active Directory & Less than 20 Characters
  if ( isUnique ( username ) && username.length() < maxUsernameLength )
    return username;
  else if ( iteration < maxIteration )
    // Try another pattern if the iteration limit is not reached
    return generateUsername ( firstName, lastName, middleName, ( iteration + 1 ) );
  else
    return null;
}

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

if(identity != null && identity != void)
  {
    String firstName = identity.getAttribute("firstname");
    String middleName = identity.getAttribute("middleName");
    String lastName = identity.getAttribute("lastname");		
      
    return generateUsername ( firstName, lastName, middleName, 0 );
  }
else
    return null;

Inside case 1, 3 and 4 in the switch block you are setting up username with an if condition. If the condition is not true, username remains null, which might be causing the NullPointerException on username.length() in the if condition after switch block. Add else to these 3 cases and try

Using recursion like this is also frowned upon from good programming practice.

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