Can't update issue in a post function script (not create)

I'm trying to make a script that collects values from other fileds and from these datas create some string and insert it to another custom field. The problem is that it't doesn't works. Somehow I have to fill two times the same information to the workflow screens fields to see them in the custom filed where I need to have them. This is a self transition. You can see the source below. Any suggestion is appreciated.

4 answers

1 accepted

1 vote
Henning Tietgens Community Champion Sep 23, 2013

You should add some log.debug() calls to log some variables and code status information to your log file. Before that you should set the log level to DEBUG (log.setLevel(org.apache.log4j.Level.DEBUG)). "log" ist already available and an org.apache.log4j.Category object.

On which position in the post function list do you call your post function?

Please post code as code (see "CODE" button in the toolbar).

Henning Tietgens Community Champion Sep 23, 2013

Please try your post function as the last postfunction in the list of postfunctions.

Henning Tietgens Community Champion Sep 23, 2013

Or, you could try the get the values from the issue object and not from the custom field object:

issue.getCustomFieldValue(cf)

In this way the at this point not stored values will be returned. If you try to get the value from the custom field it's read from the stored values.

You mean under the fire a ... event...?

Henning Tietgens Community Champion Sep 23, 2013

Yes, under the fire a ... event...

You are right! Thanks a lot!

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.fields.CustomField

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

import java.sql.Timestamp

import java.text.SimpleDateFormat

import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

import com.atlassian.jira.issue.util.IssueChangeHolder

import com.atlassian.jira.issue.ModifiedValue

import com.atlassian.jira.util.ImportUtils

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.IssueUtils

import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem;

import com.atlassian.jira.issue.MutableIssue;

 

Issue issue  = issue;

String EVENT_TYPE = "Event Type";

String EVENT_SUMMARY = "Event summary"; //az élesen "Event Summary" legyen a mező neve

String EVENT_DATE = "Event Date";

String NEXT_STEP = "Next Step";

String THREAD_HISTORY = "Thread History";

String THREAD_HISTORY_HEADER = "||Event Type||User name||Event Date||Event summary||Next Step||Due Date||";

String lineSeparator = System.getProperty("line.separator");

 

ComponentManager componentManager = ComponentManager.getInstance();

CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

 

CustomField type = customFieldManager.getCustomFieldObjectByName(EVENT_TYPE);

LazyLoadedOption option = type.getValue(issue);

String eventType = option.toString();

if (!eventType){

return false;

}

 

CustomField summary = customFieldManager.getCustomFieldObjectByName(EVENT_SUMMARY);

String eventSummary = summary.getValue(issue).toString();

if (!eventSummary){

return false;

}

 

CustomField date = customFieldManager.getCustomFieldObjectByName(EVENT_DATE);

Timestamp Date = date.getValue(issue);

String eventDate = new SimpleDateFormat("yyyy-MM-dd").format(Date);

if (!eventDate){

return false;

}

 

CustomField step = customFieldManager.getCustomFieldObjectByName(NEXT_STEP);

String nextStep = step.getValue(issue).toString();

if (!nextStep){

return false;

}

 

Timestamp due = issue.getDueDate();

String dueDate = new SimpleDateFormat("yyyy-MM-dd").format(due);

if(!dueDate){

return false;

}

 

currentUser = ComponentManager.getInstance().getJiraAuthenticationContext().getUser();

currentUserName = currentUser.getDisplayName();

if(!currentUserName){

return false;

}

 

String newLine = lineSeparator + " | " + eventType + " | " + currentUserName + " | " + eventDate + " | " + "{noformat}" + eventSummary + "{noformat}" + " | " + nextStep + " | " + dueDate;

String newValue = THREAD_HISTORY_HEADER + newLine;

 

CustomField threadHistory = customFieldManager.getCustomFieldObjectByName(THREAD_HISTORY);

String threadHistoryValue = threadHistory.getValue(issue);

if (threadHistoryValue != null && threadHistoryValue != "") {

int idx = threadHistoryValue.indexOf(lineSeparator);

if (idx != -1) {

newValue += threadHistoryValue.substring(idx);

}

}

//if (issue instanceof MutableIssue) {//((MutableIssue)issue).setCustomFieldValue(threadHistory, newValue);

//}

FieldLayoutItem fieldLayoutItem = ComponentManager.getInstance().getFieldLayoutManager().getFieldLayout(issue).getFieldLayoutItem(threadHistory);

ModifiedValue modifiedValue = new ModifiedValue(threadHistory.getValue(issue), newValue);

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

threadHistory.updateValue(fieldLayoutItem, issue, modifiedValue, changeHolder);

The target field has wiki style renderer.

As I see, the main problem is that I can't put the newest value into the filed. I can see the "second newest" under the header. When start the transition again with other values, I'm going to get the vaules before this transition.

I don't know about your script, but if you are just looking for a way to update fields based on other fields, Update issues post function from Conditioned Workflow Functions for JIRA

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

182 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you