Accessing a Custom Field at a Workflow Validator breaks access to it in the Post-Function
Asked by Artoghrul Gahramanli:

Hi dear community,

I have encountered a bug, where, if I access a custom field using issue.getCustomFieldValue in a Validator, I will get a NULL value for calling the same in a post-function in the same transition.

Let me start by explaining our usecase first:

A validator checks if all the comma separated project keys provided in a field are valid:

```
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager

def projectManager = ComponentAccessor.getComponent(ProjectManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cfProjectName = customFieldManager.getCustomFieldObject(16610)

List < String > projectKeys = (List) issue.getCustomFieldValue(cfProjectName).split(",").collect()

if (issue.getSummary().contains("Bulk Action")) {
    for (projectKey in projectKeys) {
        def project = projectManager.getProjectObjByKey(projectKey.trim().toUpperCase())

        if (!project) {
            return false
        }
    }
}

return true
```

After which, a post-function is supposed to create sub-tasks for each project key included in that field:

```
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def constantManager = ComponentAccessor.getConstantsManager() 
def issueManager = ComponentAccessor.getIssueManager() 
def subTaskManager = ComponentAccessor.getSubTaskManager() 
def projectManager = ComponentAccessor.getComponent(ProjectManager)

def cfProjectName = customFieldManager.getCustomFieldObject(16610)
def cfRequestType = customFieldManager.getCustomFieldObjectsByName("Request type")[0]
def requestType = issue.getCustomFieldValue(cfRequestType)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() 

List < String > projectKeys = (List) issue.getCustomFieldValue(cfProjectName).split(",").collect()

if (issue.getSummary().contains("Bulk Action")) {
    for (projectKey in projectKeys) {
        def newSubtask = ComponentAccessor.getIssueFactory().getIssue()
        newSubtask.setSummary("Customization Request for " + projectKey.trim().toUpperCase())
        newSubtask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find {

            it.getName() == "Sub-task"

        }.id)
        newSubtask.setProjectId(issue.getProjectId())
        newSubtask.setParentId(issue.getId())
        newSubtask.setReporterId(issue.getReporterId())
        newSubtask.description = issue.getDescription()
        newSubtask.setCustomFieldValue(cfRequestType, requestType)
        newSubtask = issueManager.createIssueObject(user, newSubtask)
        subTaskManager.createSubTaskIssueLink(issue, newSubtask, user)

    }
}
```

If the validator is not in place, this code works without an issue. Hoowever, if the validator is installed (which also works correctly for validating the project keys), the post-function stops working, and gives this error:

```
java.lang.NullPointerException: Cannot invoke method split() on null object
```

```
2024-07-16 08:52:44,923 ERROR [utils.ConditionUtils]: Workflow condition script has failed for user 'username'. View here: https://jiraurl/secure/admin/workflows/ViewWorkflowTransition.jspa?workflowMode=live&workflowName=workflowname%3A+Workflow+for+Requests&descriptorTab=validators&workflowTransition=1&highlight=2 
java.lang.NullPointerException: Cannot invoke method split() on null object  
              at java_lang_String$split$6.call(Unknown Source)  
              at Script853.run(Script853.groovy:8)
```

Anyone knows why accessing the field at the validator breaks access at post-function, and if there's a fix or a workaround for this?

P.S I've tried both vanilla scriptrunner validator and JWME app validator, with the same result.


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Artoghrul Gahramanli](/t5/user/viewprofilepage/user-id/5544830)

Welcome to the community.

Please clarify: What is the current version of Jira and ScriptRunner you are currently using?

I am asking this because the current approach you are using is pretty complex, and this can be simplified using [ScriptRunner's HAPI](https://docs.adaptavist.com/sr4js/latest/hapi) feature which has been available since version ScriptRunner version 7.12.0.

Let me know and I will try to provide a simpler approach for this.

Regarding the validator, I suggest using the custom script validator instead of the Simple Script validator because your approach seems to be more complex and eventually seems to fail.

Thank you and Kind regards,  
Ram

comments: author: Artoghrul Gahramanli
body: Hi, thanks for your reply!

Using the custom scriptrunner validator instead of the simple validator seems to have solved the issue.

Jira is Standalone Version 8.20.11, ScR is 6.58.0.

Regarding the original problem, I have narrowed it down to the .split() call using a lot of logging. For some reason, split() call on a string within a post-function won't work when the validator installed. As in, I will have 3 lines like these:  

```
def projectKeysVal = issue.getCustomFieldValue(cfProjectName)

log.warn(projectKeysVal)

List < String > projectKeys = (List) projectKeysVal.split(",").collect()
```

The log will show that the projectKeysVal variable contains a valid value yet the split() function will error out with a null string error. Doesn't matter how many times I cast it into a string, even an inline thing like "a, b, c".split(",") fails with a null error.

I spent so much time debugging this, still can't understand how something like that can happen... Also can't understand why using a custom scr validator instead of a simple scr validator would solve this issue.

But alas, things seem to be working now.


Tags: data-center

groovy

jira-data-center

post-function

script

scriptrunner

validator