Scriptrunner update a custom user field

Hi,

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.

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.user.ApplicationUser

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def dl = customFieldManager.getCustomFieldObjectByName("Approvers")


if(!dl)
{
    dl.setCustomFieldValue(dl, "email@gmail.co.uk")
}

I’m getting an error on line 14 dl.setCustomFieldValue(dl, "email@gmail.co.uk")- 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?

https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/conditions/current-user-equal-to-cf.html

Thanks!!!

Hi @h.l.hanna,

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 setCustomFieldValue() method.

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.

Cheers,

Remie

HI hanna,

customFieldManager.getCustomFieldObjectByName("") – returns a customfied object and customfield class doesn’t have any method to set custom filed value.

https://docs.atlassian.com/software/jira/docs/api/7.1.1/com/atlassian/jira/issue/fields/CustomField.html

Solution:
you should use the following

issue.setCustomFieldValue(dl,"email@gmail.co.uk")
issuemanager.updateIssue(currentUser,issue,EventDispatchOption.ISSUE_UPDATED, true)

Thanks,
Kiran

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