Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Script Runner, set 2 custom field values from rest endpoint

Sam Snowball September 17, 2021

I have a behavior on field X which links to a custom rest endpoint. I am able to set the value of field X in the return value of the rest endpoint. How can I also set field Y in the same rest endpoint execution?

Essentially, when field X is changed, it should call a rest endpoint and that endpoint should edit both field X and field Y.

For field Y, I am able to find the issue and custom field that I want to change, but am unable to make updates to that field. I am making sure to use issueManager to update the issue once it's been changed, but still no luck. The same code to update a custom field works from within a listener, but doesn't seem to work within a rest endpoint.

 

1 answer

0 votes
Martin Bayer [MoroSystems, s.r.o.]
Community Champion
September 19, 2021

HI @Sam Snowball , are you really on Cloud? Behavior functionality is not available on Cloud...

Sam Snowball September 19, 2021

no I'm on data-centre, I'll change the tags

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
September 19, 2021

Ok, can you share your source codes or scripts? I'd like to know what you have...

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
September 20, 2021

and one more question... do you want to change value of Field Y on the screen (using behavior) or using postfunction?

Sam Snowball September 20, 2021

neither, I'm trying to set the field from code within the rest endpoint but it seems impossible - but the same code works fine from within a listener

 

// Setup objects
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def customFieldManager = ComponentAccessor.getCustomFieldManager();
def componentManager = ComponentManager.getInstance();
def optionsManager = ComponentAccessor.getOptionsManager();
UserManager userManager = ComponentAccessor.getUserManager();
IssueManager issueManager = ComponentAccessor.getIssueManager();
Issue iss = issueManager.getIssueObject(event.issue.toString()); // Pass in the event.issue parameter to find the issue that was modified
MutableIssue issueToUpdate = (MutableIssue) iss;
// Find our custom fields
def fieldX = customFieldManager.getCustomFieldObjectByName('fieldX');
def fieldY = customFieldManager.getCustomFieldObjectByName('fieldY');

// Update custom fields
issueToUpdate.setCustomFieldValue(fieldX,"abc");
issueToUpdate.setCustomFieldValue(fieldY,"123");


// Save the updates
issueManager.updateIssue(currentUser, issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
September 20, 2021

try to use this method:

fieldX.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldX), "abc"), new DefaultIssueChangeHolder())
fieldY.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(fieldY), "123"), new DefaultIssueChangeHolder())

instead of

// Update custom fields
issueToUpdate.setCustomFieldValue(fieldX,"abc");
issueToUpdate.setCustomFieldValue(fieldY,"123");

// Save the updates
issueManager.updateIssue(currentUser, issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);
Sam Snowball September 27, 2021

that isn't working :/ 

Martin Bayer [MoroSystems, s.r.o.]
Community Champion
September 27, 2021

What can you see in your Jira log? Is there any exception?

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events