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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to copy Organizations to custom field

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

0 votes
Answer accepted

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())
}

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) )

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?

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 .

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)
---

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

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

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.

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across their  Jira Cloud instance.  ...

561 views 15 18
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you