Hi!
I have a custom field called 'Customer Group' which is an automatically populated field (integration with another system).
I have another custom field called 'Customer Email' which is a free text field.
I want to write a post-function(s) on the Create transition to achieve the following:
IF the 'Customer Group' field contains a "xxx" then add "x@gmail.com" and "y@gmail.com" emails to the 'Customer Email' field.
I do have ScripRunner...
Thanks in advance!
First, be careful with the order in the transitions. You probably need to use a special out of the box post function only available in creation transition.
This is only based on what I understood, you need something like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfCustomerGroup = customFieldManager.getCustomFieldObject(10100L) //put here the id of your custom field. Do not delete the "L", is mandatory
def cfCustomerEmail = customFieldManager.getCustomFieldObject(10200L) //put here the id of your custom field. Do not delete the "L", is mandatory
def customerGrupo = issue.getCustomFieldValues(cfCustomerGroup)
def customerEmail = issue.getCustomFieldValues(cfCustomerEmail)
if (customerGrupo.toString() == "XXX" {
customerEmail.updateValue(null, issue, new ModifiedValue(customerEmail, [customerEmail + "XXX@gmail.com" + " "+ "YYY@gmail.com"]), new DefaultIssueChangeHolder())
}
You will need to format the strings in the text field as you wish.
Hope it helps.
I too prefer to use "customField.updateValue" as opposed to "issueManager.updateIssue"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot for helping! I will test on Monday and come back to you! :)
Thanks Tarun as well!
//Bojana
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @[deleted],
Could you please help?
On the last line wher you have "}", I'm getting an error message saying "Expecting ")" , found "" ... but even if I add ")", I'm still getting error massage but now saying "unexpected token ", where you just see empty space.
When I click on Update to see the more detaild error massage I get the following;
"The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script268.groovy: 17: unexpected token: @ line 17, column 3. }) ^ 1 error </pre>."
I have tried sevaral combinations, but still having errors.
Many thanks in advance.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm sorry, I detected two mistakes:
The "if" needed another ")" and the new ModifiedValue has a collection (represented with the []) use this:
if (customerGrupo.toString()) == "XXX" {
customerEmail.updateValue(null, issue, new ModifiedValue(customerEmail, customerEmail + "XXX@gmail.com" + " "+ "YYY@gmail.com"), new DefaultIssueChangeHolder())
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi again @[deleted],
Thank You! I'm now getting another error regarding "==", saying that this is unexpected token, see below;
if (customerGroup.toString()) == "xxx customer" {
Any ideas?
Many thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
lol! I put the ")" in the wrong place :D I'm sorry I didn't pay much attention
if (customerGrupo.toString() == "XXX") {
customerEmail.updateValue(null, issue, new ModifiedValue(customerEmail, customerEmail + "XXX@gmail.com" + " "+ "YYY@gmail.com"), new DefaultIssueChangeHolder())
}
Anyway, if you have not much knowledge about programming, it will be a bit difficult to know what you must code in the strings. You should ask for help to someone in your company about this
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Bojana Vasic,
You can add "Script Postfunction" to the create transition.
This code will guide you.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.user.ApplicationUser;
IssueManager issueManager = ComponentAccessor.getIssueManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
Issue issue = ComponentAccessor.getIssueManager().getIssueObject(issue.key);
MutableIssue mutableIssue = (MutableIssue) issue;
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
CustomField customerGroupCF = customFieldManager.getCustomFieldObject("customfield_abcde");
CustomField customerEmailCF = customFieldManager.getCustomFieldObject("customfield_abcde");
String customerGroup = issue.getCustomFieldValue(customerGroupCF);
if(customerGroup.contains("xxxx")){
mutableIssue.setCustomFieldValue(customerEmailCF,"x@gmail.com");
issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Some tips!
You dont need to use issueManager neither mutableIssue in a postfunction, because the word "issue" already has the value of the issue which executed the transition. You need them for console or to get other issues.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot for helping! I will test on Monday and come back to you! :)
//Bojana
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.