Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

removing the value from custom field or set to null

Hentsu May 19, 2020

I want to remove the value from the custom field or set it to null. The code below is not working as expected. Please advise me.

Thanks in advance.
regards

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def customField = customFieldManager.getCustomFieldObject("customfield_10107");
def value = issue.getCustomFieldValue(customField);

customFieldManager.removeCustomField(customField)

1 answer

0 votes
PD Sheehan
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.
May 23, 2020

Hi

Try 

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager
def customField = customFieldManager.getCustomFieldObject("customfield_10107")
def value = issue.getCustomFieldValue(customField) //<-- don't know why you wanted this
issue.setCustomFieldvalue(customField, null)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Hentsu May 25, 2020

Hi Peter,

Thanks for the advice. I have tried with code below, but it still doesn't set the custom field value to null.

Please advise.

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.fields.config.FieldConfig;
import com.atlassian.jira.config.PriorityManager

def customFieldManager = ComponentAccessor.customFieldManager
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.issueManager
def user = userManager.getUserByName("UserA")
def value = issue.getCustomFieldValue(customField) 

MutableIssue mutableIssue = (MutableIssue) mutableIssue.setCustomFieldValue(customField,null)
issueManager.updateIssue(user, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

PD Sheehan
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.
May 25, 2020

Perhaps some more detail as to where this code is implemented exactly.

This code seems incomplete.

For example:

  • where is "customField" defined from the line "def value = issue.getCustomFieldValue(customField)"
  • What are you expecting "MutableIssue mutableIssue = (MutableIssue)" to do? There is no data to put into the mutableIssue variable
Hentsu June 3, 2020

Sorry for the response delay. I have a plan to implement in the post function(Transition stage)

I tried to research and modify the script but no luck. it still is getting an error as below,

 1. If function doesn't work

 2. cannot set the custom value to null

Thanks,

 

ERROR [workflow.AbstractScriptWorkflowFunction]: Script function failed on issue: ST-102, actionId: 761, file: null
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setCustomFieldvalue() is applicable for argument types: (com.atlassian.jira.issue.fields.ImmutableCustomField, null) values: [Request participants, null]
Possible solutions: setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object), getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField)
at Script127.run(Script127.groovy:22)

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.event.type.EventDispatchOption;
import com.atlassian.jira.issue.context.IssueContextImpl;
import com.atlassian.jira.issue.fields.config.FieldConfig;


def issueManager = ComponentAccessor.issueManager
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserByName("UserA")
def customFieldManager = ComponentAccessor.customFieldManager

def Participants_Value = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10101"))
def Participants = customFieldManager.getCustomFieldObject("customfield_10101")
def Company_Value = issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_10107"))
def Company = customFieldManager.getCustomFieldObject("customfield_10107")

if (Company_Value.equals("companyA")){

issue.setCustomFieldvalue(Participants , null)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

PD Sheehan
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.
June 3, 2020

Ah! 

Took me a minute.

Simple typo. It should  be setCustomFieldValue not setCustomerFieldvalue

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events