substringAfterLast() empty result
Asked by Rouven Hillenbrand:

Hello,

I want create a Task in a Software project, after a Change Request in a Service Management project reached a specific status.

Also, I created a custom drop-down field "Epic Parent", which value is selected in the Change Request and is used to assign the later created Task to an Epic. The values of this custom field have the format "\[Epic Name\] \*\[Epic Key\]", like "Application Log \*TEST-137".

My problem is now, that I can't get the needed Epic Key "TEST-137" from this string.

This JSON:

![2021-11-02 17_37_46-Window.png](https://community.atlassian.com/t5/image/serverpage/image-id/167637i8020EC6CE69482FA/image-size/large?v=v2&px=999 "2021-11-02 17_37_46-Window.png")

Results in:![2021-11-02 17_37_25-Window.png](https://community.atlassian.com/t5/image/serverpage/image-id/167638i5A5B3FC73EBA17AA/image-size/large?v=v2&px=999 "2021-11-02 17_37_25-Window.png")

Unfortunately, this is my first time working with JSON \& smart values. I tried the same statement without the substringAfterLast() function, which gives me the expected result of the whole field value. Also for testing purpose, I tried the substringAfter() function and with simple letters, instead of the "\*", but all seems to result in an empty return.

Am I using the function wrong, or is there another way to get the Epic Key out of my string?


Answers:
author: Bill Sheboy
body: Hi [@Rouven Hillenbrand](/t5/user/viewprofilepage/user-id/1217011)

Some things I wonder:

1. If you just write your custom field to the audit log, does it have the value you expect?
2. Have you tried accessing the custom field using its ID value instead?
3. Have you tried to not use the JSON edit, and just edit the Epic Link and type in the smart value? (To do this, it will appear below the field, and then you may select it.)
4. Perhaps that asterisk is being treated as a wildcard. After confirming the custom field has a value, try extracting just the numeric part and write it to the audit log. That would confirm your delimiter is the problem.

{{issue.Epic Parent.substringAfterLast("TEST")}}

Kind regards,  
Bill

comments: author: Rouven Hillenbrand
body: Hi Bill,

thanks a lot for your response!

1. Yes, if I work with "Epic Link": "{{issue.Epic Parent}}", I get the value I expect:

*Error creating issue*   
*Epic with key 'Test 4 \*DTP-21' does not exist., Specify a valid value for the Epic Link (customfield_10002)*

If I now try to just get the needed part "DTP-21" from this string, like with "Epic Link": {{issue.Epic Parent.substringAfterLast("\*")}}, it seems that I get an empty return:  
*Error creating issue*   
*Epic with key '' does not exist., Specify a valid value for the Epic Link (customfield_10002)*

2. Yes, exact the same behavior on every results if I use "customfield_10485" instead of "Epic Parent".

3. I haven't known that this is possible so far, I thought that's why JSON is there ;). I gave it a try with:

![2021-11-03 13_30_43-Project automation - Jira and 5 more pages - Work - Microsoft? Edge.png](https://community.atlassian.com/t5/image/serverpage/image-id/167811iABA00480AD97A6B3/image-size/large?v=v2&px=999 "2021-11-03 13_30_43-Project automation - Jira and 5 more pages - Work - Microsoft? Edge.png")

This resulted in the same first error from point 1., just like expected. If I now also here try to extract the needed Epic Key from this string, the automation runs successfully with no errors, but there is no Epic Link in the created Task. Maybe we get here also an empty return, but this doesn't cause an error here and just leave the Epic Link empty?

4. I gave it a try with different delimiter and different string separator, even with split(). I get always the same JSON error mentioned in 1., and always the same behavior if I use just the smart value mentioned in 3.

I even gave it a try with "Epic Link": "{{issue.customfield_10485.length()}}" which should return the error:   
*Error creating issue*   
*Epic with key '14' does not exist., Specify a valid value for the Epic Link (customfield_10002)*

But also this function seems to return an empty result, causing this error instead again:  
*Error creating issue*   
*Epic with key '' does not exist., Specify a valid value for the Epic Link (customfield_10002)*

Is it possible, that these functions doesn't even work with strings from (my) drop-down Custom Field?


author: Rouven Hillenbrand
body: My last sentence made me try it with another field with JSON. It all works fine with the standard Summary field.

Then I tried fields like Request Type, Components, Urgency, Approvers which all didn't worked. So I created a custom single line text field, and it worked fine just as the Summary field.

**To sum it up**: {{issue.Field Name}} works fine for me to get a value of any field of an issue, but if I want to use a function like {{issue.Field Name.length()}}, it needs to be an text field to work.

I could have saved the time by reading, that these smart values are (unfortunately) only meant for text fields: [Smart values - text fields \| Jira Software Cloud \| Atlassian Support](https://support.atlassian.com/jira-software-cloud/docs/smart-values-text-fields/)

So is there a way to extract part of strings from other fields, like in my case drop-downs?


author: Bill Sheboy
body: Hi [@Rouven Hillenbrand](/t5/user/viewprofilepage/user-id/1217011)

* Yes, length() only works for string (text) values
* For other fields with more structure (e.g. assignee) you need to get a text attribute first; otherwise you usually get the object...which renders as null. For example, {{issue.assignee.displayName.length()}}
* Your field is a single-select, drop-down list, so you need the value attribute

I did not see a post with the result I was expecting from the audit log write, so let's try this:

1) Please post an image of your complete rule, as you are currently trying it.

2) Please create another short rule to test your field contents:

* trigger: manual
* action: audit log the value of {{issue.customfield_10485}}
* action: audit log the value of {{issue.customfield_10485.value}}

Then please run rule #2 with an issue which has the field set and post the audit log results.

Thanks!


author: Rouven Hillenbrand
body: Hi Bill,

thanks, that it! So all I needed was to add .value to solve my problem:

{{issue.Epic Parent.value.substringAfterLast("\*")}}

Thanks a lot!


author: Bill Sheboy
body: Awesome! I am glad to learn that worked.

__Bill


Tags: json

smart-value