Skip to main content

Greetings,

I have been pulling my hair out trying to find the right/best way to do something that seems simple on the surface. I need take the value of the Time Entry field “time spent” which unfortunately is stored as a String, and turn it into a number so I use it to then calculate against an “hourly rate” field associated with an Agent. The only part of this whole process I cannot get to work is the String to Decimal conversion.

 

time_spent values are stored as a String like: “02:30” which means 2 hours and 30 minutes. Because of this I am currently using substring to split the values into the Hours and Minutes portions so I can then divide the minutes by 60 to get a decimal. I then need to add both back together.

 

The closest I have come to making this work is:

abs(substring({{P4.root.time_entry.time_spent}}, 0, 2)) + abs(substring({{P4.root.time_entry.time_spent}}, 3, 5)) / 60

 

This makes absolute values from the String portions xx:yy respectively and adds them together. It appears to work as I can use the Test Expression to get the correct result below:

 

As you can see, the test returns the result I want. However, I cannot save as I am getting the “Invalid Syntax - One of its operands is invalid” error. Is this because despite the abs() accepting ‘value’ the systems just simply does not like this hacky way of doing String conversion? There doesn’t appear to be a function to actually change the type such as a toString or toDecimal or something. I’ve dabbled a little in trying to use Liquid Filters, but those don’t seem to work within the Expression Builder (or I’m an idiot). If the answer is that I need to use an Add Note action to make a Liquid Filter that will make a variable and store the value just to use API to write that value back to a custom field, I will. I just need to know if that will actually work because so far what can be done with Liquid vs Expression appears to be nearly the same.

 

Please help. Any ideas are welcome.

 

(Or if someone from Freshservice sees this, please slap the developer that decided to store a number of hours as string)

This looks like it’s working 

{{P4.root.time_entry.time_spent | split: ':' | last }} / 60 + {{P4.root.time_entry.time_spent | split: ':' | first }}

 


Thank you so much ​@Daniel Söderlund! I’m a newb and not very familiar with the Liquid syntax, but your solution that uses split and avoids using substring which forces the data type worked like a charm!

I still have much to learn, but so far this community has been awesome!