Email attribute generator rule

Hi There, I am building the Email generator for below scenarios

  • Firstletter of firstname.lastname
  • Firstletter of firstname.firstletrrer of initials.lastname
  • Firstletter of firstname.lastname(counter value)
  • Accent characters will be converted to plain alphabets
  • O’Brien etc- > ‘ will be omitted
  • What happens in case of two lastnames?- use both surnames without spaces.

I am getting following error:
The application script threw an exception: java.lang.NullPointerException: Null Pointer in Method Invocation BSF info: Generate Unique StaffmailID at line: 0 column: columnNo"

Below is my code:

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;
import java.text.Normalizer;

int maxIteration = 1000;
int emailAddressCount = 0;
List SEARCH_EMAILADDRESS = null;
List SEARCH_IN_SOURCE_IDS = new ArrayList(Arrays.asList(new String[] {"b07f3c288bed8f9bb","b8170ad313b14c7ab550","2c8e83b4687e018a8f252ea6","2c918087846385b0727d50a5e","e085270eef4ac454"}));
log.error("Generate Unique mailID: SEARCH_IN_SOURCE_IDS"+SEARCH_IN_SOURCE_IDS);
String SEARCH_OP = "Equals";
String PROMOTED_ATTR_EMAILADDRESS = "email";

public int getDuplicateEmailAddressCount(String emailAddressValue) throws GeneralException{

    log.error("Generate Unique mailID:getDuplicateEmailAddres method");
	    emailAddressCount = 0;
		SEARCH_EMAILADDRESS = new ArrayList();
		SEARCH_EMAILADDRESS.add(emailAddressValue); 
    log.error("Generate Unique mailID:getDuplicateEmailAddres method SEARCH_EMAILADDRESS:"+SEARCH_EMAILADDRESS);      
		emailAddressCount = idn.attrSearchCountAccounts(SEARCH_IN_SOURCE_IDS, PROMOTED_ATTR_EMAILADDRESS, SEARCH_OP, SEARCH_EMAILADDRESS);
    log.error("Generate Unique mailID:getDuplicateEmailAddres method emailAddressCount:"+emailAddressCount);
		return emailAddressCount;
	}

public String modifyName(String targetName)
{
    log.error ("Inside modifyName function");
    String finalName = "";
	Integer i =0;
    StringBuilder sb = new StringBuilder(targetName.length());
    targetName = Normalizer.normalize(targetName, Normalizer.Form.NFD);
    for (char c : targetName.toCharArray()) {
        if (c <= '\u007F') sb.append(c);
    }
    log.error("targetName normalize:" +sb);
    String newName = sb.toString();
	log.error("newName normalize:" +newName);
	
    String[] parts = newName.split(" ");
    String[] parts1 = newName.split("'");
    int j = parts.length;
    int k = parts1.length;
    log.error("array length parts:" +j);
    log.error("array length parts:" +k);
    if (j>1)
    {
       for(int b=0;b<j;b++)
       {
        finalName = finalName + parts[b];
       }
    }
    else if (k>1)
    {
       for(int b=0;b<k;b++)
       {
        finalName = finalName + parts1[b];
        }
      }
    else
    {
       finalName = newName;
    }
	return finalName;
}

public String generateEmail ( String firstName,  String lastName, String initials, int iteration ) {
 log.error("Generate Unique mailID:generateEmail");
  // Data protection.
  firstName = StringUtils.trimToNull( firstName );
  firstName = modifyName(firstName);
   log.error("Generate Unique mailID:generateEmail firstName"+firstName);
  lastName = StringUtils.trimToNull( lastName );
  lastName = modifyName(lastName);
  log.error("Generate Unique mailID:generateEmail lastName"+lastName);
  initials = StringUtils.trimToNull( initials );
  initials = modifyName(initials);
  log.error("Generate Unique mailID:generateEmail initials"+initials);

  log.error("Generate Unique mailID:generateEmail iteration"+iteration);


  if ( ( firstName == null ) || ( lastName == null ) ){

   log.error("Generate Unique mailID:generateEmail firstName & lastName is null");
  return null;
}
  // This will hold the final emailID;
  
  String emailID = null;

  switch ( iteration ) {
    case 0:
      emailID = firstName.substring(0, 1) + "." + lastName + "@leeds.ac.uk";
        log.error("Generate Unique mailID:generateEmail iteration case 0"+iteration);
        log.error("Generate Unique mailID:generateEmail iteration case 0 emailID"+emailID);
      break;
    case 1:
      emailID = firstName.substring(0, 1) + "." + initials.substring(0,1)  + "." + lastName+ "@leeds.ac.uk";
              log.error("Generate Unique mailID:generateEmail iteration case 1"+iteration);
        log.error("Generate Unique mailID:generateEmail iteration case 1 emailID"+emailID);
      break;
    default:
      emailID = firstName.substring(0, 1) + "." + lastName + iteration + "@leeds.ac.uk";
                log.error("Generate Unique mailID:generateEmail iteration case default"+iteration);
        log.error("Generate Unique mailID:generateEmail iteration case default emailID"+emailID);
      break;
  }
  emailAddressCount = getDuplicateEmailAddressCount(emailID);
   log.error("Generate Unique mailID:generateEmail iteration emailAddressCount"+emailAddressCount);
  if(emailAddressCount == 0)
  {
  log.error("Generate Unique mailID:generateEmail emailAddressCount zero"+emailAddressCount);
	 return emailID;
  }
  else if ( iteration < maxIteration )
  {
  log.error("Generate Unique mailID:generateEmail emailAddressCount elseif iteration"+iteration);
    return generateEmail ( firstName,  lastName, initials, ( iteration + 1 ) );
  }
  else
    return null;
}

return generateEmail( identity.getAttribute("preferredfirstname"), identity.getAttribute("preferredlastname"), identity.getAttribute("initials"), 0 );

Can anyone help me to identify the issue?

Looks to be a Null Pointer error:

To troubleshoot this issue, you can follow these steps:

  1. I haven’t gotten a stack trace before, but if you can get one it may be helpful: The stack trace will provide more detailed information about the error, including the line number where the exception occurred. Please provide the complete stack trace so that I can assist you further.

  2. Review the code: Look for any variables or method invocations that could potentially be null. In this case, pay attention to the variables identity, identity.getAttribute(“preferredfirstname”), identity.getAttribute(“preferredlastname”), and identity.getAttribute(“initials”). Ensure that these variables are properly initialized and not null before invoking any methods on them.

  3. Another option I haven’t tested yet with hands on experience is if you are able to simulate this offline and if so, to do print statements to evaluate where your null values are at.

It seems like the error is thrown from inside the method modifyName. Add a null check to this method in the beginning as below and check

public String modifyName(String targetName) {
if(targetName == null) return null;
... current Code
}

PS: You might also want to add a null check for initials inside generateEmail method

1 Like

Thank you Fred and Nithesh, It worked!

2 Likes

Great!!
Could you please share the changes you made that resolved the issue?
TIA

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