How to set epic link in Jira using Groovy

I need to set epic link using groovy if a condition is true. I'm using the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_10101") 

If I set the epic link to null it works,

    issue.setCustomFieldValue(epiclink, null)

but setting it to an issue key doesn't work.

Any help is appreciated.