Read and Get value of a Jira System Field (Example: Labels or Summary field)

Ricardo June 26, 2018

Hi,

I would like to know how to read a Jira System Field. (get the field´s value)

I know how to read and get the value of a Custom Field, what is easy and there is a lot of information on the Internet.

However, System Field is a mystery, there is no information anywhere.

I know the method getFieldById(...) or getFieldByName(...) can read a System Field, but the problem is that it works only for the current Jira issue, the issue that has generated the current event.

I need is more flexibility, I want to be able to read not only a System field from the current issue, assuming I am behavior script. I want to be able to read a System field from any issue, including the Parent issue and I want to do it using pure Java code based on JIRA API the same way we can do for Custom fields.

I need to read System fields from Behaviour scripts and Listeners scripts as well.

Could you please help?

Cheers

 

1 answer

1 vote
Alexey Matveev
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 26, 2018

Hello,

You read system fields by the get methods of the Issue class. For example, to get the summary of an issue, you would use underlyingIssue.getSummary(). 

You can find all info about available methods of the Issue class here:

https://docs.atlassian.com/software/jira/docs/api/7.1.2/com/atlassian/jira/issue/Issue.html

If you want to get the summary of the parent issue, your code would look like this:

underlyingIssue.getParentObject().getSummary()

Ricardo June 26, 2018

Hi, Alexey

Thanks for answering.

The underlyingIssue.getSummary() can read the field Summary, but I would like to be able to read any System field, especially the field called "Labels".

If you see my code below, I will see that my problem happens when the underlying issue is a sub-task (check the condition in the code)

Cheers

--------------------------------

import com.atlassian.jira.issue.*

import static com.atlassian.jira.issue.IssueFieldConstants.*

import org.apache.log4j.Category;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.ModifiedValue;
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.index.IssueIndexingService;

def Category log=Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)

log.debug("----------------------------------------------------");
log.debug("SOWREF FIELD POPULATING PROCESS STARTED...");
log.debug(">> Current Issue: " + underlyingIssue.getKey().toUpperCase());
IssueEventService service = new IssueEventService();
service.populateSowRefField(underlyingIssue, getFieldById("labels")); //Get Labels field from current task.
log.debug("SOWREF FIELD POPULATING PROCESS COMPLETE.");
log.debug("----------------------------------------------------");

public class IssueEventService {

    private CustomFieldManager customFieldManager;
    public Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction");

    public IssueEventService() {
        log.setLevel(org.apache.log4j.Level.DEBUG);
        customFieldManager = ComponentAccessor.getCustomFieldManager();
    }

    public void populateSowRefField(Issue issue, FormField formFielLabels) {
        
        String sowRefWoValue;
        String workOrderValue;
        
        if (issue.isSubTask()) {
            
            log.debug("It is a Sub-Task");
             Issue parent = issue.getParentObject();
             log.debug("   PARENT ISSUE [" + parent.getKey().toUpperCase() + "]");
            log.debug("   LABELS VALUE: [" + getLabelsByPrefix("SOW_", formFielLabels) + "]");
            //SHOULD READ LABELS FROM PARENT TASK HERE...
            //SHOULD READ WORKORDER FROM PARENT TASK HERE...
            //GET SOWREFWO HERE IF NECESSARY
            //SET SOWREF FIELD HERE...
            
        } else if (CheckUtil.isEpicTask(issue)) {
            
            log.debug("It is an Epic Task");
            workOrderValue = getWorkOrderValue(issue);
            sowRefWoValue = getSowRefWo(workOrderValue);
            updateSowRefField(sowRefWoValue, issue);

        } else { //Any Other Tasks type.
            
            log.debug("It is NOT an Epic Task or Sub-Task");
            String sowRefFieldNewValue = getLabelsByPrefix("SOW_", formFielLabels);
            if(sowRefFieldNewValue == null){
                log.debug("No valid Label value was found! Defining default value PREFIX + WO...");
                workOrderValue = getWorkOrderValue(issue);
                sowRefFieldNewValue = getSowRefWo(workOrderValue);//Uses default value SOW_ + WO
            }
            updateSowRefField(sowRefFieldNewValue, issue);
        }
    }
    
    private String getWorkOrderValue(Issue issue){
        com.atlassian.jira.issue.fields.CustomField workOrderField = customFieldManager.getCustomFieldObjectByName(CustomField.WORK_ORDER_NUMBER);
        log.debug("   Retrieved Work Order field value [" + (String)workOrderField.getValue(issue) + "]");
        return (String)workOrderField.getValue(issue);
    }
    
    private void updateSowRefField(String sowRefFieldNewValue, Issue issue){
        log.debug("   Updating SOWRef field value in " + issue.getKey().toUpperCase());
        com.atlassian.jira.issue.fields.CustomField sowRefFieldObj = customFieldManager.getCustomFieldObjectByName(CustomField.SOW_REF);
        DefaultIssueChangeHolder defaultIssueChangeHolder = new DefaultIssueChangeHolder();
        String sowRefFieldOldValue = sowRefFieldObj.getValue(issue);
        if(!sowRefFieldNewValue.equals(sowRefFieldOldValue)){//Current SOWRef field value is different of New SOWRef value.
            sowRefFieldObj.updateValue(null, issue, new ModifiedValue(sowRefFieldOldValue, sowRefFieldNewValue), defaultIssueChangeHolder);
               log.debug("   SOWRef field value updated! Old value [" + sowRefFieldOldValue + "], New value [" + sowRefFieldNewValue + "]");
        } else {
            log.debug("   SOWRef field value NOT updated! Old value is equals to the new value. Old value [" + sowRefFieldOldValue + "], New value [" + sowRefFieldNewValue + "]");
        }
    }
    
    private String getLabelsByPrefix(String prefix, FormField formFielLabels){
        StringBuilder sowRefFieldNewValue = new StringBuilder();
        String formFieldLabelsValue = formFielLabels != null && formFielLabels.getValue() != null ? formFielLabels.getValue() : "";
        formFieldLabelsValue = formFieldLabelsValue.replace("[","").replace("]","");//Cleaning original value
        log.debug("   Original Labels field value is [" + formFieldLabelsValue + "]");
        StringTokenizer strToken = new StringTokenizer(formFieldLabelsValue, ",");
        while(strToken.hasMoreTokens()) {
            String token = strToken.nextToken().trim();
            if(token.startsWith(prefix)){//Consider only Labels with the specified prefix.
                sowRefFieldNewValue.append(token);
                sowRefFieldNewValue.append(",");
            }
        }
        if(sowRefFieldNewValue.size() > 1){
            sowRefFieldNewValue.deleteCharAt(sowRefFieldNewValue.size() - 1);//Remove last comma
            log.debug("   Filtered Labels field values is [" + sowRefFieldNewValue.toString() + "] - Only Prefix (" + prefix + ")");
            return sowRefFieldNewValue.toString();
        } else {
            log.debug("   Labels field did not have value or did not have value starting with SOW_");
            return null;            
        }
    }
    
    private String getSowRefWo(String workOrderNumber){
           log.debug("   Work Order Number [" + workOrderNumber + "]");
        StringBuilder sowRefWo = new StringBuilder("SOW_");
        sowRefWo.append(workOrderNumber);
        log.debug("   SOWREF_WO ["+sowRefWo.toString()+"]");
        return sowRefWo.toString();//Return prefix "SOW_" plus "Work Order number"
    }
}

public class CheckUtil {
    public static boolean isEpicTask(Issue issue) {
        String issueTypeName = issue.getIssueType().getName();
        return Constants.EPIC.equals(issueTypeName);
    }
}

public class Constants {
    public static final String EPIC = "Epic";
}

public class CustomField {
    public static final String EPIC_GROUPING = "Epic Grouping";
    public static final String WORK_ORDER_NUMBER = "Work Order";
    public static final String SOW_REF = "SOWRef";
    public static final String LABELS = "Labels";
}

Ricardo June 26, 2018

It´s working now!

issue.getLabels();

Cheers

Sahish March 2, 2022

@Alexey Matveev 

Hi, 

I am also having the similar type of requirement. Could you please help me on this.

I would like to be able to read the System field, especially the field called "Component/s:"

I am providing my code below,

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.user.ApplicationUser

Long ISSUE_LINK_TYPE_ID = 10005 //Cloning ID//
Long TEXT_CUSTOM_FIELD_ID = underlyingIssue.getComponents() //Component Field ID//
String ISSUE_TYPE_ID = 10601 //Problem Report ID//

IssueLink issueLink = event.getIssueLink()
if (issueLink.getIssueLinkType().getId() != ISSUE_LINK_TYPE_ID) {
return
}
MutableIssue issue = issueLink.getSourceObject()
if (issue.getIssueType().getId() != ISSUE_TYPE_ID) {
return
}
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
CustomField textCustomField = customFieldManager.getCustomFieldObject(TEXT_CUSTOM_FIELD_ID)
issue.setCustomFieldValue(textCustomField, null)
issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

Suggest an answer

Log in or Sign up to answer