Hi all,
I am trying to update my "Client" Customfield based on the "Organisations" in JIRA Service Desk v 7.3.8 (server)
My code is as follows, it is not throwing me any error or updating the Client field.
Can someone please help me to resolve this issue?
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
MutableIssue myIssue = issue;//Script console Test
//def issue = ComponentLocator.getComponent(IssueManager).getIssueObject("GRIN-67");def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Define custom fields
def cfOrg = customFieldManager.getCustomFieldObjectByName("Organizations")
def cfClt = customFieldManager.getCustomFieldObjectByName("Client")
if (cfOrg == 'ABank'){
//Set Customfield
issue.setCustomFieldValue(cfClt,'AAA')
}else if (cfOrg == 'BBank'){
issue.setCustomFieldValue(cfClt,'BBB')
}
Thank you!
Hi,
What are the types of these custom fields (client and organization) ? Select list (single choice) ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi, sorry for the delay.
This code should work if both are select list (single choice). If multiple choice, you need to use an ArrayList. Just replace the IDs.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Organizations custom field ID
int cfOrgId = 12802
def cfOrg = customFieldManager.getCustomFieldObject(cfOrgId)
def cfOrgValue = issue.getCustomFieldValue(cfOrg)
//Client custom field ID
int cfCltId = 12801
def cfClt = customFieldManager.getCustomFieldObject(cfCltId)
def cfCltValue = issue.getCustomFieldValue(cfClt)
def cfCltConfig = cfClt.getRelevantConfig(issue)
if (cfOrgValue.getValue() == "ABank"){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "AAA" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
else if (cfOrgValue.getValue() == "BBank"){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "BBB" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
Antoine
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.
Thank you very much for posting the answer.
Sorry I couldn't mention organisation field is a multiselect list and I changed the script as follows.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager;
//import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.sal.api.component.ComponentLocator
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Organizations custom field ID
int cfOrgId = 10103
ArrayList cfOrg= (ArrayList)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Organizations"));
//def cfOrg = customFieldManager.getCustomFieldObject(cfOrgId)
def cfOrgValue = issue.getCustomFieldValue(cfOrg)
//Client custom field ID
int cfCltId = 10185
def cfClt = customFieldManager.getCustomFieldObject(cfCltId)
def cfCltValue = issue.getCustomFieldValue(cfClt)
def cfCltConfig = cfClt.getRelevantConfig(issue)
if (cfOrgValue.getValue() == "ABank"){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "AAA" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
else if (cfOrgValue.getValue() == "BBank"){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "BBB" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
It throws me this error
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.getCustomFieldValue() is applicable for argument types: (java.util.ArrayList) values: [[CustomerOrganizationImpl{id=65, name=BBB}]]
Possible solutions: getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField), setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object)
at Script1.run(Script1.groovy:24)
Can you please advice?
Thank you very much!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I still advise to use the id instead of the name, that way if you (or another jira admin) updates the name of the custom field, the script will still be working.
Now try this code, it worked for me :
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager;
//import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.sal.api.component.ComponentLocator
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Organizations custom field ID
int cfOrgId = 10103
def cfOrg = customFieldManager.getCustomFieldObject(cfOrgId)
def cfOrgValue = issue.getCustomFieldValue(cfOrg)
//Client custom field ID
int cfCltId = 10185
def cfClt = customFieldManager.getCustomFieldObject(cfCltId)
def cfCltValue = issue.getCustomFieldValue(cfClt)
def cfCltConfig = cfClt.getRelevantConfig(issue)
if (cfOrgValue.any { it.getValue() == "ABank" }){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "AAA" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
else if (cfOrgValue.any { it.getValue() == "BBank" }){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "BBB" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Antoine Berry thanks for the reply.
But as I mentioned above, Organizations is a locked customfield in JIRA Service Desk. It acts as a multiselect list but in the customfield page the field type is defined as Organisation not Multiselect.
So I put Arraylist in my script. It seems picking the Organisation but not assigning the client as expected.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Well I do not have a service desk instance running atm, but looking at the api, you could use
if (cfOrgValue.any { it.getName() == "ABank" }){
instead. This should work with the organization object.
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your first answer you mentioned to use Arraylist if it is a multiselect customfield. Can you please check whether I am doing it right. My Organisation acting as multiselect customfield.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.sal.api.component.ComponentLocator
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Organizations custom field ID
int cfOrgId = 10103
ArrayList cfOrg= (ArrayList)issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Organizations"));
//def cfOrg = customFieldManager.getCustomFieldObject(cfOrgId)
def cfOrgValue = issue.getCustomFieldValue(cfOrg)
//Client custom field ID
int cfCltId = 10185
def cfClt = customFieldManager.getCustomFieldObject(cfCltId)
def cfCltValue = issue.getCustomFieldValue(cfClt)
def cfCltConfig = cfClt.getRelevantConfig(issue)
if (cfOrgValue.getValue() == "ABank"){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "AAA" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
else if (cfOrgValue.getValue() == "BBank"){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "BBB" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
This gives me following error
groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.getCustomFieldValue() is applicable for argument types: (java.util.ArrayList) values: [[CustomerOrganizationImpl{id=65, name=BBB}]]
Possible solutions: getCustomFieldValue(com.atlassian.jira.issue.fields.CustomField), setCustomFieldValue(com.atlassian.jira.issue.fields.CustomField, java.lang.Object)
at Script1.run(Script1.groovy:24)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Could you start from the script I provided, but with some logs so it will be easier to debug :
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager;
//import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.sal.api.component.ComponentLocator
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Organizations custom field ID
int cfOrgId = 10103
def cfOrg = customFieldManager.getCustomFieldObject(cfOrgId)
log.error("cfOrg : " + cfOrg)
def cfOrgValue = issue.getCustomFieldValue(cfOrg)
log.error("cfOrgValue : " + cfOrgValue)
log.error("cfOrgValue class : " + cfOrgValue.getClass())
//Client custom field ID
int cfCltId = 10185
def cfClt = customFieldManager.getCustomFieldObject(cfCltId)
log.error("cfClt : " + cfClt)
def cfCltValue = issue.getCustomFieldValue(cfClt)
log.error("cfCltValue : " + cfCltValue)
log.error("cfCltValue class : " + cfCltValue.getClass())
def cfCltConfig = cfClt.getRelevantConfig(issue)
log.error("cfCltConfig : " + cfCltConfig)
if (cfOrgValue.any { it.getName() == "ABank" }){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "AAA" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
else if (cfOrgValue.any { it.getName() == "BBank" }){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "BBB" }
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
}
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you very much for your continuous help. :)
I used your script and I got this error message. Its not picking value for the client field.
Time (on server): Mon Apr 08 2019 10:13:39 GMT+1000 (Australian Eastern Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfOrg : Organizations 2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfOrgValue : [CustomerOrganizationImpl{id=65, name=BBB}] 2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfOrgValue class : class java.util.ArrayList 2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfClt : Client 2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfCltValue : null 2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfCltValue class : class org.codehaus.groovy.runtime.NullObject 2019-04-08 10:13:39,736 ERROR [workflow.ScriptWorkflowFunction]: cfCltConfig : com.atlassian.jira.issue.fields.config.FieldConfigImpl@886842cb 2019-04-08 10:13:39,741 ERROR [workflow.ScriptWorkflowFunction]: ************************************************************************************* 2019-04-08 10:13:39,741 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: GRE-53, actionId: 1, file: <inline script> java.lang.ClassCastException: com.atlassian.jira.issue.customfields.option.LazyLoadedOption cannot be cast to java.util.Collection at com.atlassian.jira.issue.customfields.impl.AbstractMultiCFType.createValue(AbstractMultiCFType.java:39) at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source) at Script205.run(Script205.groovy:43)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Aravindi Amarasinghe , you're welcome !
Looking at the error, I guess the client field is a multi-select list ? I assumed it was a single select list. In that case I guess you can replace the update statement with :
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, [cfNewValue]), new DefaultIssueChangeHolder())
(same of the other one)
Antoine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks a lot for your contribution. Really appreciate it.
With few slight changes to the script I could get field updated. I called Client field by name.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.sal.api.component.ComponentLocator
//Script console Test
//def issue = ComponentLocator.getComponent(IssueManager).getIssueObject("GRE-50");
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Organizations custom field ID
int cfOrgId = 10103
def cfOrg = customFieldManager.getCustomFieldObject(cfOrgId)
log.error("cfOrg : " + cfOrg)
def cfOrgValue = issue.getCustomFieldValue(cfOrg)
log.error("cfOrgValue : " + cfOrgValue)
log.error("cfOrgValue class : " + cfOrgValue.getClass())
//Client custom field ID
//int cfCltId = 10185
def cfClt = customFieldManager.getCustomFieldObjectByName("Client")
log.error("cfClt : " + cfClt)
def cfCltValue = issue.getCustomFieldValue(cfClt)
log.error("cfCltValue : " + cfCltValue)
log.error("cfCltValue class : " + cfCltValue.getClass())
def cfCltConfig = cfClt.getRelevantConfig(issue)
log.error("cfCltConfig : " + cfCltConfig)
if (cfOrgValue.any { it.getName() == "ABank" }){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "AAA" }
//cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, [cfNewValue]), new DefaultIssueChangeHolder())
}
else if (cfOrgValue.any { it.getName() == "BBank" }){
def cfNewValue = ComponentAccessor.optionsManager.getOptions(cfCltConfig)?.find { it.toString() == "BBB" }
//cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, cfNewValue), new DefaultIssueChangeHolder())
cfClt.updateValue(null, issue, new ModifiedValue(cfCltValue, [cfNewValue]), new DefaultIssueChangeHolder())
}
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.