Hey everyone,
Forgive the long question. I'm working on Groovy script to set the value of a custom field (Incident or Request) based on the choice in a different custom field(s).
This is a first attempt and is fairly messy, but as it stands, we have four different Issue Types, Hardware, Software, Permissions, and Misc. Each of these types has a custom field asking the user a question that boils down to "Is this something new or is something broken?"
Due to the nature of the different forms, the custom fields aren't the exact same, so I had to call the content of the form's custom field in order to give Incident or Request something to work off of.
The script "works", right now it sets everything to Incident. Which is half the battle, but not everything is an incident! Please find the attempted script below:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueMgr = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def cf = customFieldManager.getCustomFieldObjectByName("Incident or Request")
def cFieldValueHardware = customFieldManager.getCustomFieldObjectByName("Hardware Issue Type").getValue(issue)
def cfMiscType = customFieldManager.getCustomFieldObjectByName("Misc Ticket Type")
def cfPermType = customFieldManager.getCustomFieldObjectByName("Permissions Ticket Type")
def cfSoftwareType = customFieldManager.getCustomFieldObjectByName("Software Issue Type")
def cFieldValueMisc = issue.getCustomFieldValue(cfMiscType)
def cFieldValuePerm = issue.getCustomFieldValue(cfPermType)
def cFieldValueSoftware = issue.getCustomFieldValue(cfSoftwareType)
def fieldConfig = cf.getRelevantConfig(issue)
def value = 'Request'
if(cFieldValueHardware == "I need something!" || cFieldValueMisc == "I need something" || cFieldValuePerm == "I need access to something" || cFieldValueSoftware == "I need some software")
value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Request' }
else
value = ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Incident' }
I've dug through the forums and found plenty of questions about changing custom fields with Groovy but I can't find anything that's exactly this situation. Does anyone have any ideas on how I could tweak this to work better? Or glaring gaps in what's been put together?
Please let me know if you need any additional information to rescue me!
Thanks.
Hi @Max Carlaftes .
Please try the script below, order your filter so you can properly change the right value of the custom field.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.user.ApplicationUser
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue issueUpdate = (MutableIssue) issue
CustomField cf = customFieldManager.getCustomFieldObjectsByName("Incident or Request")[0]
CustomField cfHardwareType = customFieldManager.getCustomFieldObjectsByName("Hardware Issue Type")[0]
CustomField cfMiscType = customFieldManager.getCustomFieldObjectsByName("Misc Ticket Type")[0]
CustomField cfPermType = customFieldManager.getCustomFieldObjectsByName("Permissions Ticket Type")[0]
CustomField cfSoftwareType = customFieldManager.getCustomFieldObjectsByName("Software Issue Type")[0]
String cFieldValueMisc = issueUpdate.getCustomFieldValue(cfMiscType)
String cFieldValuePerm = issueUpdate.getCustomFieldValue(cfPermType)
String cFieldValueSoftware = issueUpdate.getCustomFieldValue(cfSoftwareType)
String cFieldValueHardware = issueUpdate.getCustomFieldValue(cfHardwareType)
FieldConfig fieldConfig = cf.getRelevantConfig(issueUpdate)
if(cFieldValueHardware == "Request" && cFieldValueMisc == "Request" && cFieldValuePerm == "Request" && cFieldValueSoftware == "Request"){
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Request' })
} else if(cFieldValueHardware == "Support" && cFieldValueMisc == "Support" && cFieldValuePerm == "Support" && cFieldValueSoftware == "Support"){
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Support' })
}else {
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Incident' })
}
ApplicationUser userObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(userObj, issueUpdate, EventDispatchOption.DO_NOT_DISPATCH, false)
Hello Bryan,
Thanks for sending this along, I gave it a shot and the same issue occurs. It does set the 'Incident or Request' field, but it always sets it to Incident regardless of which option is chosen in the form. All things that should come through as a 'Request' have Incident chosen.
I've done minor tweaking on my side but I can't exactly see where it would be failing. It's working, but something about the initial 'if' statement doesn't jive and it defaults to the 'else' for Incident.
I added some logging to it and it's returning this on a 'success'.
2020-01-07 09:01:32,928 DEBUG [workflow.ScriptWorkflowFunction]: Hardware:null 2020-01-07 09:01:32,929 DEBUG [workflow.ScriptWorkflowFunction]: Misc:null 2020-01-07 09:01:32,929 DEBUG [workflow.ScriptWorkflowFunction]: Perm:null 2020-01-07 09:01:32,929 DEBUG [workflow.ScriptWorkflowFunction]: Software:null
So it's not pulling anything for the four form types.
I'll keep plugging away, thank you again for the attempt! If you have any insight, I'd really appreciate hearing it!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The four fields are all Checkboxes with two options configured.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Max Carlaftes ,
Try change the CF's variables like so below.
String cFieldValueMisc = issueUpdate.getCustomFieldValue(cfMiscType)*.value
String cFieldValuePerm = issueUpdate.getCustomFieldValue(cfPermType)*.value
String cFieldValueSoftware = issueUpdate.getCustomFieldValue(cfSoftwareType)*.value
String cFieldValueHardware = issueUpdate.getCustomFieldValue(cfHardwareType)*.value
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm getting an error on the new string lines.Does it need to call or import something else to be able to use this value?
Thank you for all the help!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Max Carlaftes .
I finally figured out why it is not returning the value.
The customfield value is returning a list instead of a string well sort of and it does not matter really.
What we can do is to just search for the value like so below.
cFieldValueHardware?.contains("Request")
log.debug(cFieldValueHardware?.contains("Request"))
So the working part of the code should be.
String cFieldValueMisc = issueUpdate.getCustomFieldValue(cfMiscType)
String cFieldValuePerm = issueUpdate.getCustomFieldValue(cfPermType)
String cFieldValueSoftware = issueUpdate.getCustomFieldValue(cfSoftwareType)
String cFieldValueHardware = issueUpdate.getCustomFieldValue(cfHardwareType)
FieldConfig fieldConfig = cf.getRelevantConfig(issueUpdate)
if(cFieldValueHardware?.contains("Request") && cFieldValueMisc?.contains("Request") && cFieldValuePerm?.contains("Request") && cFieldValueSoftware?.contains("Request")){
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Request' })
} else if(cFieldValueHardware?.contains("Support") && cFieldValueMisc?.contains("Support") && cFieldValuePerm?.contains("Support") && cFieldValueSoftware?.contains("Support")){
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Support' })
}else {
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Incident' })
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Good morning, thanks for this!
I plugged it in and the custom field is still defaulting to the Else, 'Incident', regardless of what's input on the form.
I'm not getting any errors on the script but the logs are still reporting "null". Here's the whole script as I've got now, with some tweaks on my side to apply to the custom fields on my side. Mainly in the .contains section for the options within 'Incident or Request'.
log.setLevel(org.apache.log4j.Level.DEBUG)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.user.ApplicationUser
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
MutableIssue issueUpdate = (MutableIssue) issue
CustomField cf = customFieldManager.getCustomFieldObjectsByName("Incident or Request")[0]
CustomField cfHardwareType = customFieldManager.getCustomFieldObjectsByName("Hardware Issue Type")[0]
CustomField cfMiscType = customFieldManager.getCustomFieldObjectsByName("Misc Ticket Type")[0]
CustomField cfPermType = customFieldManager.getCustomFieldObjectsByName("Permissions Ticket Type")[0]
CustomField cfSoftwareType = customFieldManager.getCustomFieldObjectsByName("Software Issue Type")[0]
String cFieldValueMisc = issueUpdate.getCustomFieldValue(cfMiscType)
String cFieldValuePerm = issueUpdate.getCustomFieldValue(cfPermType)
String cFieldValueSoftware = issueUpdate.getCustomFieldValue(cfSoftwareType)
String cFieldValueHardware = issueUpdate.getCustomFieldValue(cfHardwareType)
FieldConfig fieldConfig = cf.getRelevantConfig(issueUpdate)
if(cFieldValueHardware?.contains("I need something!") || cFieldValueMisc?.contains("I need something") || cFieldValuePerm?.contains("I need access to something") || cFieldValueSoftware?.contains("I need some software")){
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Request' })
}else {
issueUpdate.setCustomFieldValue(cf, ComponentAccessor.optionsManager.getOptions(fieldConfig)?.find { it.toString() == 'Incident' })
}
log.debug("Issue:" + issue.key)
log.debug("Hardware:" + cFieldValueHardware)
log.debug("Misc:" + cFieldValueMisc)
log.debug("Perm:" + cFieldValuePerm)
log.debug("Software:" + cFieldValueSoftware)
ApplicationUser userObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(userObj, issueUpdate, EventDispatchOption.DO_NOT_DISPATCH, false)
I hate to keep asking but is there anything that jumps out as to why the script isn't returning 'Request' when a user picks any of the listed options?
Thank you so much, Bryan!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.