Transform to Truncate a String

Hi Everyone, I don’t see a transform that can truncate a string. For example, when provisioning to the downstream system, we want to truncate certain attributes to first 60 characters.

I have tried using “substring” but it doesn’t work if value of attribute is less than 60character. I get StringIndexOutOfBoundsException.

Are there other ways to truncate a string using a Transform?

Thanks
Vijay

nevermind, this does not work after all. ignore this and scroll down for the real answer :stuck_out_tongue: (i’ve left this here as a warning that this method won’t work)

hey vijay!

you’ll want to wrap that substring transform inside a firstValid transform, where the first value is the substring transform, and your second value is the attribute without the substring.

the way this works is that the firstValid transform will skip over any value(s) that return errors, until it gets to a value that works. since your substring transform will error on a string under 60 characters, it’ll skip that and return the second value as expected.

as an example, say i was (for some reason) attempting to get the first 10 characters of the email identity attribute:

{
    "name": "ShortenString",
    "type": "firstValid",
    "attributes": {
        "values": [
            {
                "attributes": {
                    "begin": -1,
                    "end": 10,
                    "input": {
                        "attributes": {
                            "name": "email"
                        },
                        "type": "identityAttribute"
                    }
                },
                "type": "substring"
            },
            {
                "attributes": {
                    "name": "email"
                },
                "type": "identityAttribute"
            }
        ],
        "ignoreErrors": false
    }
}

hope that helps!

1 Like

ya, it ran into same issue (StringIndexOutOfBoundsException.). I wish they had out of box transform for string truncation. thank you for looking into though…

-Vijay

alright, i’m back with a fully-tested way to do this, now with more velocity!

what we’re doing here is using the apache velocity .length() and .substring() methods wrapped in an if/else statement. i also set the length as a variable in-line so that it only needs to be written one time (this way, if you want to change the max length, you don’t have to worry about accidentally missing it in one of the two spots we need it).

{
    "name": "shortenString",
    "type": "static",
    "attributes": {
        "email": {
            "attributes": {
                "name": "email"
            },
            "type": "identityAttribute"
        },
        "value": "#set($length = 60)#if($email.length() > $length)$email.substring(0,$length)#{else}$email#end"
    }
}
4 Likes

Wow!! great solution. Works like a charm. Learned a new way to use velocity. thank you @sup3rmark .

2 Likes

thanks! glad that worked for you! i’m a big fan of the extensibility of transforms leveraging velocity, it opens up worlds of additional options!

3 Likes

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