Unlock a field with Script Runner

MattS
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.
January 20, 2014

Has anyone used Script Runner to unlock a custom field? The current case is the creating a new metric in Service Desk creates a locked SLA field. If you delete the particular service desk that uses the metric, the custom field is not removed.

4 answers

1 accepted

3 votes
Answer accepted
Henning Tietgens
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.
January 20, 2014

I think, something like this should work.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.managedconfiguration.ManagedConfigurationItemService
import com.atlassian.jira.issue.CustomFieldManager

ManagedConfigurationItemService managedConfigurationItemService = ComponentAccessor.getComponent(ManagedConfigurationItemService)
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObjectByName('My Customfield')

if (cf) {
    def mci = managedConfigurationItemService.getManagedCustomField(cf)
    if (mci) {
        managedConfigurationItemService.removeManagedConfigurationItem(mci)
    }
    customFieldManager.removeCustomField(cf)
}

But, I didn't test it because I don't want to have to set up a new test environment. :-)

MattS
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.
January 21, 2014

Worked like a charm, thanks! For followers: to only unlock the field just comment out the removeCustomField method near the end.

MattS
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.
August 15, 2014

I've heard that unlocking a custom field stops it appearing as a choice in the Add Column with JIRA 6.2 and later. Test this all in a staging JIRA first, as usual.

0 votes
MattS
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.
August 15, 2014

And from Rene Bodack (kreuzwerker) at https://confluence.atlassian.com/display/AGILEKB/How+to+unlock+a+Locked+fieldto lock the field again:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.managedconfiguration.ConfigurationItemAccessLevel
import com.atlassian.jira.config.managedconfiguration.ManagedConfigurationItemService
import com.atlassian.jira.issue.CustomFieldManager
ManagedConfigurationItemService managedConfigurationItemService = ComponentAccessor.getComponent(ManagedConfigurationItemService)
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
def cf = customFieldManager.getCustomFieldObjectByName('My Customfield')
 
if (cf) {
    def mci = managedConfigurationItemService.getManagedCustomField(cf)
    if (mci) {
        def managedConfigurationItemBuilder = mci.newBuilder();
        def updatedMci = managedConfigurationItemBuilder
            .setManaged(true)
            .setConfigurationItemAccessLevel(ConfigurationItemAccessLevel.LOCKED)
            .build();
        managedConfigurationItemService.updateManagedConfigurationItem(updatedMci);
    }
}


0 votes
MattS
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.
January 20, 2014

I wonder if customFieldManager.removeCustomField(customFieldObject) will work on locked fields?

0 votes
MattS
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.
January 20, 2014

There is info how to do it at the database level at https://confluence.atlassian.com/display/AGILEKB/How+to+unlock+a+Locked+field

but I don't want to restart JIRA

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events