# AttributeGenerator Rule

**URL:** <https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810>\
**Category:** SHF Discussion and Questions\
**Created:** [October 18, 2021, 6:14am UTC](https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810 "2021-10-18T06:14:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sushant](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@Sushant](https://developer.sailpoint.com/discuss/u/Sushant)\
**Post date:** [October 18, 2021, 6:14am UTC](https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810/1 "2021-10-18T06:14:31Z")

</div>

Hi All,

I am working on the AttributeGenerator rule to generate the unique value for the “userPrincipalName” while provisioning new accounts in Active Directory. The rule is working fine while provisioning the first account but I am getting an error while provisioning the second account. (java.lang.IllegalStateException: Unable to generate a unique value for Source[sourcename] Field [userPrincipalName] after 1 retries. BSF info: Generate Upn at line: 0 column: columnNo).

Can anyone please let me know what changes do I need to make to get it working

Here is the rule:

```
public String normalizeInput(String inputString)
{
  if (inputString!=null && !inputString.isEmpty())
  {
    // Remove any non-alphanumerics
    inputString=inputString.replaceAll("[^A-Za-z0-9]", "");
    // Strip off any non-printables
    inputString=StringUtils.trimToNull(inputString);
  }
  
  return inputString;
}	

  //Check Active Directory for uniqueness
  public boolean isUnique(String upn) {
  log.error("Entering isUnique, userPrincipalName is:" + upn);
  LDAPConnectorService ldapService = ServiceModule.getService( LDAPConnectorService.class );
  
  Field field = new Field();
  field.setName("userPrincipalName");
  field.setAttribute("cloudMaxSize", "99" );
  field.setAttribute("cloudMaxUniqueChecks", "1" );	  
  field.setType("String");
  field.setAttribute("cloudRequired", "true");
  field.setAttribute("cloudToUpperCase",false);	  
  field.setAttribute("template", upn);
  String generated = ldapService.generateUniqueLDAPAttribute( context, application, identity, field, null );
  log.error("Generated value is:" + generated);

	if(generated == null || !generated.equals(upn)) { 
		return false; 
	}
	else { 
		return true; 
	}
}

public String generateupnAddress (String firstName, String middleName, String lastName, String domainForEmailCreation) 
{		
	// set some variables we'll use later
	String fi;
	String mi;
	String alias = null;
	String upn = null;
	int iterator = 0;
	int counter = 1;
			
	if ( ( firstName == null ) || ( lastName == null )){
			return null;
		}
	
	else if (( middleName != null ) && ( middleName != "" ) ) {
		mi = middleName.substring(0,1);
		do
		{
			switch ( iterator )
			{
				case 0:
					alias = firstName + "." + lastName;
					break;
				case 1:
					alias = firstName + "." + mi + "." + lastName;
					break;
				default:
					alias = firstName + "." + lastName + String.valueOf(counter);
					counter++;
					break;					
			}
			
			iterator++;
			upn = alias + "@" + domainForEmailCreation;
		
		} while ( !(isUnique(upn) ) && ( iterator < 999 ) );
		// Don't let the it loop forever if something goes wrong
		
		if( ( iterator >= 999) || (upn.isEmpty()) || upn == null ) 
			return null;
		else
			return upn;
	}
	else {
		do
		{
			switch ( iterator )
			{
				case 0:
					alias = firstName + "." + lastName;
					break;
				default:
					alias = firstName + "." + lastName + String.valueOf(counter);
					counter++;
					break;
			}
			
			iterator++;
			upn = alias + "@" + domainForEmailCreation;

		} while ( !(isUnique(upn) ) && ( iterator < 999 ) );
		// Don't let the loop forever if something goes wrong

		if( ( iterator >= 999) || (upn.isEmpty()) || upn == null ) 
			return null;
		else
			return upn;
	}
}

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

```

Thank you,  
Sushant

---

<div class="post-metadata">

**Author:** ![colin\_mckibben](https://sea1.discourse-cdn.com/sailpoint/discuss/user_avatar/developer.sailpoint.com/colin_mckibben/32/37182_2.png) [@colin\_mckibben](https://developer.sailpoint.com/discuss/u/colin_mckibben)\
**Post date:** [October 25, 2021, 7:33pm UTC](https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810/2 "2021-10-25T19:33:40Z")

</div>

I took a look at the [username generator transform docs](https://community.sailpoint.com/t5/IdentityNow-Wiki/IdentityNow-Transforms-Username-Generator/ta-p/180577), and I think you could try increasing your `cloudMaxUniqueChecks` to 4. The docs I shared have a `cloudMaxUniqueChecks` value that is one greater than the total number of patterns. I see you have a few switch statements, but the one with the most patterns has a total of 3, so a unique check value of 4 might do the trick.

---

<div class="post-metadata">

**Author:** ![seemarani](https://avatars.discourse-cdn.com/v4/letter/s/ed655f/32.png) [@seemarani](https://developer.sailpoint.com/discuss/u/seemarani)\
**Post date:** [February 3, 2022, 2:37pm UTC](https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810/3 "2022-02-03T14:37:54Z")

</div>

@Sushant Is the solution mentioned by @colin_mckibben worked for you. we’ve something similar requirement for sAMAccountName uniqueness generation

---

<div class="post-metadata">

**Author:** ![vijay\_sharma](https://avatars.discourse-cdn.com/v4/letter/v/8edcca/32.png) [@vijay\_sharma](https://developer.sailpoint.com/discuss/u/vijay_sharma)\
**Post date:** [June 14, 2023, 5:30am UTC](https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810/4 "2023-06-14T05:30:06Z")

</div>

I am writing attribute generator for below:

1. samaccountName
2. displayName

I need to use output of 1 inside 2 attribute generator rule. could you please guide us. Thank you !

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/sailpoint/original/2X/f/f2136700ed5e3703e0b85e02f6be799dacca7735.png) [@system](https://developer.sailpoint.com/discuss/u/system)\
**Post date:** [August 13, 2023, 5:30am UTC](https://developer.sailpoint.com/discuss/t/attributegenerator-rule/1810/5 "2023-08-13T05:30:13Z")

</div>

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