How Fix Groovy Behaviour Script Error

Shah Baloch February 28, 2024

Hi Community, 

How can I fix the behavior script error? I created a script that adds start and end date and time into a multi-text custom field. I added the script in both the initializer and server-side script for the custom field. The last 15 executions failed from the history, and there is an error. 

2024-02-28 14:11:32,820 ERROR [behaviours.BehaviourManagerImpl]: *************************************************************************************
2024-02-28 14:11:32,823 ERROR [behaviours.BehaviourManagerImpl]: Script function failed on issue: (create issue) project/issuetype: CC/Task, user: jirauser, fieldId: __init__, file: <inline script>, edit behaviour: https://ourjira.instance.com:8443/plugins/servlet/scriptrunner/admin/behaviours/edit/someKindOfCodeHere
java.lang.IllegalArgumentException
at aabbcc112233ddeeff445566gghh778.run(aabbcc112233ddeeff445566gghh778.groovy:23)

Here is the script.

import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FieldBehaviours
import java.text.SimpleDateFormat
import java.util.Date

@BaseScript FieldBehaviours fieldBehaviours

def message = getFieldById("customfield_1001")
def startDate = getFieldById("customfield_11400")
def endDate = getFieldById("customfield_11401")

// Get the raw date values from the startDate and endDate fields
def rawStartDateValue = startDate.getValue()
def rawEndDateValue = endDate.getValue()

// Specify the desired date format (EEEE MM/dd/yyyy hh:mm a for example)
def desiredDateFormat = "EEEE MM/dd/yyyy hh:mm a"

// Create a SimpleDateFormat instance
def dateFormat = new SimpleDateFormat(desiredDateFormat)

// Parse the raw date values and format them
def formattedStartDate = dateFormat.format(new Date(rawStartDateValue.toString()))
def formattedEndDate = dateFormat.format(new Date(rawEndDateValue.toString()))

// Create the table content with the formatted dates
def table = """
||Service Impact||Scheduled Maintenance||
|*What*|The system will be undergoing scheduled maintenance. |
|*When*|{*}From{*}: $formattedStartDate
{*}To{*}: $formattedEndDate
|*Impact*| * Something 1
* Something 2|

{color:#de350b}*Note:* Please contact the Helpdesk if you have any questions.{color}

*Internal Teams:* IT Team
"""

message.setFormValue(table)

How can I fix this error?

Thank you,

 

1 answer

1 accepted

0 votes
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 29, 2024

Hi @Shah Baloch

Please clarify, are you using the same script that you have shared in your description in the Behaviour Initialiser as well as the Server-Side Behaviour?

If yes, it will only work for the Behaviour Initialiser, not the Server-Side Behaviour.

It would be helpful if you could share a screenshot of your Server-Side Behaviour configuration.

I am looking forward to your feedback and clarification.

Thank you and Kind regards,

Ram

Shah Baloch February 29, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ yes I am using same script for both. Here are the screenshots. Thanks

Screenshot 2024-02-29 092832.pngScreenshot 2024-02-29 092856.png

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 4, 2024

Hi @Shah Baloch

After going through your last comment, I can confirm the approach you are taking will not work.

In your case, you are trying to pass the values of two fields, i.e. Start Date and End Date, into the Multi-Line Text Field.

Hence, you must create two Server-Side Behaviours, i.e. for the Start Date field and the second End Date field and not for the Multi-Line Text Field as you have configured.

Also, you must not create a Behaviour Initialser for this as this will implicitly fail since the value set on the Multi-Line text field depends on the Start Date and End Date field values. 

Below is what your Server-Side Behaviour Configuration should look like:-

1. For the Start Date field, below is a sample working code for your reference:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import java.text.SimpleDateFormat

@BaseScript FieldBehaviours behaviours
def startDate = getFieldById(fieldChanged)
def endDate = getFieldByName('End Date')

def sampleMultiLine = getFieldByName('Sample Multi Line')
def simpleDateFormat = new SimpleDateFormat('yyyy-MM-dd')

if (startDate.value && endDate.value) {
def startDateValue = startDate.value as Date
def endDateValue = endDate.value as Date

def startDateFormatted = simpleDateFormat.format(startDateValue)
def endDateFormatted = simpleDateFormat.format(endDateValue)

def message = """
||Service Impact||Scheduled Maintenance||
|*What*|The system will be undergoing scheduled maintenance. |
|*When*|{*}From{*}: ${startDateFormatted}
{*}To{*}: ${endDateFormatted}
|*Impact*| * Something 1
* Something 2|

{color:#de350b}*Note:* Please contact the Helpdesk if you have any questions.
"""

sampleMultiLine.setFormValue(message)
}


Below is a screenshot of the Server-Side Behaviour configuration for the Start Date field:-

start_date_behaviour_config.png

 

2. For the End Date field, below a sample working code for your reference:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import java.text.SimpleDateFormat

@BaseScript FieldBehaviours behaviours
def startDate = getFieldByName('Start Date')
def endDate = getFieldById(fieldChanged)

def sampleMultiLine = getFieldByName('Sample Multi Line')
def simpleDateFormat = new SimpleDateFormat('yyyy-MM-dd')


if (startDate.value && endDate.value) {
def startDateValue = startDate.value as Date
def endDateValue = endDate.value as Date

def startDateFormatted = simpleDateFormat.format(startDateValue)
def endDateFormatted = simpleDateFormat.format(endDateValue)

def message = """
||Service Impact||Scheduled Maintenance||
|*What*|The system will be undergoing scheduled maintenance. |
|*When*|{*}From{*}: ${startDateFormatted}
{*}To{*}: ${endDateFormatted}
|*Impact*| * Something 1
* Something 2|

{color:#de350b}*Note:* Please contact the Helpdesk if you have any questions.
"""

sampleMultiLine.setFormValue(message)

}

Below is a screenshot of the Server-Side Behaviour configuration for the End Date field:-

end_date_behaviour_config.png

Please note that the sample working codes above are not 100% exact to your environment. Hence, you will need to make the required modifications.

If you observe both the code samples above, they are 99% similar. The only difference is in these lines:-

For the Start Date field's Behaviour configuration, the field is declared as:-

def startDate = getFieldById(fieldChanged)

and for the End Date field's Behaviour configuration, the field is declared:-

def endDate = getFieldById(fieldChanged)

 The fieldChanged is required to identify which field has made the change and to update the value in the Multi-Line text field accordingly.

Another point to note, you must include the condition below. This ensures that the behaviour will not trigger until both the Start Date and End Date values are entered. Otherwise, it will return Null Pointer Exceptions.

if (startDate.value && endDate.value) {


}

Below are a couple of test screenshots for your reference:-

1. If I only add the value for either the Start Date or End Date as expected, the Multi-Line text field will not updated as shown in the screenshots below:-

test1.png

test2.png

 

2. Only once the value for both date fields are included will the Multi-Line text field get filled as shown in the screenshot below:-

test3.png

3. If I try to modify the value for the Start Date, it will update the value in the Multi-Line text field as shown in the screenshots below:-

test4.png

test5.png

4. Similarly, if I try to update the value of the End Date, it will display the update in the Multi-Line text field as shown in the screenshots below:-

test6.png

test7.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

 

 

 

 

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 6, 2024

Hi @Shah Baloch

Has your question been answered?

If yes, kindly accept the answer.

Thank you and Kind regards,

Ram

Shah Baloch March 6, 2024

Thank you, @Ram Kumar Aravindakshan _Adaptavist_, it worked great. I have an issue with a listener script related to the same start and date, but I will create a separate post for that. Thank you so much for your help. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events