Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert a calculated numeric value into a (different) string value

Dan Kane January 30, 2024

Hi All,

I'm struggling with something that should have been very easy, and it probably is, but I'm just not seeing it :(  I'm also sure someone has probably asked this question before, but I can't seem to find it.  So, here I am.

I have an automation rule that generates an email triggered by a schedule, which queries some issues.

To keep this simple, all I want to do is determine if a number is odd or even and write the string "odd" or "even."

I started with the following in the content of my email:

{{#issues}}
Issue index = {{index}}
{{/}}

This returns just what I expected, a list of issue index values:

Issue index = 0
Issue index = 1
Issue index = 2
Issue index = 3

In order to determine if the index is odd/even, I do some math:

{{#issues}}
Issue {{index}} = {{#=}}({{index}} % 2){{/}}
{{/}}

The results:

Issue 0 = 0
Issue 1 = 1
Issue 2 = 0
Issue 3 = 1

So far, so good!  Now I just need to evaluate the odd/even numeric value right? (only evaluating odd to keep it simple)

{{#issues}}
Issue {{index}} = {{#if(equals({{#=}}({{index}} % 2){{/}},1))}}Odd{{/}}
{{/}} 

 Unfortunately, this is where I hit the wall and get the error:

Error rendering smart-values when executing this rule:
Mismatched start/end tags: null != in template-11fdf479-9202-4944-accc-
bca5fa38ba64:4: {{#issues}} Issue {{index}}
= {{#if(equals({{#=}}({{index}} % 2){{/}},1))}}Odd{{/}} {{/}}

I have tried every permutation, method, conversion, and prayer that I can think of and nothing gets me the simple little word of "odd."

Where am I going wrong!?  THANK YOU!

 

3 answers

2 accepted

2 votes
Answer accepted
Dan Kane January 31, 2024

So after a bunch of Red Bull I came up with a work around using the RGB method... So if you are looking to have alternating row colors in your table, simply add the following to your <tr> tag:

<tr style="background-color: rgb({{#=}}IF({{index}}%2, 243, 208){{/}},{{#=}}IF({{index}}%2, 246, 224){{/}},{{#=}}IF({{index}}%2, 244, 227){{/}});"> 

 Explanation:

2024-01-31_11-43-44.png

Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2024

Thanks, Dan!  That's great!

I did not know the modulo operator could be used in a stand-alone condition.  Must be the math operation wrapper that allows that, and it certainly opens up possibilities for the other operators.

1 vote
Answer accepted
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2024

Hi @Dan Kane -- Welcome to the Atlassian Community!

Index cannot be used in functions (or apparently conditions) within the iterator.  There is a defect for this in the public backlog for Jira Cloud automation, although it is unclear what the usage scenario was to drive that defect: https://jira.atlassian.com/browse/AUTO-1087

What is the problem you are trying to solve?  Knowing that may help the community to suggest alternatives.

I recall an edge-case scenario from a couple of years ago, where a customer wanted to find a specific numbered item in a list.  Index did not help and once inside of an iterator, no other scope data is visible.  The work-around was to expand the list into a created variable, and then use inline iteration and the match function to dynamically find the item.  Again...this was an edge-case.

Kind regards,
Bill

Dan Kane January 30, 2024

Thank you for the info @Bill Sheboy!  I had hoped the Jira god would answer!  ;)

I feared a bug would be the cause.   It is odd though that I can use the index in a math computation, but not pass the result (even when converted to a string) into a simple IF conditional.

The use case is extremely basic. The email produced contains a table of issues, which we would like to see with alternating row colors.  Since the target client is Outlook, everything must be done inline as there's no JavaScript or Head access for css.

Best regards and thank you again,

~Dan

Like Bill Sheboy likes this
Bill Sheboy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 30, 2024

Yup, that is a good use case :^)

How about a work-around...if you do not care about consistency of the first row being the same color?

Try using the issue's id value instead, with inline math functions:

{{#lookupIssues}}{{#if(equals(id,id.divide(2).floor.multiply(2)))}}issue {{key}} has an even id of {{id}}; {{/}}{{/}}

This works around not having a modulo operator for inline math functions.

 

Oops...that was an unhelpful idea as there is no guarantee of alternating issue id values in the result set of issues!

 

0 votes
Aron Gombas
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2024

I don't know about the possibilities in Outlook, but if you can add this single CSS rule  to your email somehow, then you won't need to work with the index at all and CSS will take care of finding odd/even indices.

https://www.w3schools.com/howto/howto_css_table_zebra.asp

Dan Kane January 31, 2024

@Aron Gombas Unfortunately, though the preferable solution, you're unable (as far as I know) to modify the <head> tag within the Outlook message in order to insert the CSS required.

Aron Gombas _Midori_
Community Champion
January 31, 2024

@Dan Kane I saw that you finally managed to solve the problem with the Handlebars expression, great work!

Also, let me note that most (all major?) browser accept the <style> tag even within the <body>. I don't know about Outlook, but it is rather likely that it would work with that, too.

Suggest an answer

Log in or Sign up to answer