Workflow - 'Substring' Operator within 'Define Variable' Operator

Hi All …

I have a use case where I need to retrieve description of a dynamic entitlement and then email it to the user. The description is the network file share path. The description text varies as some entitlements’ description do have “ITSM-12345” text at the start and some don’t have it. For example;

  1. ITSM-12345-\\network file share path
  2. \\network file share path

The ‘ITSM’ text is a static value but the integer part (e.g 12345 or 67894) is a dynamic value, so here I can’t use ‘Replace’ operator to replace ‘ITSM-12345’ with some other text or output. The aim is to email description of entitlement (network file share path) but without “ITSM-12345” part if it has it.

Proposed Solution:

Step1: Use ‘Get Index’ operator to find the location of double backslashes (\\) where the network file share path starts. After lots of testing, I found that ‘Get Index’ operator doesn’t work as expected. I’ve already created another post about this. Workflow - Issue with ‘Get Index’ Operator in Define Variable Operator

Step2: Once the ‘Get Index’ operator issue is resolved, then use ‘Substring’ operator within Define Variable to retrieve the desired text i.e only return the network file share path (without the ‘ITSM-12345’). After some testing, I found I can’t get the desired result because of the below scenarios.

Note: The description length is unknown.

Use case1 - (where network file share path doesn’t have ‘ITSM-12345’ text at the start and the path starts with double backslashes)
start value: 0
Length value: blank as it’s unknown (dynamic)
Output result: blank/null value (not getting the desired outcome)

Use case2
Start value: 0
Length value: 100 (if this value is greater than expected length value, then workflow throws an error saying invalid length provided as shown in attached screenshot). If it’s less than the expected length value then it doesn’t return full path i.e some text is missing at the end as expected.
Output result: Not getting the desired outcome

Use case3
Start value: 1 or 2 or any other integer except 0
Length value: blank as it’s unknown (dynamic)
Output result: Getting the desired result (but this option doesn’t work if network file share path starts without ‘ITSM-12345’ text at the start i.e start value is 0 as explained above in use case1).

I am not sure if it’s possible for the Workflows support team to make the Length value optional i.e if it’s blank then returns full text regardless the start value is 0 or or any other integer.

OR, please let me know if the above can achieved in a different way.

Thanks
Substring_error

Any input on this workflow would be greatly appreciated.

Thanks very much!

Hello @nhassan,

I was able to achieve this using two Define Variable operators. The first uses the getIndex operator to find the location of the first backslash. The Pattern regex used was [\\\\]

You will have to use the advanced tab as described here to change the patternGetIndex key to Pattern to get around the current UI bug.

The second Define Variable uses the result of the first variable to start at that index for the substring operator, by default the UI will create a length key. Using the Advanced tab you can remove that length key which tells the substring operator to return the rest of the string after the start index.

At this point I had a string with \\ description text. I used another substring with start:1 to remove the two slashes off the begging of the string.

TylerTest20231211

Full Workflow:

{
	"name": "Tyler Test",
	"description": "",
	"modified": "2023-12-11T19:04:36.453421602Z",
	"modifiedBy": {
		"type": "IDENTITY",
		"id": "2c918089796ba06c01798018af8d4161",
		"name": "tyler.mairose"
	},
	"definition": {
		"start": "Define Variable",
		"steps": {
			"Define Variable": {
				"attributes": {
					"id": "sp:define-variable",
					"variables": [
						{
							"description": "",
							"name": "Index",
							"transforms": [
								{
									"id": "sp:transform:getIndex:int",
									"input": {
										"Pattern": "[\\\\]"
									}
								}
							],
							"variableA.$": "$.trigger.requestedItemsStatus[0].name"
						}
					]
				},
				"nextStep": "Define Variable 1",
				"type": "Mutation"
			},
			"Define Variable 1": {
				"attributes": {
					"id": "sp:define-variable",
					"variables": [
						{
							"description": "",
							"name": "Substring",
							"transforms": [
								{
									"id": "sp:transform:substring:string",
									"input": {
										"start.$": "$.defineVariable.index"
									}
								},
								{
									"id": "sp:transform:substring:string",
									"input": {
										"start": 1
									}
								}
							],
							"variableA.$": "$.trigger.requestedItemsStatus[0].name"
						}
					]
				},
				"nextStep": "End Step — Success",
				"type": "Mutation"
			},
			"End Step — Success": {
				"type": "success"
			}
		}
	},
	"creator": {
		"type": "IDENTITY",
		"id": "2c918089796ba06c01798018af8d4161",
		"name": "tyler.mairose"
	},
	"trigger": {
		"type": "EVENT",
		"attributes": {
			"id": "idn:access-request-post-approval"
		}
	}
}

Hi @tyler_mairose

Thanks for your help with this.

There are two senarios as mentioned below i.e description text is dynamic e.g

1: ITSM-12345-\\network file share path
2: \\network file share path

Your suggested solution worked fine but only for the above 1st scenario, where the double backslashes are in the middle of string. It retuns the description text but only with single backslash at the start, which is fine as I had to do a workaround for this (including another single backslash in the email body), which all looks good.

It doesn’t work for the above 2nd scenario, where the double backslashes are at the start of string. It throws an error message as shown in the attached screenshot.

I tried changing the start value of substring in the advanced editor but without any luck.

Can you please advise what values should I set for the substring(s) to make the workflow working for both scenarios and this should return the description text including the double backslashes at the start?



Hey @nhassan,

My bad, I got a little excited that it worked for the first case and forgot to test on the second scenario :slight_smile:

This may be another bug in the getIndex operator, but when the start location is 0 it does not correctly calculate the end or length of the substring. I tried getting around this by passing the $.hTTPRequest.body[0].description.length() which would get us to the end of the string. This length() property works on some implementations of jsonPath but apparently does not on ours within workflows.

To workaround this issue I added in a Compare Numbers check if the index retrieved by the first define variable is 0 then we know that the backslash is at the beginning of the string and we can just take off the first two characters, otherwise use the index to get the substring with the backslashes and the description, then also trim the backslashes.

In the workflow below I named both variables after the Compare Numbers check to EntDescription which it seems to allow so that you wouldn’t have to use a different variable in your email to the user.

Let me know if this solves your use-case fully!

{
   "name": "Tyler Test",
   "description": "",
   "modified": "2023-12-12T19:06:27.597266339Z",
   "modifiedBy": {
   	"type": "IDENTITY",
   	"id": "2c918089796ba06c01798018af8d4161",
   	"name": "tyler.mairose"
   },
   "definition": {
   	"start": "Define Variable",
   	"steps": {
   		"Compare Numbers": {
   			"choiceList": [
   				{
   					"comparator": "NumericEquals",
   					"nextStep": "Define Variable 2",
   					"variableA.$": "$.defineVariable.index",
   					"variableB": 0
   				}
   			],
   			"defaultStep": "Define Variable 1",
   			"description": "Check whether the index is at the beginning of the string or in the middle.",
   			"type": "choice"
   		},
   		"Define Variable": {
   			"attributes": {
   				"id": "sp:define-variable",
   				"variables": [
   					{
   						"description": "",
   						"name": "Index",
   						"transforms": [
   							{
   								"id": "sp:transform:getIndex:int",
   								"input": {
   									"Pattern": "[\\\\]"
   								}
   							}
   						],
   						"variableA.$": "$.trigger.requestedItemsStatus[0].name"
   					}
   				]
   			},
   			"nextStep": "Compare Numbers",
   			"type": "Mutation"
   		},
   		"Define Variable 1": {
   			"attributes": {
   				"id": "sp:define-variable",
   				"variables": [
   					{
   						"description": "",
   						"name": "Ent Description",
   						"transforms": [
   							{
   								"id": "sp:transform:substring:string",
   								"input": {
   									"start.$": "$.defineVariable.index"
   								}
   							},
   							{
   								"id": "sp:transform:substring:string",
   								"input": {
   									"start": 1
   								}
   							}
   						],
   						"variableA.$": "$.trigger.requestedItemsStatus[0].name"
   					}
   				]
   			},
   			"nextStep": "End Step — Success",
   			"type": "Mutation"
   		},
   		"Define Variable 2": {
   			"attributes": {
   				"id": "sp:define-variable",
   				"variables": [
   					{
   						"description": "",
   						"name": "Ent Description",
   						"transforms": [
   							{
   								"id": "sp:transform:substring:string",
   								"input": {
   									"start": 2
   								}
   							}
   						],
   						"variableA.$": "$.trigger.requestedItemsStatus[0].name"
   					}
   				]
   			},
   			"nextStep": "End Step — Success",
   			"type": "Mutation"
   		},
   		"End Step — Success": {
   			"type": "success"
   		}
   	}
   },
   "creator": {
   	"type": "IDENTITY",
   	"id": "2c918089796ba06c01798018af8d4161",
   	"name": "tyler.mairose"
   },
   "trigger": {
   	"type": "EVENT",
   	"attributes": {
   		"id": "idn:access-request-post-approval"
   	}
   }
}

Hi @tyler_mairose

Happy New Year!

Sorry for the late reply as I was on leave.

Yes, it seems to be working fine now for both scenarios. Thanks very much!

I added in a Compare Numbers check - For 1st scenario where the index retrieved by the first define variable is 0 then we know that the backslash is at the beginning of the substring. As shown in the attached 1st screenshot, I did set the start location to 1 and it does correctly calculate the end or length of the substring. As expected, it removes the first backslash from the substring and leaves the second backslash as the start location. As a workaround I added another single backslash in the email body to make it double backslashes at the start of the substring as we want (i.e network file share path starts from \\)

If I set the start location to 0 then it doesn’t correctly calculate the end or length of the substring and this may be a bug in the getIndex operator.

For the 2nd scenario where the index retrieved by the first define variable is not 0 then we know that the backslash is not at the beginning of the substring. As shown in the attached 2nd screenshot, in the 1st substring operator, I did set the start location to be the index value returned. In the 2nd substring operator, I did set the start location to 1 and it does correctly calculate the end or length of the substring. As a workaround I added another single backslash in the email body to make it double backslashes at the start of the substring as we want (i.e network file share path starts from \\)

Thanks


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