Need to update a custom field value

Saurabh Sudhir March 12, 2015

We have a business requirement where we have to update a custom field with the value of a profield when the issue is being created. For this we have applied a post function script which runs on create issue transition in the workflow. But the script startup failed and unable to resolve classes error is coming in the logs.

In the script profield data is coming correctly.  

Can you please suggest the solution to resolve the issue and any modification if required.

This is the error log file 

import com.atlassian.jira.component.ComponentAccessor
import com.deiser.jira.profields.api.field.FieldService
import org.apache.log4j.Logger
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.Issue
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.atlassian.jira.event.issue.IssueEvent;
public class UpdateStudioJobsListener {
//Register the Event Listener Class with the EventPublisher         		 
		 public UpdateStudioJobsListener(EventPublisher eventPublisher) {
			eventPublisher.register(this);    
		}
@EventListener
public void onIssueEvent(IssueEvent issueEvent) {
	
def FIELD_ID = 7;
def fieldService = ComponentAccessor.getOSGiComponentInstanceOfType(FieldService.class);
def logger = Logger.getLogger(getClass());
def profieldsField = fieldService.get(FIELD_ID);
def profieldsFieldValue = profieldsField.getValueInProject(issue.projectObject);
println("first"+profieldsFieldValue);
logger.warn(profieldsFieldValue);
def ComponentManager componentManager = ComponentManager.getInstance();
def customFieldManager = ComponentManager.getCustomFieldManager();
def customFieldAccountName= customFieldManager.getCustomFieldObject('customfield_21900');          //write your own custom field id here
def customFieldAccountNameValue = issue.getCustomFieldValue(customFieldAccountName);
println("first second"+customFieldAccountNameValue)
if(profieldsFieldValue!=null){
	
Issue issue = issueEvent.getIssue();
	
IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
customFieldAccountName.updateValue(null,issue,new ModifiedValue(customFieldAccountNameValue,profieldsFieldValue),changeHolder);

println("second"+customFieldAccountNameValue)
//logger.warn(customFieldAccountNameValue );
}
}
}

1 answer

1 accepted

0 votes
Answer accepted
JamieA
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.
March 13, 2015

You have a bunch of missing imports, eg ModifiedValue

And, this is not going to work:

import com.deiser.jira.profields.api.field.FieldService

You need to use @WithPlugin annotation to allow the script to access the classloader of the profields plugin. 

See this page: https://jamieechlin.atlassian.net/wiki/display/GRV/Scripting+Other+Plugins

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events