Create account generator sometimes has errors

When trying to create a new user in SalesForce, one of the requirements is generating a value for the Alias field.

Here are the SalesForce requirements for that field

For the Create profile, I’m just taking the first 8 characters of the identityAttribute uid

{
            "name": "Alias",
            "transform": {
                "name": "SalesForce Username Generator",
                "type": "static",
                "attributes": {
                    "salesForceUsername": {
                        "attributes": {
                            "name": "uid"
                        },
                        "type": "identityAttribute"
                    },
                    "value": "$salesForceUsername.substring(0,8)"
                }
            },
            "attributes": {},
            "isRequired": false,
            "type": "string",
            "isMultiValued": false
        }

This seems to work fine for some users, but I sometimes see this error when it’s trying to create an account and it’s not super helpful… any ideas?

 "errorMessages": [
            [
                {
                    "locale": "en-US",
                    "localeOrigin": "DEFAULT",
                    "text": "An unexpected error occurred: Error rendering template: $salesForceUsername.substring(0,8)"
                }
            ]
        ]

The KB article I found isn’t super helpful

Its probably because sometimes your uid could be null/empty or less than 8 characters.

1 Like

Null probably won’t happen, but fewer than 8 characters might. I need to look at my non-prod environments and see if I can reproduce this. I think it worked fine with my username (mcheek), but I would have to check.

Is this a situation where you would have to check the length of the uid and if it’s < 8, plug it into the substring?

Yes, that would be way to go. You can achieve that with a combination of Get End of String, First Valid and Sub String transforms.

Could this also be done using Velocity?

{
            "name": "Alias",
            "transform": {
                "name": "SalesForce Username Generator",
                "type": "static",
                "attributes": {
                    "uid": {
                        "attributes": {
                            "name": "uid"
                        },
                        "type": "identityAttribute"
                    },
                    "value": "#set($uidLength = $uid.length()) #if($uidLength < 8) $uid.substring(0,$uidLength) #else $uid.substring(0,8) #end"
                }
            },
            "attributes": {},
            "isRequired": false,
            "type": "string",
            "isMultiValued": false
        }

Yes, it can be done using Velocity.
Tested this and it works

{
    "type": "static",
    "attributes": {
        "salesForceUsername": {
            "attributes": {
                "name": "uid"
            },
            "type": "identityAttribute"
        },
        "value": "#if($salesForceUsername.length()<8)$salesForceUsername.substring(0,$salesForceUsername.length())#{else}$salesForceUsername.substring(0,8)#end"
    },
    "internal": false
}
2 Likes

Thanks @atarodia, that worked!

1 Like

could you not simplify this line

"value": "#if($salesForceUsername.length()<8)$salesForceUsername.substring(0,$salesForceUsername.length())#{else}$salesForceUsername.substring(0,8)#end"

as

"value": "#if($salesForceUsername.length()<8)$salesForceUsername#{else}$salesForceUsername.substring(0,8)#end"
1 Like

@iamnithesh Yes, it does makes sense. Thank you

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