Email template Post function - If statement

Arthur SALMON October 14, 2019

Hi community,

 

[Jira version : v7.8.0#78000 - ScriptRunner version : 5.3.7]

I am trying to use the if statement in Jira workflow post function - send custom email. I was trying to use the if condition directly into the template to display values only if the value of the customField is not empty but I can't find the right way to make it.

And I am not sure if it's better to use "condition and configuration" or directly "email template" to do that.

 

To explain a bit more, I have created a Jira project to do visit request in the company and I require for each visitors Last Name, First Name, Company and Nationality. The thing is, for each request there can be up to 6 visitors so I did something like :

- Last Name 1

- First Name 1

- Company 1

- Nationality 1

and I have created 6 tabs and I increment the number for each. But only the first visitor is requested to be filled as we can create a request for only 1 visitor but we won't request for no visitor.

 

Today I receive that :

A non UE resident request visit as been validated. 
- Mary Poppins Disney American
- null null null UE
- null null null UE
- null null null UE
- null null null UE
- null null null UE

And I would like to remove all lines with no values in the email sent.

 

Thank in advance for the help, 

1 answer

1 accepted

2 votes
Answer accepted
Arthur SALMON October 15, 2019

I figured it out !

 

I filled the "Condition and Configuration" field with this :

import com.atlassian.jira.component.ComponentAccessor

//Verify if the nationality "precision" field is filled or not
def preciValue1 = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Precision 1"))
if (preciValue1){
config.precision1 = preciValue1
}else{
config.precision1 = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Nationality 1"))
}

//Create the rest of the text to display depending is there is values or not.
def text = ""
for(def i = 2; i < 7; i++){
def lastNameVal = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Last Name ${i}"))
if(lastNameVal){
def firstNameVal = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("First Name ${i}"))
def companyVal = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Company ${i}"))
def precisionVal = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Precision ${i}"))
if(!precisionVal){
precisionVal = issue.getCustomFieldValue(ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Nationality ${i}"))
}
text = text + "Name : " + lastNameVal + " " + firstNameVal + "\rCompany : " + companyVal + "\rNationality : " + precisionVal + "\r\r"
}
}
config.textDisplay = text

and the email template with this :

 

A non UE resident request visit as been validated.

Name : ${cfValues['Last Name 1']} ${cfValues['First Name 1']}
Company : ${cfValues['Company 1']}
<% if(precision1) out << "Nationality: " << precision1 %>

<% if(textDisplay) out << textDisplay %>

Regards,

 

This way, I have the Visitor 1 display all the time and the others displayed only if they are defined.

Hope this will help some people to use "send custom email" function a little bit deeper.

Suggest an answer

Log in or Sign up to answer