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

How to copy Organizations to custom field

Berry Kersten
Contributor
September 29, 2020

Current situation:

ScriptRunner Post function 'Clones an issue, and links' used for cloning an issue from project X to project Y

Wish

Copy the field Organizations in JSD project X to custom field Reported Organization in project Y

Current implementation

The solution must be something like this

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Reported Organization'}
issue.setCustomFieldValue(sourceIssue.(Organizations)

However, I'm getting an error:

Script305.groovy: 4: unexpected token:  @ line 4, column 54.
   ue(sourceIssue.(Organizations)

Ideas anyone? Thanks.

2 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Berry Kersten
Contributor
October 2, 2020

This is the script I used that works now:

def cfReporterOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Reported Organization'}
def cfOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Organizations'}
def cfOrganizationValue = sourceIssue.getCustomFieldValue(cfOrganization)

if (cfOrganizationValue) {
issue.setCustomFieldValue(cfReporterOrganization,cfOrganizationValue.first().name.toString())
}

0 votes
Gustavo Félix
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 Leaders.
September 29, 2020

Hi @Berry Kersten 
I'm not sure if this is a typo when you wrote the code here, but 
issue.setCustomFieldValue(sourceIssue.(Organizations) 
is missing a parentheses at 
issue.setCustomFieldValue(sourceIssue.(Organizations) )

Berry Kersten
Contributor
September 30, 2020

Hi @Gustavo Félix thanks!

I've changed the code a bit. However now it's populating the custom field 'Reported Organization' with "Organizations" in stead of the value of Organizations in the linked issue..

def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Reported Organization'}
issue.setCustomFieldValue(cf,'Organizations')

If I change it to 

issue.setCustomFieldValue(cf,Organizations)

then the variabele is undeclared.

I guess I have to define Organizations first?

Gustavo Félix
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 Leaders.
September 30, 2020
def cf = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Reported Organization'}
issue.setCustomFieldValue(cf,'Organizations')

That's because setCustomFieldValue has as first parameter the customField and then the value you want to set. In this case, you are saying that you want to set the text "Organizations" to your custom Field.

def cfReporterOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Reported Organization'}
def cfOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Organizations'}
issue.setCustomFieldValue(cfReporterOrganization ,sourceIssue.getCustomFieldValue(cfOrganization ))

If I understand right your situation, I think it should be like that
You are going to get the value from Organizations and set it to Reported.

Let me know if that worked .

Berry Kersten
Contributor
October 1, 2020

Hi @Gustavo Félix thanks again!

When using that script I'm getting a NullPointerException: Cannot get property 'id' on null object. When removing the script, the existing post function does clone the issue successfully, so the error seems related to that script.

For completeness, in additional issue actions I have the following script:

//set reporter based on assignee
issue.reporter = sourceIssue.assignee

//set Isssue Security of Reported Issue in WOF form
def cf2 = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Set Reported Issue Public?'}

if (sourceIssue.getCustomFieldValue(cf2).toString().equals("Yes")) {
issue.setSecurityLevelId(10100)
} else {
issue.setSecurityLevelId(10002)
}

// set Reported Organization based on organization in JSD
def cfReporterOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Reported Organization'}
def cfOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Organizations'}
issue.setCustomFieldValue(cfReporterOrganization,sourceIssue.getCustomFieldValue(cfOrganization))

 

 

More details in the error log:

---
Caused by: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:778)
at com.atlassian.jira.issue.managers.DefaultIssueManager.createIssue(DefaultIssueManager.java:592)
... 362 more
Caused by: java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class java.lang.String (java.util.ArrayList and java.lang.String are in module java.base of loader 'bootstrap')
at com.atlassian.jira.issue.customfields.impl.GenericTextCFType.getDbValueFromObject(GenericTextCFType.java:51)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:144)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.workflow.function.issue.IssueCreateFunction.execute(IssueCreateFunction.java:84)
at com.opensymphony.workflow.AbstractWorkflow.executeFunction(AbstractWorkflow.java:1014)
at com.opensymphony.workflow.AbstractWorkflow.transitionWorkflow(AbstractWorkflow.java:1407)
at com.opensymphony.workflow.AbstractWorkflow.initialize(AbstractWorkflow.java:606)
at com.atlassian.jira.workflow.OSWorkflowManager.createIssue(OSWorkflowManager.java:754)
... 363 more
2020-10-01 07:04:00,432 ERROR [workflow.AbstractScriptWorkflowFunction]: Workflow script has failed on issue UFC-25633 for user 'sysadmin'. View here: https://support.test.uniface.com/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=Uniface+Issue+Jira+Service+Desk+IT+Support+Workflow+-+V0.2&descriptorTab=postfunctions&workflowTransition=41&highlight=1
java.lang.NullPointerException: Cannot get property 'id' on null object
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue.execute(CloneIssue.groovy:164)
at com.onresolve.scriptrunner.canned.jira.workflow.postfunctions.CloneIssue$execute$0.callCurrent(Unknown Source)
---

Gustavo Félix
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 Leaders.
October 1, 2020

What kind of custom fields are your fields ? TextFields or Select List? 

Berry Kersten
Contributor
October 1, 2020

Organizations is a custom field provided by JSD of type Organizations (select list, multiple choice)

Reported Organization is of type Text field (single line)

Although several Organizations are possible, in practice 1 will always be chosen.

Schermafbeelding 2020-10-01 om 10.07.30.png

Gustavo Félix
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 Leaders.
October 1, 2020

Ok, two things.
1) Caused by: com.atlassian.jira.workflow.WorkflowException: Error occurred while creating issue. This could be due to a plugin being incompatible with this version of JIRA. For more details please consult the logs, and see: http://confluence.atlassian.com/x/3McB

It seems that your version is not updated. I've never seen that error, but that webpage says that.  Maybe something you need to check out. 

2) I didnt read that you were using jira service desk, I thought it was Jira Server . So , I cant replicate the use of "Organizations". It seems from what I've searched, is a select list, but I'm not sure how to handle the field.

I would try 
def cfOrganization = customFieldManager.getCustomFieldObjects(sourceIssue).find {it.name == 'Organizations'} as List

Before the setCustomFieldValue , add a 
log.warn "cfOrganization.get(0)" 

Just to test if it logs something.

But again, I dont have JSD , so I'm almost guessing here.
My suspect is that you are trying to set an Organizations type field to a String field, so that has to be converted somewhere.

I hope this helps you.

Berry Kersten
Contributor
October 2, 2020

Thanks @Gustavo Félix 

I changed the script again (and moved the post function below the re-index post function)

and now it works!

Like Gustavo Félix likes this
TAGS
AUG Leaders

Atlassian Community Events