Scriptrunner Custom Email Subject - Unwanted Spaces

Vivo Bandito September 14, 2023

Hello,

I'm currently working on a sending a custom email with the scriptrunner post function. I'm having some issues with the subject line of the email. Regardless of what I have tested the output into the email always contains two spaces before the text. I have tried using the subject template, setting the subject within the configurations, and even just adding plain text to the subject all resulting in multiple spaces added before the text. 

Any and all help is appreciated. 

Version: Jira server 9.4.5

Scriptrunner: 7.8.0

 

 

Subject Template Plain Text

Notification

 Output: "  Notification"

 

Subject Template: 

<%// components

    def notificationValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Notification Email")?.getValue(issue)?.toString()

    def resolvedValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Resolve")?.getValue(issue)

    def updateValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Business Notification Update Select")?.getValue(issue)?.toString()

    def correctValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Business Notification Correction")?.getValue(issue)?.toString()

    def summaryValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Notification Summary")?.getValue(issue)?.toString()

// Notification Mapping

    def notificationMappings = [

    "Proactive Maintenance":"Notification - {notificationValue} - {summaryValue}{status}"

    ]  

// Status Conditions

    def status = ""

    if (correctValue == "Yes") {

        status = " - CORRECTION"

    } else if (resolvedValue != null) {

        status = " - RESOLVED"

    } else if (updateValue == "Yes" && resolvedValue == null) {

     status = " - UPDATE"

    }

// Retrieve the output string from the map

    def outputString = notificationMappings.get(notificationValue)

    if (outputString) {

// Replace placeholders with actual values

    outputString = outputString.replace("{notificationValue}", notificationValue)

                               .replace("{summaryValue}", summaryValue)

                               .replace("{status}", status)

    out << outputString

    }%>

Output: "  Notification - etc..."

 

Configuration

import com.atlassian.jira.component.ComponentAccessor

// Get the "Bcc.." custom field

    def multiSelectAddressesCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Bcc..")

    // Check if the custom field value is not empty

    if (issue.getCustomFieldValue(multiSelectAddressesCF)) {

    // Retrieve the email addresses from the custom field

    def recipients = issue.getCustomFieldValue(multiSelectAddressesCF)?.collect {

        it.emailAddress

    }?.join(",")

    // BCC Email List

    mail.setBcc(recipients)

    } else {

    // Custom field is empty, log the condition

    log.warn("Skipping BCC setting because the 'Bcc..' custom field is empty.")

    }

  // components

    def notificationValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Notification Email")?.getValue(issue)?.toString()

    def resolvedValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Resolve")?.getValue(issue)

    def updateValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Business Notification Update Select")?.getValue(issue)?.toString()

    def correctValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Business Notification Correction")?.getValue(issue)?.toString()

    def summaryValue = com.atlassian.jira.component.ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Notification Summary")?.getValue(issue)?.toString()

// Notification Mapping

    def notificationMappings = [

    "Proactive Maintenance":"Notification - {notificationValue} - {summaryValue}{status}"

    ]  

// Status Conditions

    def status = ""

    if (correctValue == "Yes") {

        status = " - CORRECTION"

    } else if (resolvedValue != null) {

        status = " - RESOLVED"

    } else if (updateValue == "Yes" && resolvedValue == null) {

     status = " - UPDATE"

    }

// Retrieve the output string from the map

    def outputString = notificationMappings.get(notificationValue)

    if (outputString) {

// Replace placeholders with actual values

    outputString = outputString.replace("{notificationValue}", notificationValue)

                               .replace("{summaryValue}", summaryValue)

                               .replace("{status}", status)

    mail.setSubject(outputString)

    }

Output: "  Notification - etc..."

 

2023-09-14 09_28_32-!Staff - DMcVay@solarityhealth.com - Outlook.png

1 answer

1 accepted

2 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.
September 15, 2023

Hi @Vivo Bandito

For your subject template and configuration, can you modify the outputString variable slightly with:-

outputString[1..outputString.length() - 1]

So that it will remove the first empty character.

Also, please update your ScriptRunner plugin to the latest release, i.e. 8.11.0 so you can use ScriptRunner's HAPI feature and simplify your code.

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

Thank you and Kind regards,

Ram

Vivo Bandito September 15, 2023

Hey @Ram Kumar Aravindakshan _Adaptavist_ 

That worked! Thank you for the help. 

Also we are currently waiting until moving to DC as we expect some scripts to fail going to the latest release. When moving we'll have the appropriate time to fix as we go. I'm looking forward to HAPI!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events