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

I have created a groovy to copy reporter value to a custom field "QA conatact" but that fails can you please help me to create groovy

Basudev Behera Mar 20, 2016


log.setLevel(org.apache.log4j.Level.DEBUG);

MutableIssue issue = issue;
CustomField cfQAContact = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("QA Contact");

def qacontact = issue.getCustomFieldValue(cfQAContact);

if(qacontact==null)
{
def currentReporter = issue.reporter;
issue.setCustomFieldValue(cfQAContact, currentReporter);
issue.store();
}

7 answers

0 votes
Thanos Batagiannis [Adaptavist] Community Leader Mar 20, 2016

Hi Basudev,

Is this in a post function ?

0 votes
Basudev Behera Mar 21, 2016

Yes Thanos

0 votes
Thanos Batagiannis [Adaptavist] Community Leader Mar 21, 2016

Then make sure that you call this function before the "Update change history..." step. If that is not successful, try the script below

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfQAContact = customFieldManager.getCustomFieldObjects(issue).find {it.name == "QA Contact"}
def qacontact = issue.getCustomFieldValue(cfQAContact)
 
//issue.reporter should not be null
if(!qacontact && issue.reporter) {
	def changeHolder = new DefaultIssueChangeHolder()
	cfQAContact.updateValue(null, issue, new 	ModifiedValue(cfQAContact, issue.reporter),changeHolder)
}

Please let me know if this works for you.

0 votes
Basudev Behera Mar 21, 2016

It shows below error

2016-03-21 05:55:38,655 http-bio-443-exec-18 ERROR bbehera 355x332593x1 s9is0g 10.22.160.111 /secure/QuickCreateIssue.jspa [scriptrunner.jira.workflow.ScriptWorkflowFunction] Script function failed on issue: DESTDMSTR-37, actionId: 1, file: D:\Atlassian\Application Data\JIRA\scripts\Workflow Transition\NewGroovy1.groovy
java.lang.ClassCastException: com.atlassian.jira.crowd.embedded.ofbiz.OfBizUser cannot be cast to com.atlassian.jira.user.ApplicationUser
at com.atlassian.jira.issue.customfields.impl.UserCFType.getDbValueFromObject(UserCFType.java:79)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:161)
at com.atlassian.jira.issue.fields.CustomFieldImpl.createValue(CustomFieldImpl.java:833)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:505)
at com.atlassian.jira.issue.fields.CustomFieldImpl.updateValue(CustomFieldImpl.java:487)
at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)
at NewGroovy1.run(NewGroovy1.groovy:29)

0 votes
Thanos Batagiannis [Adaptavist] Community Leader Mar 21, 2016

I suspect the issue.reporter return to you a user of type OfBizUser. But you want the Application User. You should use userManager to get the Application User, but before I continue could you tell me you JIRA version ?   

0 votes
Basudev Behera Mar 21, 2016

It's 6.3.15

0 votes
Thanos Batagiannis [Adaptavist] Community Leader Mar 21, 2016

So, use UserUtil or UserManager to get the ApplicationUser using reporter's name or key. Using UserUtil for example, replace the updateValue with

cfQAContact.updateValue(null, issue, new    ModifiedValue(cfQAContact, ComponentAccessor.getUserUtil().getUserByName(issue.getReporter().getName())),changeHolder)

or in your original script 

issue.setCustomFieldValue(cfQAContact, ComponentAccessor.getUserUtil().getUserByName(issue.getReporter().getName()));

PS. In your original script you don't need to use the .store() if the post function is before the "Update change history..." step.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

742 views 7 0
Join discussion

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