I am using a nested ternary to set a value based on another value. Testing the operator in a groovy dev environment works perfectly on it's own. When i implement it in my script, it isn't being evaluated correctly. Am i being tripped up by scriptrunner, groovy truth, Jira or my own stupidity? Please help me solve this mystery!
This is strange to me because the code works fine by itself in an online editor like https://groovyconsole.appspot.com/
********Stand Alone Code in Editor********
def entity = "value 1"
def complianceEmail = (entity == "value 1") ? "email 1" :
(entity == "Value 2") ? "email 1" : "email 3"
print complianceEmail
********END Stand Alone Code in Editor********
However, the statement is not being evaluated correctly in my code. Email 3 is being returned when entity is set to value 1 or value 2. I know I can write this differently since the outcome for value 1 and value 2 should be the same, but this will not always be the case, so i prefer to write it like this for when the outcome for value 2 will change.
Code*************************************************
// all global script vars
groupManager = ComponentAccessor.getGroupManager()
userManager = ComponentAccessor.getUserManager()
issueMgr = ComponentAccessor.getIssueManager()
customFieldManager = ComponentAccessor.getCustomFieldManager()
customerCF = customFieldManager.getCustomFieldObjectByName("Customer Name")
customer = issue.getCustomFieldValue(customerCF)
depositCurrencyCF = customFieldManager.getCustomFieldObjectByName("Deposit Currency")
depositCurrency = issue.getCustomFieldValue(depositCurrencyCF)
disputedAmountCF = customFieldManager.getCustomFieldObjectByName("Disputed Amount")
disputedAmount = issue.getCustomFieldValue(disputedAmountCF)
entityCF = customFieldManager.getCustomFieldObjectByName("Entity")
entity = issue.getCustomFieldValue(entityCF)
resolutionAmountCF = customFieldManager.getCustomFieldObjectByName("Resolution Amount")
resolutionAmount = issue.getCustomFieldValue(resolutionAmountCF)
/**
* sendNotificationToApprovers() Method
* Sends out the Needs Approval notification to the Primary Approver,
* and CCs compliance, Lyn and Olga (CCs are Lyn's request).
* */
def sendNotificationToApprovers() {
def complianceEmail = (entity == "value 1") ? "email 1" :
(entity == "value 2") ? "email 1" : "email 3"
def baseUrl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
def reporter = issue.reporter.displayName
def subject = "A Customer Dispute Resolution Requires Your Approval"
def body = "A proposed resolution for a Customer Dispute requires your approval. <br/><br/>" +
"Click the link below to <strong>Approve</strong> or <strong>Reject</strong> the proposed resolution.<br/><br/>" +
"<strong>Dispute Link: </strong><a href=" + "$baseUrl/browse/$issue.key" + ">$issue.key</a> <br/><br/>" +
"<strong>Submitter: </strong>$reporter <br/><br/>" +
"<strong>Customer: </strong>$customer <br/><br/>" +
"<strong>Entity: </strong>$entity <br/><br/>" +
"<strong>Currency: </strong>$depositCurrency <br/><br/>" +
"<strong>Disputed Amount: </strong>$disputedAmount <br/><br/>" +
"<strong>Resolution Amount: </strong>$resolutionAmount <br/><br/>" +
"<strong>Description: </strong>${issue.description} <br/><br/>"
def toAddress = usersToAdd[0].emailAddress
def ccAddress = "$complianceEmail,obessmertnaya@test.com,lyn@test.com "
def bccAddress = "klucio@test.com"
def mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
if (mailServer) {
Email email = new Email(toAddress, ccAddress, bccAddress)
email.setSubject(subject)
email.setBody(body)
email.setMimeType("text/html")
mailServer.send(email)
} else {
// TODO: Problem getting the mail server from JIRA configuration, log this error
}
log.debug("Entity is $entity")
log.debug("Compliance Address is set to $complianceEmail")
log.debug("Sending notifications - To:$toAddress; CC:$ccAddress BCC:$bccAddress")
}
Logging ******************************
2019-04-24 12:57:00,991 DEBUG [workflow.ScriptWorkflowFunction]: Entity is value 1
2019-04-24 12:57:00,992 DEBUG [workflow.ScriptWorkflowFunction]: Compliance Address is set to email 3
2019-04-24 12:57:00,992 DEBUG [workflow.ScriptWorkflowFunction]: Sending notifications - To:lyn@test.com; CC:email 3,obessmertnaya@ftest.com,lyn@test.com BCC:klucio@test.com
After a lot of banging my head against the wall, I figured out that there wasn't any problem with the logic at all. The problem was that the value for entity is being taken from a select list CF, and is not a String, but an Option. The value was being displayed as a string in the log, and can be u treated as a string in many ways, but it failed the comparison test of groovy truth and the ternary operator.
Obviously, my test code worked because I was passing in a String instead of setting the value from a non-String field.
using
entity = issue.getCustomFieldValue(entityCF).toString()
instead of
entity = issue.getCustomFieldValue(entityCF)
solved the inequality problem
When I use your standalone script in my scriptrunner script console I got "email 1".
But then I changed
def entity = " value 1"
to
def entity = "value 1" //removed the leading space
The return value was email 1.
Could that be the issue?
Maybe this would be better:
def complianceEmail = (entity.toLowerCase().trim() == "value 1") ? "email 1" :
(entity == "value 2") ? "email 1" : "email 3"
Are value 1 and value 2 supposed to both yield email 1?
If so, you could use a construct like this:
def entities = ["value ","value 2"]
def complianceEmail = (entity.toLowerCase().trim() in entities) ? "email 1" : "email 3"
Or, to be more generic and allow for easily adding new values to entities
def entity = " Value 4"
def emailMapping = [
"value 1": "email 1",
"value 2": "email 2",
]
def defaultEmail ="email 3"
def complianceEmail = emailMapping[entity.toLowerCase().trim()] ?: defaultEmail
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you.
The space was a typo when posting. It is not in my code. The result for value 1 and value 2 are to be the same for now, but eventually they will need to be different. I like the idea of having a map, as you suggested, and may do that. I would really like to know why my code, when used in the method/script, does not return the correct result.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.