Hello All,
I know that we can create Event that we trig on state changes in workflows.
Can we do the same kind of thing, but not at state transition, but rather when a user field is filled.
For example, we have a custom user field "Validation owner". We would like that this owner is notified when this field is set and/or updated.
I tried with the below steps:
1.Under Actions click on Edit Notifications
2.Add a new notification at "Issue Updated" event
3.Select "User custom field value" and choose the name of your user picker field from the list.
Apparently, it sends a mail to the validation owner whatever the update which is performed on the issue,
whereas I would like it sends a mail to the validation owner only when the issue is set to him thru the Validation owner field, like we have Assignee receiving notification when the issue is assigned to him.
Is this possible?
Thanks in advance,
Manikanta
For that, you'll need some sort of event listener via scriptrunner.
Listen for the "issue updated" event (or perhaps all issue event), then examine the changelog to see if the "Validation owner" has changed. Then decide what you want to do with that information.
You could use the built-in "send a custom email" listener. The check for the "Validation owner" would be in the condition field.
If you'd rather send a built-in notification, you could instead choose "fire an event when a condition is true".
Again, the code to check the changed field will go in the condition. You can then pick an event of your choice (including a custom event). Then use the Notification Scheme to set the target recipient of the notification to the "current" user in the "user custom field". At the time of the firing, the current user will be the new user.
The code would look like this (for a condition)
changeItems.any { it.field == 'Validation owner' && it.oldstring != it.newstring }
Hi Peter,
Thanks for the response!
I am new to script runner add-on and scripts. Could you please help with the Email template. What template should i put in Email Template?
We would like that this Validation owner is notified when this field is updated.
Thanks in advance,
Manikanta
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can find some examples here
https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_send_a_custom_email
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the reply again.
I have added Email Template as below:
Dear Validation Owner,
The ${issue.issueType.name} href = "${baseurl}/browse/${issue.getKey()}" has been assigned to you.
Regards,
${issue.reporter?.displayName}
after updating the issue i got notification from Jira as below:
How can i replace Validation Owner name with the custom filed user name like replacing reporter name in Email notification?
Thanks in advance,
Manikanta
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Refer to this: https://scriptrunner.adaptavist.com/latest/jira/builtin-scripts.html#_additional_configuration_in_emails
In the condition and configuration box add the following to retrieve the validation owner and store it in the config variable.
import com.atlassian.jira.component.ComponentAccessor
def validationOwnerChange = changeItems.find { it.field == 'Validation owner' && it.oldstring != it.newstring }
if(validationOnwerChange){
def validationOwner = ComponentAccessor.userManager.getUserByName(validationOnwerChange.newString)
config.validationOnwer = validationOwner
return true
} else {
return false
}
The config variable is passed to the template context and can be retrieved like this:
Dear ${validationOwner.displayName},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Looks like I made a typo ... make sure validationOwnerChange is spelled the same in all three places.
But you may still get a static type checking error. Those don't always mean that the code won't run. It's just the editor's type checker can't confirm the type of a variable to see if it matches the type the method accepts.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
changed the validationOwnerChange name correctly in all places in script. Now i don't see "Variable undeclared error". But still it shows error "Cannot find Matching method".
Below is the error in logs:
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2019-08-21 20:38:14,854 ERROR [utils.ConditionUtils]: ************************************************************************************* 2019-08-21 20:38:14,854 ERROR [utils.ConditionUtils]: Condition failed on issue PORTFOLIO1-9, built-in script:com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail java.lang.IllegalArgumentException: [GenericEntity.get] "newString" is not a field of ChangeItem at org.ofbiz.core.entity.GenericEntity.get(GenericEntity.java:246) at org.ofbiz.core.entity.GenericEntity.get(GenericEntity.java:1051) at Script247.run(Script247.groovy:6) 2019-08-21 20:38:14,858 ERROR [utils.ConditionUtils]: Script follows: import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.MutableIssue def validationOwnerChange = changeItems.find { it.field == 'Validation Owner' && it.oldstring != it.newstring } if(validationOwnerChange){ def validationOwner = ComponentAccessor.userManager.getUserByName(validationOwnerChange.newString) config.validationOwner = validationOwner return true } else { return false }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Looks like changeItem has a newstring attribute and not a newString attribute.
Again... my apologies with the typos. It was correct on line 3 but incorrect on line 5
import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.MutableIssue def validationOwnerChange = changeItems.find { it.field == 'Validation Owner' && it.oldstring != it.newstring } if(validationOwnerChange){ def validationOwner = ComponentAccessor.userManager.getUserByName(validationOwnerChange.newstring) config.validationOwner = validationOwner return true } else { return false }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Replaced script with your updated script. still i see the same error on line 5.
FYI:
I have added this script in the Script Listeners(send custom email) not in post function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Below is the error in logs:
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2019-08-21 21:13:35,448 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2019-08-21 21:13:35,448 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, script: com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail java.lang.NullPointerException: Cannot get property 'displayName' on null object at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.mail.AbstractSendCustomEmail.constructMail(AbstractSendCustomEmail.groovy:258) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail.super$2$constructMail(SendCustomEmail.groovy) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail.constructMail(SendCustomEmail.groovy:208) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail$constructMail$1.callCurrent(Unknown Source) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.mail.AbstractSendCustomEmail.constructMailWithConditionResult(AbstractSendCustomEmail.groovy:246) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail.super$2$constructMailWithConditionResult(SendCustomEmail.groovy) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail.constructMailWithConditionResult(SendCustomEmail.groovy:190) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail$constructMailWithConditionResult$0.callCurrent(Unknown Source) at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.SendCustomEmail.doScript(SendCustomEmail.groovy:157)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Peter,
It is working after adding ? in the Email Template. I got notification but Validation Owner name in the notification is null.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That looks like it's might not be coming from the script, but from somewhere else in your configuration. Maybe the template.
Maybe confi.validationOwner was set to null. So you may need to trap against that.
I'm made some assumption in my recommendation. One of which is that the validation owner field is a user picker field and the changelogItem.newstring value is the username string for that field.
You may need to output some more logs to debug it.
Something along those lines:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
def validationOwnerChange = changeItems.find { it.field == 'Validation Owner' && it.oldstring != it.newstring }
if(validationOwnerChange ){
if(validationOwnerChange?.newstring){
def validationOwner = ComponentAccessor.userManager.getUserByName(validationOwnerChange.newstring.toString())
if(validationOwner){
log.info "validation owner changed detected from $validationOwnerChange.oldstring to $validationOwnerChange.newstring"
log.debug "validationOwnerChange.newstring is of class ${validationOwnerChange.newstring.getClass()}"
config.validationOwner = validationOwner
return true
} else {
log.error "no jira user found for validation owner: $validationOwnerChange.newstring"
}
} else {
log.info "validation owner changed but new value is empty"
return false
}
} else {
log.debug "no change found for validation owner field"
return false
}
And maybe instead of
config.validationOwner = validationOwner
try
config.validOwnerName = validationOwner?.displayName
and in your template use
Dear ${validOwnerName},
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Peter,
Thanks a lot for helping.
I have tried with the above script and changed the config.validOwnerName both the methods as you described above, still it is not working.
There is no error in the console but i see same error in the logs for both config.validOwnerName methods.
2019-08-22 02:34:59,096 ERROR [mail.AbstractSendCustomEmail]: no jira user found for validation owner: Manikanta
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah... I think I see my mistake. The "changeHistoryItem.newstring" is actually the displayName of the user.
We need to get the changeHistoryItem.newvalue instead to get the username
def validationOwner = ComponentAccessor.userManager.getUserByName(validationOwnerChange.newvalue)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Peter,
Thank you so much for taking time and helping. It is working now I can see Validation Owner name in the emails.
Really appreciate your help.
Thanks again,
Manikanta
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.