Script failing to run in Workflow Process

Andrew Downs July 25, 2018

Hi guys,

 

I need some assistance please, I have a requirement to do a Joiner, Mover, Leaver (JML) process, I have received some assistance on the forum but am at a point again where I seem to be having issues.

 

The process would be as follows:

 

1) A user logs the JML on the Jira Portal

2) Risk an compliance officer will open the SSD in Jira and transition the call through a Validate Account, the validate should fire the script below:

 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService

// Enable Logging
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

// Define Required Component Access

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

// Get Issue ID that contains the Data
def issueKey = event.issue
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey");

// Get field values
def fullName = customFieldManager.getCustomFieldObjectByName("Full Name");
def fullNameValue = issue.getCustomFieldValue(fullName);

// Get required configuration for comment
final SD_PUBLIC_COMMENT = "sd.public.comment"
def commentPropertyService = ComponentAccessor.getComponent(CommentPropertyService)
def commentManager = ComponentAccessor.getCommentManager()

// Define HTTP URL and call API

def httpBuilder = new HTTPBuilder("https://server.contoso.com")
httpBuilder.setHeaders([Authorization: "Basic <redacted>"])

httpBuilder.get(
    path: "/api/Users",
    requestContentType: ContentType.JSON,
    query: [search: "${fullNameValue}", container: "DC=contoso,DC=com"]
){
   resp, reader ->
        userList(resp, reader)
    }

def userList(def resp, def reader){
    
    log.debug "Response is $resp"
    log.debug "Reader is $reader"
}

 I receive the following error on the script execution:

 

Time (on server): Thu Jul 26 2018 08:15:49 GMT+0200 (South Africa Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-07-26 08:15:49,745 DEBUG [jira.groovy]: debug statements
2018-07-26 08:15:49,748 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-07-26 08:15:49,749 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SSD-18229, actionId: 31, file: <inline script>
groovy.lang.MissingPropertyException: No such property: event for class: Script452
	at Script452.run(Script452.groovy:33)

I cannot seem to figure out how to get the logging detail so I can see what is wrong.

Also, I need to update the comment field with the results from $resp or at least from $reader with any results that may have been returned. I am using Jira 7.10.1. Could some one possible assist?

1 answer

1 accepted

0 votes
Answer accepted
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 26, 2018

Hello @Andrew Downs

It seems that you are using this code in workflow.

Try to replace 

def issueKey = event.issue

on 

def issueKey = issue.getKey()

variable event exist only in Script Listeners.

Andrew Downs July 26, 2018

Hi Mark,

 

Thank you. I have updated but now getting a different error, as per below:

 

Time (on server): Thu Jul 26 2018 11:24:33 GMT+0200 (South Africa Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-07-26 11:24:33,950 DEBUG [jira.groovy]: debug statements
2018-07-26 11:24:33,950 DEBUG [jira.groovy]: debug statements
2018-07-26 11:24:33,958 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-07-26 11:24:33,958 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-07-26 11:24:33,958 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SSD-18229, actionId: 31, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script103.run(Script103.groovy:41)
2018-07-26 11:24:33,958 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SSD-18229, actionId: 31, file: <inline script>
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script103.run(Script103.groovy:41)

It seems that it cannot get the custom field value on the SSD. Am I doing something wrong in the code?

 

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.UpdateIssueRequest
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.json.JSONObject
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category
import com.atlassian.jira.bc.issue.comment.property.CommentPropertyService

// Enable Logging
def Category log = Category.getInstance("com.onresolve.jira.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

// Define Required Component Access

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()

// Get Issue ID that contains the Data
def issueKey = issue.getKey()
//Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey");
//def issueManager = ComponentAccessor.getIssueManager()
//MutableIssue issue = issueManager.getIssueObject("issueKey")
Issue issue = issueManager.getIssueObject( "issueKey" );

// Get field values
def fullName = customFieldManager.getCustomFieldObjectByName("Full Name");
def fullNameValue = issue.getCustomFieldValue(fullName);
Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 26, 2018

Oh, ofcourse its null,

Issue issue = issueManager.getIssueObject( "issueKey" );

remove quotes :)

Issue issue = issueManager.getIssueObject(issueKey);
Andrew Downs July 26, 2018

Ahh cool, thank you.

 

One last question, how would I new update the comments with the data returned or at least a custom field?

 

Basically I have created a custom field called AD Verification, I want to update this field with the results I get back from the reader in the following way:

 

Name      LogonName

User1       user1.user

User2        user2.user

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 26, 2018

If you want to update customfield use

issue.setCustomFieldValue(cf, value)

or to create comment you ll need to use commentManager's create method.

Andrew Downs July 27, 2018

Hi Mark,

 

I don't seem to be getting any joy. I am new to Groovy so that could explain the troubles I am having :) But so we learn.

 

The error is as follows:

Time (on server): Fri Jul 27 2018 14:07:03 GMT+0200 (South Africa Standard Time)

The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.

2018-07-27 14:07:03,865 DEBUG [jira.groovy]: debug statements
2018-07-27 14:07:03,865 DEBUG [jira.groovy]: debug statements
2018-07-27 14:07:03,938 DEBUG [workflow.ScriptWorkflowFunction]: Response is groovyx.net.http.HttpResponseDecorator@2441d6e4
2018-07-27 14:07:03,938 DEBUG [workflow.ScriptWorkflowFunction]: Response is groovyx.net.http.HttpResponseDecorator@2441d6e4
2018-07-27 14:07:03,939 DEBUG [workflow.ScriptWorkflowFunction]: Reader is <redacted>
2018-07-27 14:07:03,939 DEBUG [workflow.ScriptWorkflowFunction]: Reader is <redacted>
2018-07-27 14:07:03,942 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-07-27 14:07:03,942 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2018-07-27 14:07:03,943 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SSD-18229, actionId: 31, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script1253
 at Script1253.userList(Script1253.groovy:68)
 at Script1253$_run_closure1.doCall(Script1253.groovy:55)
 at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
 at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
 at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:285)
 at groovyx.net.http.HTTPBuilder$get$0.call(Unknown Source)
 at Script1253.run(Script1253.groovy:49)
2018-07-27 14:07:03,943 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: SSD-18229, actionId: 31, file: <inline script>
groovy.lang.MissingPropertyException: No such property: customFieldManager for class: Script1253
 at Script1253.userList(Script1253.groovy:68)
 at Script1253$_run_closure1.doCall(Script1253.groovy:55)
 at groovyx.net.http.HTTPBuilder$1.handleResponse(HTTPBuilder.java:503)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223)
 at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165)
 at groovyx.net.http.HTTPBuilder.doRequest(HTTPBuilder.java:515)
 at groovyx.net.http.HTTPBuilder.get(HTTPBuilder.java:285)
 at groovyx.net.http.HTTPBuilder$get$0.call(Unknown Source)
 at Script1253.run(Script1253.groovy:49)

The code is as follows:

 

httpBuilder.get(
    path: "/api/ADUsers",
    requestContentType: ContentType.JSON,
    query: [search: "${fullNameValue}", container: "DC=contose,DC=com"]
){
   resp, reader ->
        userList(resp, reader)
    }

def userList(def resp, def reader){
        
        log.debug "Response is $resp"
        log.debug "Reader is $reader"
        
        def name = reader.name
        def logonName = reader.samAccountName

        
        // Update field
        def ADVerification = customFieldManager.getCustomFieldObjectByName("AD Verification");
         def updateValue = (name = '') ? 'An account with ${name} and ${logonName} has been found.' : 'No existing accounts have been found'
    
           issue.setCustomFieldValue(ADVerifiation,name)
}

 

I am trying to do this within the response I get back from the http get request, so that might also be a problem. But if I move the update out of the http get it says that it cannot find the variables I am referencing. Which then brings in another question, how to I use results from a http get in a value outside of the get request. Hope that makes sense.

Thank you for the assistance, it is highly appreciated.

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 27, 2018

Have a look on this code. You think in right way :) there are only two minor errors

userList is a method and it did not have customFieldManager in it. So it gives you an error about that. So you can pass customFieldManager as parameter or define it inside this method.

And second one for compare two variables you shoul use double equal sign.

So code should be like this

httpBuilder.get(
path: "/api/ADUsers",
requestContentType: ContentType.JSON,
query: [search: "${fullNameValue}", container: "DC=contose,DC=com"]
){
resp, reader ->
userList(resp, reader)
}

def userList(def resp, def reader){

log.debug "Response is $resp"
log.debug "Reader is $reader"

def name = reader.name
def logonName = reader.samAccountName

def customFieldManager = ComponentAccessor.getCustomFieldManager()
// Update field
def ADVerification = customFieldManager.getCustomFieldObjectByName("AD Verification");
def updateValue = (name == '') ? 'An account with ${name} and ${logonName} has been found.' : 'No existing accounts have been found'

issue.setCustomFieldValue(ADVerifiation,name)
}
Andrew Downs August 2, 2018

Hi Mark,

 

Sorry for the late response. I have tested the changes and it is working as far as updating the custom field goes. Still battling to get the comments to work, but that is ok for now.. I am going to mark my initial problem as solved and if I still do not come right with the comments I will raise a new question.

 

Thank you for the assistance, it is appreciated and has been most informative.

Mark Markov
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 2, 2018

You re welcome!

If it will help, here is example how create comment from script console

import com.atlassian.jira.component.ComponentAccessor

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issue = ComponentAccessor.getIssueManager().getIssueObject("TEST-1")
def commentBody = "Some test comment body"

commentManager.create(issue, user, commentBody, false)
Andrew Downs August 2, 2018

Thanks Mark, will give it a try and let you know.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events