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

This widget could not be displayed.

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);
}

This widget could not be displayed.

Can't alter default value in Field Configuration scheme.

This widget could not be displayed.

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
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

292 views 5 0
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