Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Set custom field value based on condition

Sara December 11, 2017

Hello,

We'd also like to see if we can create Rules around the reporter and stakeholder fields, i.e. if Reporter = a or b then Stakeholder = XYZ. This will save time in ticket creation and will also allow us to report out on tickets and progress more easily.

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Gaston Valente
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.
December 13, 2017

Here's a sample:


import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.bc.project.component.ProjectComponent;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.security.IssueSecurityLevelManager;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.IssueEventBundle
import com.atlassian.jira.event.issue.IssueEventManager
import com.atlassian.jira.event.issue.IssueEventBundleFactory
import com.atlassian.crowd.embedded.api.Group
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.IssueInputParametersImpl;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.IssueEventDispatcher;
import com.atlassian.jira.util.ImportUtils;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import org.apache.log4j.Logger
import org.apache.log4j.Level

IssueService issueService = ComponentAccessor.getIssueService();
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();

final IssueService.IssueResult issueResult = issueService.getIssue(authContext.getLoggedInUser(), issue.getKey());
final MutableIssue mutableIssue = issueResult.getIssue();

IssueInputParameters issueInputParameters = new IssueInputParametersImpl();
issueInputParameters.setSkipScreenCheck(true);
issueInputParameters.setRetainExistingValuesWhenParameterNotProvided(true, true);


issueInputParameters.setSecurityLevelId(12901L);
issueInputParameters.setAssigneeId("newassignee_username");
issueInputParameters.addCustomFieldValue("customfield_XXXXX","newvalue");


IssueService.UpdateValidationResult updateValidationResult = issueService.validateUpdate(
authContext.getLoggedInUser(),
mutableIssue.getId(),
issueInputParameters);

if (updateValidationResult.isValid())
{

IssueService.IssueResult updateResult = issueService.update(
authContext.getLoggedInUser(),
updateValidationResult,
EventDispatchOption.DO_NOT_DISPATCH,
false );

if (updateResult.isValid())
{
//OK
}
else{
//ERR
}
}
else
{
//Validation ERROR
}
Sara December 15, 2017

Thanks for your quick replies Gaston. Where should i add the condition to change the stakeholder custom field automatically if Reporter = John or Eric then Stakeholder = Account Advisers. I am  very new to groovy scripts. I am not able to catch that.

0 votes
Gaston Valente
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.
December 12, 2017

Laxmi,

this can be done by using a script post function or listner with script runner.

Do you need a code example?

Sara December 12, 2017

Sure Gaston, Can you please send me an example on that.

TAGS
AUG Leaders

Atlassian Community Events