Forums

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

ScriptRunner Listener - Setting an additional action to add Label

craig rayner
August 8, 2018

Hi, i have a custom scriptrunner listener that adds a subtask when a certain condition is true. I'm trying to add a label of "Testing" to this subtask in the 'Additional issue actions' part of the listener. The code i've been trying to use is:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager

ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager.class)
labelManager.addLabel(user, issue.id, 'Testing', false)

 

I'm receiving the following error:

The script could not be compiled:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script51.groovy: 4: unable to resolve class ApplicationUser 
 @ line 4, column 17.
   ApplicationUser user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
                   ^

1 error

5 answers

4 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Marius Pienaar
February 2, 2021

Hiding Fields based on priority (Behaviour Priority Field)

 



import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.IssueConstantImpl

// Code to make fields mandatory on resolution on sev1/2 tickets
if (getFieldScreen().name == "ISC - JIRA Service Desk Resolve Issue Screen"){

def priority = getFieldById(getFieldChanged())
def fieldValue = ((IssueConstantImpl) priority.getValue()).getId()

FormField rootcause = getFieldById ("customfield_10303")
FormField incont = getFieldById ("customfield_28402")
FormField scrb = getFieldById ("customfield_28401")

if (fieldValue == "2") { //Sev 1

rootcause.setFormValue(-1)
rootcause.setRequired(true)
rootcause.setHidden(false)
incont.setRequired(true)
incont.setHidden(false)
scrb.setRequired(true)
scrb.setHidden(false)
}

else if (fieldValue == "10101") { //Sev 2

rootcause.setFormValue(-1)
rootcause.setRequired(true)
rootcause.setHidden(false)
incont.setRequired(true)
incont.setHidden(false)
scrb.setRequired(true)
scrb.setHidden(false)
}

else {

rootcause.setFormValue(-1)
rootcause.setRequired(false)
rootcause.setHidden(true)
incont.setRequired(false)
incont.setHidden(true)
scrb.setRequired(false)
scrb.setHidden(true)
}
}

// Code to hide Fields from Edit Screens when not Sev1/2 tickets
else if (getFieldScreen().name == "ISC: JIRA Service Desk Edit Screen"){

def priority = getFieldById(getFieldChanged())
def fieldValue = ((IssueConstantImpl) priority.getValue()).getId()
def defaultvalue = null

FormField rootcause = getFieldById ("customfield_10303")
FormField incont = getFieldById ("customfield_28402")
FormField scrb = getFieldById ("customfield_28401")
FormField need = getFieldById ("customfield_28403")

if (fieldValue == "2") { //Sev1

rootcause.setFormValue(-1)
rootcause.setRequired(false)
rootcause.setHidden(false)
incont.setRequired(false)
incont.setHidden(false)
scrb.setRequired(false)
scrb.setHidden(false)
}

else if (fieldValue == "10101") { //Sev2

rootcause.setFormValue(-1)
rootcause.setRequired(false)
rootcause.setHidden(false)
incont.setRequired(false)
incont.setHidden(false)
scrb.setRequired(false)
scrb.setHidden(false)
}

else {

//rootcause.setFormValue(-1)
rootcause.setRequired(false)
rootcause.setHidden(true)
rootcause.setFormValue(defaultvalue)
incont.setRequired(false)
incont.setHidden(true)
incont.setFormValue(defaultvalue)
scrb.setRequired(false)
scrb.setHidden(true)
scrb.setFormValue(defaultvalue)
need.setHidden(true)

}
}
0 votes
Answer accepted
Marius Pienaar
February 2, 2021

Deleting Checklist on Lower Priority Tickets (Listener)

 


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issueManager = ComponentAccessor.getIssueManager()
def issue = event.getIssue()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).findByName("Do we need")
assert cf : "Could not find custom field with name $cf"
def optionsManager = ComponentAccessor.getOptionsManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def checkListCustomFieldValue = issue.getCustomFieldValue(cf)
def cfConfig = cf.getRelevantConfig(issue)
def checkListOptions = optionsManager.getOptions(cfConfig)
def cfType = cf.getCustomFieldType()
//def newCheckListValueItem1 = cfType.getSingularObjectFromString('{"name" : "PIR", "checked" : false, "mandatory" : true, "rank" : 1}')
//def newCheckListValueItem2 = cfType.getSingularObjectFromString('{"name" : "Long Term Resolution", "checked" : false, "mandatory" : true, "rank" : 2}')
List newCheckListValueList = new ArrayList();

if (issue.priority?.name == '3 - Medium') {
newCheckListValueList.clear()

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), newCheckListValueList), new DefaultIssueChangeHolder())
}

else if (issue.priority?.name == '4 - Low') {
newCheckListValueList.clear()

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), newCheckListValueList), new DefaultIssueChangeHolder())
}
0 votes
Answer accepted
Marius Pienaar
February 2, 2021

Adding Checklist on High Sev Tickets (Listener - Create and Update Events)



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.event.type.EventType


def issueManager = ComponentAccessor.getIssueManager()
def issue = event.getIssue()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjects(issue).findByName("Do we need")
assert cf : "Could not find custom field with name $cf"
def optionsManager = ComponentAccessor.getOptionsManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def checkListCustomFieldValue = issue.getCustomFieldValue(cf)
def cfConfig = cf.getRelevantConfig(issue)
def checkListOptions = optionsManager.getOptions(cfConfig)
def cfType = cf.getCustomFieldType()
if (event.eventTypeId != EventType.ISSUE_CREATED_ID) {
def changeLog = event?.changeLog;
def priorityChange = changeLog.getRelated("ChildChangeItem").find { it['field'] == 'priority' };
if (priorityChange == null) {
return;
}
}
def newCheckListValueItem1 = cfType.getSingularObjectFromString('{"name" : "PIR","checked" : "false", "mandatory" : false, "rank" : 1}')
def newCheckListValueItem2 = cfType.getSingularObjectFromString('{"name" : "Long Term Resolution", "checked" : "false", "mandatory" : false, "rank" : 2}')

List newCheckListValueList = new ArrayList();


if (issue.priority?.name == '1 - Critical') {

newCheckListValueList.add(newCheckListValueItem1)
newCheckListValueList.add(newCheckListValueItem2)

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), newCheckListValueList), new DefaultIssueChangeHolder())
}

else if (issue.priority?.name == '2 - High') {

newCheckListValueList.add(newCheckListValueItem1)
newCheckListValueList.add(newCheckListValueItem2)

cf.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cf), newCheckListValueList), new DefaultIssueChangeHolder())

}
0 votes
Answer accepted
Jia Jie
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 Champions.
February 1, 2021

Hi Marius,

The values returned by the Checklist field is of a specific type (ChecklistItem) in JSON which means that pure strings will not work. To convert a JSON string to a CheklistItem, you need to get the customefieldType from the custom field and call getSingularObjectFromString. The string needs to be a JSON notation in the form:

{"name" : "A", "checked" : true, "mandatory" : false, "rank" : 1} 

 

Here's the sample script which you can refer to update the checklist in Edit Screen and the Checklist field will be hidden when the Priority met the condition:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueConstantImpl
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventTypeManager

if (getActionName() != "Create") {
   
    def issueManager = ComponentAccessor.getIssueManager()

    //Get issue ID from current Edit Screen    def issue = issueManager.getIssueObject(underlyingIssue.id)

    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def cbField2 = getFieldByName("Checklist")
    def checkListField = customFieldManager.getCustomFieldObjectByName("Checklist")

    def optionsManager = ComponentAccessor.getOptionsManager()
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

    def checkListCustomFieldValue = issue.getCustomFieldValue(checkListField)
    def checkListConfig = checkListField.getRelevantConfig(issue)
    def checkListOptions = optionsManager.getOptions(checkListConfig)

    //To convert a json string to a CheklistItem,    def checkListCustomFieldType  = checkListField.getCustomFieldType()
    def newCheckListValueItem1 = checkListCustomFieldType.getSingularObjectFromString('{"name" : "A", "checked" : true, "mandatory" : false, "rank" : 1}')
    def newCheckListValueItem2 = checkListCustomFieldType.getSingularObjectFromString('{"name" : "B", "checked" : true, "mandatory" : false, "rank" : 2}')
    def newCheckListValueItem3 = checkListCustomFieldType.getSingularObjectFromString('{"name" : "C", "checked" : false, "mandatory" : false, "rank" : 1}')
    def newCheckListValueItem4 = checkListCustomFieldType.getSingularObjectFromString('{"name" : "D", "checked" : false, "mandatory" : false, "rank" : 2}')
    def newCheckListValueItem5 = checkListCustomFieldType.getSingularObjectFromString('{"name" : "E", "checked" : false, "mandatory" : false, "rank" : 3}')

    //def newCheckListValueList = []    List newCheckListValueList = new ArrayList();

    def priority = getFieldById(getFieldChanged())
    def fieldValue = ((IssueConstantImpl) priority.getValue()).getId()


    if (fieldValue == "1") {
        cbField2.setHidden(false)
        newCheckListValueList.add(newCheckListValueItem1)
        newCheckListValueList.add(newCheckListValueItem2)
        
        issue.setCustomFieldValue(checkListField, newCheckListValueList)
        issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH , false)  

    } else if (fieldValue == "2") {
        cbField2.setHidden(false)
        newCheckListValueList.add(newCheckListValueItem3)
        newCheckListValueList.add(newCheckListValueItem4)
        newCheckListValueList.add(newCheckListValueItem5)
        
        issue.setCustomFieldValue(checkListField, newCheckListValueList)
        issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH , false)    

    } else {
        cbField2.setHidden(true)
        return
    }
}

Please see thisPopulating the Jira Checklist plugin via groovy scriptrunner for more information.

Hope this helps!

Marius Pienaar
February 2, 2021

hi Jia, 

 

Thank you for the reply and explanation, I should've closed this ticket - I've managed to resolve this by making use of a Behavior and Listeners using the code you provided. 

 

note checklist field will always be visible if it contains prepopulated values - functionality doesnt exist to hide the field form the view screen

 

In my solution I create and clear the checklist field based on priority.

 

I'll post the solution and close the thread 

 

 

Regards

Marius 

Jia Jie
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 Champions.
February 2, 2021

Hi Marius,

No problem! Happy to help!

0 votes
Marius Pienaar
February 2, 2021

Hiding Fields based on Priority (Behaviours)


import com.onresolve.jira.groovy.user.FormField

import com.atlassian.jira.issue.IssueConstantImpl




// Code to make fields mandatory on resolution on sev1/2 tickets 

if (getFieldScreen().name == "ISC - JIRA Service Desk Resolve Issue Screen"){

def priority = getFieldById(getFieldChanged())

def fieldValue = ((IssueConstantImpl) priority.getValue()).getId()

FormField rootcause = getFieldById ("customfield_10303")

FormField incont = getFieldById ("customfield_28402")

FormField scrb = getFieldById ("customfield_28401")

if (fieldValue == "2") { //Sev 1 field id

rootcause.setFormValue(-1)

rootcause.setRequired(true)

rootcause.setHidden(false) 

incont.setRequired(true)

incont.setHidden(false)

scrb.setRequired(true)

scrb.setHidden(false)     

}

else if (fieldValue == "10101") { //Sev 2 field id

rootcause.setFormValue(-1)

rootcause.setRequired(true)

rootcause.setHidden(false)    

incont.setRequired(true)

incont.setHidden(false) 

scrb.setRequired(true)

scrb.setHidden(false)     

}

else {

rootcause.setFormValue(-1)

rootcause.setRequired(false)

rootcause.setHidden(true)

incont.setRequired(false)

incont.setHidden(true)

scrb.setRequired(false)

scrb.setHidden(true)    

}

}


// Code to hide Fields from Edit Screens when not Sev1/2 tickets

else if (getFieldScreen().name == "ISC: JIRA Service Desk Edit Screen"){

def priority = getFieldById(getFieldChanged())

def fieldValue = ((IssueConstantImpl) priority.getValue()).getId()

def defaultvalue = null
   

FormField rootcause = getFieldById ("customfield_10303")

FormField incont = getFieldById ("customfield_28402")

FormField scrb = getFieldById ("customfield_28401")

FormField need = getFieldById ("customfield_28403")     
 
if (fieldValue == "2") { //Sev1

rootcause.setFormValue(-1)

rootcause.setRequired(false)

rootcause.setHidden(false) 

incont.setRequired(false)

incont.setHidden(false)

scrb.setRequired(false)

scrb.setHidden(false)     

}

else if (fieldValue == "10101") { //Sev2

rootcause.setFormValue(-1)

rootcause.setRequired(false)

rootcause.setHidden(false)    

incont.setRequired(false)

incont.setHidden(false) 

scrb.setRequired(false)

scrb.setHidden(false)     

}

else {

//rootcause.setFormValue(-1)

rootcause.setRequired(false)

rootcause.setHidden(true)

rootcause.setFormValue(defaultvalue)     

incont.setRequired(false)

incont.setHidden(true)

incont.setFormValue(defaultvalue)     

scrb.setRequired(false)

scrb.setHidden(true)

scrb.setFormValue(defaultvalue)

need.setHidden(true)    
  

}

}

 

TAGS
AUG Leaders

Atlassian Community Events