updating field during transition not working as expected

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 vote

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
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 Tuesday 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...

116 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