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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

640 views 6 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