Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Avoid / add Condition to skip empty field value in groovy script

Omprakash Thamsetty
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.
November 15, 2023

Hi,

 

I am trying to avoid adding the null value in my email reader. Basically, how I can add validator/condition to skip the field value update if there is no value using groovy script?

 

Ex:

 

My email body content is as below

 

Type:Work Request
Summary: - IN211 - IHCDA - IERA - Visionlink - Question
Severity:
Component:IN211
Division:IHCDA - IERA
ContactName:Brenda Gmail
ContactEmail:brenda.havens@gmail.com
Emailcc:

 

I see Severity field and EmailCC fields were empty. How I can add the condition to skip adding in issueobject. I tried with

 

if (Emailcc[1] != null) condition but this giving me error as there is out of

 

ERROR:

 

Exception thrown while executing the script. java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1

 

Added condition as below.

 

How can I skip or add condition to ignore if there is a null/empty

 

Any help much appreciated.

 

//SET CC address

String EmailccMatcher = "Emailcc:.*"

String EmailccMatcherspace = "Emailcc:\s"

String EmailccMatch = body.find(EmailccMatcher)

String EmailccValue = null

String[] Emailcc = null

log.debug "CC email matcher" + EmailccMatcherspace

if(EmailccMatcherspace){

  log.debug "CC email contain null"

}else if (EmailccMatch) {

    //LastNameValue = LastNameMatch.substring(LastNameMatch.indexOf(':'))

    Emailcc = EmailccMatch.split(":")

if (Emailcc[1] != null) {

  def EmailccField = customFieldManager.getCustomFieldObjectsByName("CC Address Emails").first()

 

  issueObject.setCustomFieldValue(EmailccField, Emailcc[1])

}

}

2 answers

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
November 16, 2023

Hi @Omprakash Thamsetty

Could you please provide more information, i.e., what are you using to validate this? Is it a Post-Function or a Validator?

If it is a Post-Function, what Post-Function feature are you using? Else if it is a Validator, what type of validator are you using?

It would be helpful if you could also share a screenshot of your configuration.

I'm looking forward to your feedback and clarification.

Thank you and Kind regards,

Ram

0 votes
Bobby Bailey
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.
November 16, 2023

Hi @Omprakash Thamsetty , 

 

When you use the .split function, it will only fill out the array with the data available. So, if you only have "Emailcc:" your array will only have 1 entry, so trying to access the second value will fail. 

Instead, you can check the length of your array, so instead use this if: 

if(Emailcc.length > 0){

}


This confirms if there is anything after ':'. 

Does this help?

Kind regards, 

Bobby

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.15
TAGS
AUG Leaders

Atlassian Community Events