Customized sAmaccountName in AD

Hi Team,

I am trying to figure out, how to generate the sAmaccountName where it picks up the value from email. We have a project where the new executive hire’s email address and sAmaccountName can be customized if they want us to add the customized email address [email protected] then this email should be updated in the email attribute alog with sAmaccountName should be XYZ. not firstname.lastname .

We have a cloud rule implemented for generating the sAmaccountName as per our company naming standard, which generates the sAmaccountname with this format( first name. Lastname or first name.m.lastname) Now I want to add the above logic too. So first it checks if HR enters the email address then that value should be used in the email attribute and sAmaccountName . Generally, HR never updates the email address , they only update when the hire executive wants to use customized email and sAmaccountName .

Here is below static transform which use to update the email address .
{
“name”: “mail”,
“transform”: {
“type”: “static”,
“attributes”: {
“calcEmail”: {
“attributes”: {
“value”: “[email protected]
},
“type”: “static”
},
“workEmail”: {
“type”: “firstValid”,
“attributes”: {
“values”: [
{
“attributes”: {
“name”: “email”
},
“type”: “identityAttribute”
},
“none”
]
}
},
“value”: “#if($workEmail == ‘none’)$calcEmail#{else}$workEmail#end”
}
},
“attributes”: {},
“isRequired”: false,
“type”: “string”,
“isMultiValued”: false
},

Could you please help me to add the logic to the cloud rule? here is the rule, which was written.

if(identity != null)
{
String firstName = normalizeInput(identity.getAttribute(“firstname”));
String lastName = normalizeInput(identity.getAttribute(“lastname”));
String middleName = normalizeInput(identity.getAttribute(“middleName”));
log.info(“[RingCentral-sama] Generate sAMAccountName for Identity: " + identity.getName());
log.info(”[RingCentral-sama] Input parameters: firstname: " + firstName + " middleName: " + middleName + " lastName: " + lastName);

	if (StringUtils.isNotBlank(firstName)){
		firstName = firstName.toLowerCase();
	}
	if (StringUtils.isNotBlank(middleName)){
		middleName = middleName.toLowerCase();
	}
	if (StringUtils.isNotBlank(lastName)){
		lastName = lastName.toLowerCase();
	}

	String sAMAccountName = "";
	int cursor = 0;
	int counter = 1;
	if( null != firstName && null != lastName )
	{
		log.info("[RingCentral-sama] Trying case 1 with: " + firstName + lastName);
		sAMAccountName = firstName + "." + lastName;
		log.info("[RingCentral-sama] sAMAccountName: " + sAMAccountName);
		if(!idn.accountExistsByDisplayName(applicationName,sAMAccountName))
		{
			return sAMAccountName;
		}
	}

	if ( null != firstName && null != lastName && null != middleName )
	{
		log.info("[RingCentral-sama] Trying case 2 with: " + firstName + middleName.substring(0,1) + lastName);
		sAMAccountName =  firstName + "." + middleName.substring(0,1) + "." + lastName;
		log.info("[RingCentral-sama] sAMAccountName: " + sAMAccountName);
		if(!idn.accountExistsByDisplayName(applicationName,sAMAccountName))
		{
			return sAMAccountName;
		}
	}
	for(counter=1;counter<10;counter++)
	{

		if ( null != firstName && null != lastName && null != middleName )
		{
			log.info("[RingCentral-sama] Trying case 3 with: " + firstName + middleName.substring(0,1) + lastName + String.valueOf(counter));
			sAMAccountName =  firstName + "." + middleName.substring(0,1) + "." + lastName + String.valueOf(counter);
			log.info("[RingCentral-sama] sAMAccountName: " + sAMAccountName);

		}

		if(!idn.accountExistsByDisplayName(applicationName,sAMAccountName))
		{
			return sAMAccountName;
		}

	}
	return null;
}
]]></Source>