JIRA Post Function to clear default value of a custom field

I have a custom user field for one of our specialists that defaults to their username because we only have one person in that role. Hence, the specialist.

We have added a new workflow where this specialist may or may not be needed so I added another custom field asking if we need the specialist (Yes/None).

If the answer is None, I would like to clear the default value of Specialist so the field does not appear on the issue. As it is not optional on established issues already using the Specialist field, I need to keep the default value in place.

Could I try a custom field configuration for this new issue type without a default value and essentially do the converse of this script and add the name instead or can this script be punched up to work?

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.converters.StringConverter;
import com.atlassian.jira.issue.customfields.customfieldvalue.CustomFieldValueImpl;
import com.atlassian.jira.issue.customfields.CustomFieldType;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.UpdateIssueRequest;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.security.JiraAuthenticationContext;

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()

def cField = customFieldManager.getCustomFieldObjectByName("Does this require a Specialist?")
def cfCustomerJobValue = issue.getCustomFieldValue(cField)
def str = cfCustomerJobValue.toString().indexOf('None');

if (str == 0 || str == -1)
{
 IssueManager issueManager = ComponentAccessor.getIssueManager();
 CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager();
 CustomField cf2 = cfm.getCustomFieldObjectByName("Specialist");
 def noSpec = "";
// def noSpec = null;
 issue.setCustomFieldValue(cf2, noSpec)
 cf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf2), noCopy),new DefaultIssueChangeHolder());
 issueManager.updateIssue(authenticationContext.getLoggedInUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);
}

3 answers

1 accepted

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.customfields.converters.StringConverter;
import com.atlassian.jira.issue.customfields.customfieldvalue.CustomFieldValueImpl;
import com.atlassian.jira.issue.customfields.CustomFieldType;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.UpdateIssueRequest;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.security.JiraAuthenticationContext;

JiraAuthenticationContext authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def groupManager = ComponentAccessor.getGroupManager()

def cField = customFieldManager.getCustomFieldObjectByName("Does this require a Specialist?")
def cfCustomerJobValue = issue.getCustomFieldValue(cField)
def str = cfCustomerJobValue.toString().indexOf("None");

if (str == 0)
{
 IssueManager issueManager = ComponentAccessor.getIssueManager();
 CustomFieldManager cfm=ComponentAccessor.getCustomFieldManager();
 CustomField cf2 = cfm.getCustomFieldObjectByName("Specialist");
 //def noSpec = "";
 def noSpec = null;
 issue.setCustomFieldValue(cf2, noSpec)
 cf2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf2), noSpec),new DefaultIssueChangeHolder());
 issueManager.updateIssue(authenticationContext.getLoggedInUser(),issue,EventDispatchOption.DO_NOT_DISPATCH,true);
}

Can't alter default value in Field Configuration scheme.

Error dump is

2017-06-02 14:39:59,387 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-06-02 14:39:59,387 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: MKTEST-61, actionId: 1, file: <inline script>
java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.user.ApplicationUser
 at com.atlassian.jira.issue.customfields.impl.UserCFType.updateValue(UserCFType.java:83)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:425)
 at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:395)
 at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
 at Script276.run(Script276.groovy:32)

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

3,203 views 13 19
Join discussion

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