Search code examples
groovyjiraatlassian-sourcetreeatlassian-plugin-sdkscriptrunner-for-jira

How to retrieve the value of the summary system field using the postfunction of a workflow transition in scriptrunner


I am trying to retrieve the value of the Summary system field in Jira using ScriptRunner. I am using the following code in scriptrunner but the problem is that the variable cf returned by the line def cf = customFieldManager.getCustomFieldObject("Summary") is null. How can I fix this and retrieve the value of the summary field in ScriptRunner?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Logger

def log = Logger.getLogger("atlassian-jira.log")

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()



def cf = customFieldManager.getCustomFieldObject("Summary")
log.warn("MOUNA 1: "+cf)

issue.setCustomFieldValue(cf, "mouna")
log.warn("MOUNA 2: "+issue)

Solution

  • "Summary" field in Jira is not a custom field.

    You can access the Summary field (and other system fields) directly from issue:

    log.warn(issue.summary)
    

    But for updating it in Post Function, you need to use MutableIssue class:

    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.MutableIssue
    import com.atlassian.jira.event.type.EventDispatchOption
    
    def mIssue = (MutableIssue) issue
    
    mIssue.setSummary("New Summary")
    
    def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // Or you can get any user by using UserManager
    
    ComponentAccessor.getIssueManager().updateIssue(user, mIssue, EventDispatchOption.ISSUE_UPDATED, false)
    

    Of course, don't forget to import required classes at beginning of your code: