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?
nevermind, this does not work after all. ignore this and scroll down for the real answer (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:
ya, it ran into same issue (StringIndexOutOfBoundsException.). I wish they had out of box transform for string truncation. thank you for looking into though…
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).