anteo / redmine_custom_workflows

Allows to create custom workflows for Redmine
http://www.redmine.org/plugins/custom-workflows
GNU General Public License v2.0
178 stars 72 forks source link

Access custom field data #318

Closed JGallot closed 1 year ago

JGallot commented 1 year ago

Hello,

I've got a little question. In my issue, i've got a custom field which is User TYpe.

I can acces the value(id) of the User selected. I can get find the User object thanks to the find_by_id() method.

But i don't undertand (maybe too tired ;) ) why i can't access to User.mail or User.login values.

code example:

id_beneficiaire=322
id_selected_beneficiaire = custom_field_value(id_beneficiaire)
u=User.find_by_id(id_selected_beneficiaire)

raise RedmineCustomWorkflows::Errors::WorkflowError, u.login

Error Message when trying to save custom workflow is : Workflow script executable before saving observable object contains error: undefined method 'login' for nil:NilClass

But if a replace raise RedmineCustomWorkflows::Errors::WorkflowError, u.login by raise RedmineCustomWorkflows::Errors::WorkflowError, u.inspect i can save and obtain object infos.

Any ideas ?

AirTibu commented 1 year ago

Hi,

You should manage the potential nil? values. Use this code to get mail address and login name:


selected_user = User.find_by_id(custom_field_value(322).to_i) unless custom_field_value(322).nil?

user_login = selected_user.login  unless selected_user.nil?
user_mail = selected_user.mail unless selected_user.nil?

Note: nil is a special Ruby object used to represent an “empty” or “default” value. If your custom field is not selected, then you got nil (empty) value, and the User.find_by_id method couldn't work, because it has no id value.

The issue is same, when you try to access to login and mail attributes. If the User.find_by_id did not find any user, the selected_user value will be nil, then you cannot access to empty.mail or empty.login.

I hope it helps!

JGallot commented 1 year ago

Hi, Effectivly, i was a bit lazy... yes it helps, it sound good now ! Thanks