annikoff/redmine_plugin_computed_custom_field

Retrieve Value from custom field key/value List

smahou98 opened this issue · 6 comments

Hi,
I tried to create a computed custom field with the value of another key/value type custom field. I can retrieve the key but I don't know how to get the value.
Please can you help me?

Same problem here :)

Hi. Sorry for the delayed response. For example if id of key/value custom field is 1: cfs[1].try(:id) will return id of selected value, and cfs[1].try(:name) will return name of selected value.
I will cover this case in README.

Thank's :-)
I try your solution and it works fine for one case but not for another
The formula above is from a text custom_field (name XXX) (id=50):

if self.tracker_id==9 
   self.cfs[47].try(:name)
else
   'not avalaible'
end

Work's fine for issues with tracker_id=9 but for issues with another tracker_id where the custom_field is not available it crash
Error while formula computing in field "XXX" — undefined method "first" for nil:NilClass
I try the expression

if available_custom_fields.map(&:id).include?(47)
   self.cfs[47].try(:name)
else
   'not avalaible'
end

but the result is the same.

How do I use the method .try(:name) if I want to retrieve the value of root issue?

Can you help me with this?

Can't reproduce this.
This code self.cfs[47].try(:name) will cause of the following error Error while formula computing in field "XXX" — : undefined method 'cfs' for #<Issue:.

You should use cfs without self, like this cfs[47].try(:name)

What do you mean by "to retrieve the value of root issue"?

I try to explain my situation (sorry for my English)
I have two differentes trackers tracker A and tracker B. In tracker A is available the cfs[47] (key/value list not computed) but not in tracker B. I create a new cfs[50] available for tracker A and B, text type and computed with the formula:

if self.tracker_id==A 
   cfs[47].try(:name)
else
   'not avalaible'
end

When I save and issue with tracker A it works fine and in cfs[50] the result is the element name, but when I save and issue with tracker B (the result must B 'not avalaible') it crash with the message:
Error while formula computing in field "XXX" — undefined method "first" for nil:NilClass

The other question is how to retrieve the value from another issue. In other formulas when I have to retrieve the value of the parent issue (or root issue) the formula is root.try(:custom_field_value, 21) but I don't know how to do it if field 21 is Key/value type.

I'm sorry, but I'm really can't reproduce this. The formula works fine.
To get root issue custom field try this root.try(:custom_field_value, 21).try(:name) or root.try(:custom_field_value, 21).try(:id)