anteo / redmine_custom_workflows

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

How to notify custom user? #216

Closed benitapetrosiute closed 3 years ago

benitapetrosiute commented 3 years ago

Hi all,

Maybe someone knows how to configurate email notification based on custom field value?

A custom field list from users has been created and we want the user selected in the task get an email.

AirTibu commented 3 years ago

Hi,

You can use this code:

Do not forget to replace the CF_ID to your custom field id!


unless custom_field_value(CF_ID).nil?
  custom_field_value(CF_ID).each do |user_id|
     mailuser= User.find_by_id(user_id)
    @html = "<b><p><strong>Dear #{mailuser.firstname} #{mailuser.lastname},</strong></p>
    <p>Please be informed, that...</p>
    <p>Please check your task in Redmine system!</p>
    </b>".html_safe

   CustomWorkflowMailer.deliver_custom_email(mailuser, "THIS IS THE MAIL SUBJECT: Redmine ID: #{@issue.id}", @html )
  end
end

I hope it helps!

bhetsko commented 2 years ago

We have used this code successfully as written, however it has the unfortunate effect of resending the email every time the issue is changed and saved. Is there a way to only send a notification the first time that custom user field is selected or changed?

AirTibu commented 2 years ago

Hi,

Use this code. This code is running, if custom field value changed.

BEFORE_SAVE


@notify_users = Issue.find(@issue.id).custom_field_value(CF_ID).uniq.sort != @issue.custom_field_value(CF_ID).uniq.sort

AFTER_SAVE


if @notify_users
  custom_field_value(CF_ID).each do |user_id|
     mailuser= User.find_by_id(user_id)
    @html = "<b><p><strong>Dear #{mailuser.firstname} #{mailuser.lastname},</strong></p>
    <p>Please be informed, that...</p>
    <p>Please check your task in Redmine system!</p>
    </b>".html_safe

   CustomWorkflowMailer.deliver_custom_email(mailuser, "THIS IS THE MAIL SUBJECT: Redmine ID: #{@issue.id}", @html )
  end
end

Note: Do not forget to change the CF_ID.

I hope it helps!

bhetsko commented 2 years ago

Thank you. I have replaced the CF_ID with the correct ID number.

I think I am close but I now get the error when I save this workflow. "Workflow script executable before saving observable object contains error: Couldn't find Issue without an ID"

AirTibu commented 2 years ago

Hi,

Change the BEFORE SAVE section to this:


@notify_users = Issue.find(@issue.issue_id).custom_field_value(CF_ID).uniq.sort != @issue.custom_field_value(CF_ID).uniq.sort
bhetsko commented 2 years ago

I changed to this: @notify_users = @.***_id).custom_field_value(217).uniq.sort != @issue.custom_field_value(217).uniq.sort

and now I get:

Everything looks right so I wonder if there is something wrong on the back end. All of the REST stuff looks ok.

-BH

On Thu, Aug 25, 2022 at 2:56 PM AirTibu @.***> wrote:

Hi,

Change the BEFORE SAVE section to this:

@notify_users = @.***_id).custom_field_value(CF_ID).uniq.sort != @issue.custom_field_value(CF_ID).uniq.sort

— Reply to this email directly, view it on GitHub https://github.com/anteo/redmine_custom_workflows/issues/216#issuecomment-1227644854, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALEWERUWFI3GGMCPCMCEHRTV266W3ANCNFSM44YSQ56Q . You are receiving this because you commented.Message ID: @.***>

AirTibu commented 2 years ago

Hi,

Yes, I forget, to test it for new_record (new record has no id...). Try this code:


@notify_users = Issue.find(@issue.id).custom_field_value(CF_ID).uniq.sort != @issue.custom_field_value(CF_ID).uniq.sort if @issue.id
bhetsko commented 2 years ago

That did it! Thanks, works like a charm. Now on to the next thing I can break.

bhetsko commented 1 year ago

For some reason I now get an error with this workflow element.

== Custom workflow exception: undefined method 'uniq' for "117":String in the production.log

The @notify_users... code provided above worked fine for a while so I'm not sure what may have changed. Appreciate any help.