I’m trying to check if a custom field called “Approvers” is empty and if it is then to populate it with the reporters name.
So far this is the code I’ve got where I am trying to just populate the value with a random email address. I thought I could do the addition of the reporter after I’d managed to get the basics working.
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def dl = customFieldManager.getCustomFieldObjectByName("Approvers")
I’m getting an error on line 14
dl.setCustomFieldValue(dl, "email@example.com")- Please check if the declared type is right and the method exists.
I’ve tried the scriptrunner docs and the forum examples without any luck. Could anyone show me where I’m going wrong, or point me in the direction of a working example?
Not sure which version of Jira you are running, but in the latest version (7.12.1) the CustomFieldManager
getCustomFieldObjectByName() returns a
CustomField which indeed does not expose a
Updating the value of a custom field requires a couple of steps, as there are some constraints on using custom fields. For instance, your current code does not tell Jira for which issue you wish to set the value.
Unfortunately, I’m not familiar with any proper tutorials or code examples, so you will either have to google it or work your way through the Jira API documentation by trial/error. Here are the links for the documantion of CustomFieldManager and CustomField.
customFieldManager.getCustomFieldObjectByName("") – returns a customfied object and customfield class doesn’t have any method to set custom filed value.
you should use the following
@rbolte thanks for that. I didn’t realise I’d have to specify a specific issue so I don’t think this will work for my purposes. I thought the post function would be triggered after the creation of any form - then it would check my if statement and proceed only if that custom field was empty. Do you know if there is a way of specifying all issues? Thanks for the links they were very useful!
@c-kiran.jonnada thanks for the info! I didn’t realise I’d have to set this for a specific issue, I thought I’d be able to do it for all issues being submitted. Thanks for the docs link, that’s very useful!
I know it’s late, but better late than never, and someone else may benefit.
I think perhaps you misunderstood the code example. In the example, “issue” is the actual code, not a placeholder. So “issue” represents the issue going through the post function at the time.
If ABC-123 goes through the post function, then “issue” will represent ABC-123. If ABC-456 goes through the post function, then “issue” will represent ABC-456. You don’t have to change the code for each issue. It does work for all issues.
issue is a kind of variable for Custom Workflow Functions like Conditions, Validators, Post Functions, etc. It represents the class com.atlassian.jira.issue.MutableIssue. There’s a list of these types of variables on Adaptavist’s site if you’re interested.