Set custom field value based on condition

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

0 votes
Gaston Valente Community Champion Dec 12, 2017

Laxmi,

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

Do you need a code example?

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

0 votes
Gaston Valente Community Champion Dec 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
}

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.

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

261 views 0 12
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot