Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Groovy Script to Change the Value of a Custom Field Based On Value of Another Field

Max Carlaftes January 6, 2020

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.

1 answer

0 votes
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 6, 2020

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)
Max Carlaftes January 7, 2020

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!

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 7, 2020

Hi @Max Carlaftes ,

What is the field type of Hardware, Misc, Perm and Software custom fields?

Max Carlaftes January 7, 2020

@brbojorque,

The four fields are all Checkboxes with two options configured.

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 7, 2020

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
Max Carlaftes January 7, 2020

@brbojorque ,

I'm getting an error on the new string lines.Groovy Snip 1.PNGDoes it need to call or import something else to be able to use this value?

Thank you for all the help!

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 8, 2020

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' })
}
Max Carlaftes January 8, 2020

@brbojorque

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!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events