Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to write postfunction to add random email to custom field based on values selected in anot field

Bojana Vasic
Contributor
June 29, 2018

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!

2 answers

2 votes
Cristian Rosas [Tecnofor]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 29, 2018

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.

Tarun Sapra
Community Champion
June 29, 2018

I too prefer to use "customField.updateValue" as opposed to "issueManager.updateIssue" 

Bojana Vasic
Contributor
June 29, 2018

Thanks a lot for helping! I will test on Monday and come back to you! :)

 

Thanks Tarun as well!

//Bojana

Bojana Vasic
Contributor
July 4, 2018

Hi @Cristian Rosas [Tecnofor],

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.

Cristian Rosas [Tecnofor]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 4, 2018

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())
}
Bojana Vasic
Contributor
July 4, 2018

Hi again @Cristian Rosas [Tecnofor],

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!

Cristian Rosas [Tecnofor]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 4, 2018

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

1 vote
Tansu Akdeniz
Community Champion
June 29, 2018

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);
}
Cristian Rosas [Tecnofor]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 29, 2018

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.

Bojana Vasic
Contributor
June 29, 2018

Thanks a lot for helping! I will test on Monday and come back to you! :)

 //Bojana

Suggest an answer

Log in or Sign up to answer