Padleft uniqueCounter in username transform

I need to pad the uniqueCounter but cannot figure out how to, instead of 1 I need 001.

{
    "name": "sAMAccountName",
    "transform": {
        "type": "usernameGenerator",
        "attributes": {
            "sourceCheck": true,
            "patterns": [
                "$fi$ln$counter"
            ],
            "fi": {
                "type": "identityAttribute",
                "attributes": {
                    "name": "firstInitial"
                }
            },
            "ln": {
                "type": "lower",
                "attributes": {
                    "input": {
                        "type": "identityAttribute",
                        "attributes": {
                            "name": "lastname"
                        }
                    }
                }
            },
            "counter": {
                "type": "leftPad",
                "name": "counter",
                "attributes": {
                    "padding": "0",
                    "length": "3",
                    "input": "$(uniqueCounter)"
                }
            }
        }
    },
    "attributes": {
        "cloudMaxUniqueChecks": "50",
        "cloudMaxSize": "20",
        "cloudRequired": "true"
    },
    "isRequired": false,
    "type": "string",
    "isMultiValued": false
}

Hi @KyleHewitt0,

Welcome to the Sailpoint Developer Community.

Can you try this :

{
    "name": "sAMAccountName",
    "transform": {
        "type": "usernameGenerator",
        "attributes": {
            "sourceCheck": true,
            "patterns": [
                "$fi$ln$counter"
            ],
            "fi": {
                "type": "identityAttribute",
                "attributes": {
                    "name": "firstInitial"
                }
            },
            "ln": {
                "type": "lower",
                "attributes": {
                    "input": {
                        "type": "identityAttribute",
                        "attributes": {
                            "name": "lastname"
                        }
                    }
                }
            },
            "counter": {
                "type": "leftPad",
                "attributes": {
                    "padding": "0",
                    "length": "3",
                    "input": {
                        "type": "static",
                        "value": "$uniqueCounter"
                    }
                }
            }
        }
    },
    "attributes": {
        "cloudMaxUniqueChecks": "50",
        "cloudMaxSize": "20",
        "cloudRequired": "true"
    },
    "isRequired": false,
    "type": "string",
    "isMultiValued": false
}

I’m sure if the uniqueCounter variable is available for calculate counter but you can try.

1 Like

It’s giving me this error. I was playing around with it but can’t manage to get around it.

“trackingId: [alphaNumberic] java.lang.RuntimeException: sailpoint.tools.GeneralException: Invalid get reference: $counter - seaspray[line 1, column 7]”

Hello Kyle,
can you update “value”: “$uniqueCounter” to “value”: “${uniqueCounter}” and test.
Please let me know.
Thanks
Santosh

It gets the same results for the following attempts:
$uniqueCounter
$(uniqueCounter)
${uniqueCounter}

"counter": {
    "type": "leftPad",
    "attributes": {
        "padding": "0",
        "length": "3",
        "input": {
            "type": "static",
            "value": "${uniqueCounter}"
        }
    }
}

Any other ideas are appreciated!

What error message you are getting?

trackingId: b4cbf42f07094650a7b3f3bde8ccbc15 java.lang.RuntimeException: sailpoint.tools.GeneralException: Invalid get reference: $counter - seaspray[line 1, column 7]

@KyleHewitt0 to achieve your usecase you must use a custom attribute generator rule.

Here an example getting from Sailpoint documentation:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE Rule PUBLIC "sailpoint.dtd" "sailpoint.dtd">
<Rule name="UsernameGenerator" type="AttributeGenerator">
    <Description>Generate a unique username for Active Directory.</Description>
    <Source><![CDATA[

    import sailpoint.tools.GeneralException;
    import sailpoint.object.*;
    import java.util.ArrayList;
    import sailpoint.api.*;
    import java.util.List;
    import org.apache.commons.lang.StringUtils;
    import java.text.Normalizer;

    int MAX_USERNAME_LENGTH = 12;

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

        if(firstName != null) {
            firstName = (Normalizer.normalize(firstName, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", ""));
            firstName = firstName.replaceAll("[^a-zA-Z0-9]", "");
        }

        if(lastName != null) {
            lastName = (Normalizer.normalize(lastName, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", ""));
            lastName = lastName.replaceAll("[^a-zA-Z0-9]", "");
        }

        if((firstName == null) || (lastName == null)) {
            log.debug( "AD Create User Name | Exit from generateUsername method. No last name and first name for user" );
            return null;
        }

        String username = null;
        String fullName = firstName + "." + lastName;

        if(fullName.length() > MAX_USERNAME_LENGTH) {
            int firstNameLength = firstName.length();

            if(firstNameLength > (MAX_USERNAME_LENGTH - 2)) {
                for(int lastNameLength = 0; lastNameLength < lastName.length(); lastNameLength++) {
                    username = firstName.substring(0, (MAX_USERNAME_LENGTH - 2)) + "." + lastName.charAt(lastNameLength);
                    username = username.toLowerCase();
                    if (isUnique(username)) {
                        log.debug( "AD Create User Name | Unique username generated: " + username);
                        log.debug( "AD Create User Name | Exit from the  GenerateUsername Method" );
                        return username;
                    }
                }
            } else {
                for(int lastNameLength = 0; lastNameLength < lastName.length(); lastNameLength++) {
                    username = firstName + "." + lastName.charAt(lastNameLength);
                    username = username.toLowerCase();
                    if (isUnique(username)) {
                        log.debug( "AD Create User Name | Unique username generated: " + username);
                        log.debug( "AD Create User Name | Exit from the  GenerateUsername Method" );
                        return username;
                    }
                }
            }
        } else {
            username = fullName;
            username = username.toLowerCase();
            if (isUnique(username)) {
                log.debug( "AD Create User Name | Unique username generated: " + username);
                log.debug( "AD Create User Name | Exit from the  GenerateUsername Method" );
                return username;
            } else {
                for(int lastNameLength = 0; lastNameLength < lastName.length(); lastNameLength++) {
                    username = firstName + "." + lastName.charAt(lastNameLength);
                    username = username.toLowerCase();
                    if (isUnique(username)) {
                        log.debug( "AD Create User Name | Unique username generated: " + username);
                        log.debug( "AD Create User Name | Exit from the  GenerateUsername Method" );
                        return username;
                    }
                }
            }
        }



        return null;
    }

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

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

  ]]></Source>
</Rule>

If you are specifically after a 3 digit counter, you can add a 2-digit random number, then a do the counter, but limit that to 9 checks:

 "attributes": {
   "template": "$(firstname).$(lastname)$(rndNumber)$(uniqueCounter)",
   "cloudMaxUniqueChecks": "9",
   "cloudRequired": "true"
 },

Something like that

Hello @KyleHewitt0

did you get around to solving this. I encountered the same issue. In my case i used an attributeGenerator rule to generate a conter i can use where necessary. rule returns an empty string if counter = 0 or the counter in as a string. the same rule works if i use it to generate sAMAccountName, but i get an error when i run it to get a uniquecounter.

Any help will be appreciated.

Thank you

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