Substring Tansform

I have a substring transform that works great if the department length is over 28 characters but if it is under that number it errors out. Is there a way to do it so it takes 28 or less

{
        "id": "bf150b23-0352-42da-8d32-20666c17a980",
        "name": "Department Substring Transform",
        "type": "substring",
        "attributes": {
            "input": {
                "attributes": {
                    "name": "department"
                },
                "type": "identityAttribute"
            },
            "end": 28,
            "begin": -1
        },
        "internal": false
}

There are a few ways you can solve for this.

You could use the Right Pad transform to ensure a minimum length, then use your Substring transform, and finally a Replace All or Trim transform to remove the padding.

Alternatively, you can use a Split transform with a Regular Expression to select the first 28 characters as the delimiter.

Personally, I would probably use the Split transform.

1 Like

Hello @krigney,

Blaise’s suggestions would work great. I have another way using the static transform and velocity that may look a little more readable.

{
  "type": "static",
  "attributes": {
    "deparment": {
      "type": "static",
      "attributes": {
        "value": "thisisatestwithalongstringofchars"
      }
    },
    "value": "#if($deparment.length() > 28)$deparment.substring(0,28)#else$deparment#end"
  }
}
4 Likes

I personally try to avoid static transforms with velocity logic whenever possible because they are easy to make mistakes with, hard to debug, and tend to fail in unexpected ways.

For example, in the above transform if the “department” attribute is blank you would expect the end result to be a blank but it actually gives an error and puts your identity into an error state.

Here’s the approach we took with a firstValid:

{
    "type": "firstValid",
    "attributes": {
		"values": [
			{
				"type": "substring",
				"attributes": {
					"begin": 0,
					"end": 28,
					"input": {
						"type": "identityAttribute",
						"attributes": {
							"name": "department"
						}
					}
				}
			},
			{
		        "type": "identityAttribute",
				"attributes": {
					"name": "department"
				}
			}			
		]
	}
}

4 Likes

Thank you so much… That totally solved my issue. I would have never thought to use a firstValid

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