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

Kramarics György September 23, 2013

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
Answer accepted
Henning Tietgens
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 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
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 23, 2013

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

Henning Tietgens
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 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.

Kramarics György September 23, 2013

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

Henning Tietgens
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 23, 2013

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

Kramarics György September 23, 2013

You are right! Thanks a lot!

0 votes
Bob Swift OSS (Bob Swift Atlassian Apps)
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 23, 2013

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

0 votes
Kramarics György September 23, 2013

The target field has wiki style renderer.

Kramarics György September 23, 2013

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.

0 votes
Kramarics György September 23, 2013
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);

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events