updating field during transition not working as expected

Oded Priva February 5, 2015

Hi, 

 

I have the following script to update a field called 'bamboo plan' 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
//IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
//MutableIssue issue = issueManager.getIssueObject("ICTST-702");
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def bp = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Bamboo Plan'};
def aco = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Account Class'};
def ac = issue.getCustomFieldValue(aco)
if ((ac == 'Gold') || (ac == 'Bronze') || (ac == 'Silver') || (ac == 'Basic')) { 
    def changeHolder = new DefaultIssueChangeHolder();
	bp.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(bp), 'IOFF-OADTPAS'), changeHolder);
	issue.store();
    }

 

On the same transition, I use the following script to call for the bamboo plan : 

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import org.apache.commons.codec.binary.Base64;
def user = "***";
def passwd = "*****";
def requestMethod = "POST";
def URLParam = [:];


IssueManager issueManager = ComponentManager.getInstance().getIssueManager();

MutableIssue Issue = issue;
def issueID = Issue.getKey();
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField bambooPlanObject = customFieldManager.getCustomFieldObjectByName("Bamboo Plan");
def userBambooPlan = Issue.getCustomFieldValue(bambooPlanObject)?.trim();

if (userBambooPlan != null ){
    URLParam.put('bamboo.variable.jira_issue', issueID);
    def baseURL = "http://bamboo-ci.******.com:8085/rest/api/latest/queue/${userBambooPlan}";
    def query = URLParam.collect { it }.join('&');
    URL url;
    url = new java.net.URL(baseURL + "?" + query);
    def authString = user + ":" + passwd;
    byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
    String authStringEnc = new String(authEncBytes);
    URLConnection connection = url.openConnection();
    connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
    connection.setRequestMethod(requestMethod);
    connection.setRequestProperty("Content-Type", "application/xml");
    connection.setRequestProperty("Accept", "application/xml");
    println("Connection URL  :" + url);
    connection.connect();
    println("status:" + connection.getResponseCode())
    println("status:" + connection.getResponseMessage())
    connection.getContent()
} else {
    println ("bamboo plan field is empty, doing nothing.")
}

the problem is that the script to call the bamboo plan is taking the old value before the transition although changing the field value is the first thing on the transition and running and starting the bamboo plan is the last thing ( even after the generic event ) 

 

 

Any idea what I'm missing ? 

2 answers

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.
February 5, 2015

To answer your question as it stands, you probably just need to refresh the issue, eg:

issue = issueManager.getIssueObject(issue.id)

But instead of updating the value as you have, I would just use:

issue.setCustomFieldValue(cf, object)

this should work fine so long as your function comes before the ones the save and reindex the issue. If you do this, you should be fine without refreshing the issue.

0 votes
Oded Priva February 12, 2015

Thanks Jamie ... helpful ( as usual smile ) 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events