updating field during transition not working as expected



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);


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.setRequestProperty("Content-Type", "application/xml");
    connection.setRequestProperty("Accept", "application/xml");
    println("Connection URL  :" + url);
    println("status:" + connection.getResponseCode())
    println("status:" + connection.getResponseMessage())
} 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

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.

Thanks Jamie ... helpful ( as usual smile ) 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Mar 13, 2019 in Marketplace Apps

Marketplace Spotlight: Marketing apps for Confluence to keep your teams working on the same page


254 views 0 6
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