Trying to add an "if" statement when using the tgtField.updateValue in a JIRA Post Function script.

Wayne Cranford March 8, 2018

I am a non-software developer that has responsibility for creating workflows for our Engineering and Quality department.  I have been using Script Runner for several years, and so far I have been able to follow examples and direction on this forum to solve my simple needs.   Now I have a more complicated need to populate a custom unlimited text field with several fields on a single line, and while I do see examples similar to what I think I need, I am unable to get it working. Have spent about 3 days trying to hack a solution, including using input from software-developer types in our company, but they are not familar with JIRA.

 

Need: I need to build a single line of information into a unlimited text field (Temp/Partial Resume Request) that includes the Date, 1 Checkbox field, 1 Radio Button, 1 single text field and 2 unlimited text fields. One of the unlimited text (Special Instructions, field 6 in the below) is not required, so if field is not populated then the String field6Value does not need to be included in the line of information.  I need a solution for an "if" statement to only include this information if populated. 

I have made numerous attpemts based on what I pulled from the forum, and here is the last thing I tried:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat
IssueManager issueManager = ComponentAccessor.getIssueManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Date date = new Date();
CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Type"}
CustomField field3 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Resume Temp P/N(s)"}
CustomField field4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume For"}
CustomField field5 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Qty"}
CustomField field6 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Special Instructions"}

String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = " : Temp " + (String)issue.getCustomFieldValue(field2)
String field3Value = " for " + (String)issue.getCustomFieldValue(field3)
String field4Value = ", " + (String)issue.getCustomFieldValue(field4)
String field5Value = ", QTY: " + (String)issue.getCustomFieldValue(field5) + " units"
String field6Value = ", Spcl Instructions: " + (String)issue.getCustomFieldValue(field6)
String field7Value = ""
String lineSeparator = System.getProperty("line.separator")

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
def changeHolder = new DefaultIssueChangeHolder();

if(field6 == null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field7Value + date + field2Value + field3Value + field4Value + field5Value),changeHolder);
}
if(field6 != null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field7Value + date + field2Value + field3Value + field4Value + field5Value + field6Value),changeHolder);
}

 

If the Special Instructions is populate, this is the output needed:image.png

If the Special Instructions field is not populate, do not want it appearing, but here is the current output:

image.png

 

We are at 7.5.4 for JIRA and 5.3.5 for Adaptavist ScriptRunner for JIRA and all feedback are appreciated.

1 answer

0 votes
Joshua Yamdogo @ Adaptavist
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.
March 12, 2018

Hi Wayne,

I think the problem here is that you are checking to see if the field 'field6' is null. 'field6' itself won't be null, because it exists. The value of field6 will be null depending on whether or not is is populated. Therefore I think you should simply change your if statements to check for the value of field6:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat
IssueManager issueManager = ComponentAccessor.getIssueManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Date date = new Date();
CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Type"}
CustomField field3 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Resume Temp P/N(s)"}
CustomField field4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume For"}
CustomField field5 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Qty"}
CustomField field6 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Special Instructions"}

String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = " : Temp " + (String)issue.getCustomFieldValue(field2)
String field3Value = " for " + (String)issue.getCustomFieldValue(field3)
String field4Value = ", " + (String)issue.getCustomFieldValue(field4)
String field5Value = ", QTY: " + (String)issue.getCustomFieldValue(field5) + " units"
String field6Value = ", Spcl Instructions: " + (String)issue.getCustomFieldValue(field6)
String field7Value = ""
String lineSeparator = System.getProperty("line.separator")

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
def changeHolder = new DefaultIssueChangeHolder();

if(issue.getCustomFieldValue(field6) == null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field7Value + date + field2Value + field3Value + field4Value + field5Value),changeHolder);
}
if(issue.getCustomFieldValue(field6) != null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field7Value + date + field2Value + field3Value + field4Value + field5Value + field6Value),changeHolder);
}

   

 

Wayne Cranford March 12, 2018

Joshua - thank you and it took several attempts but was able to get it working as needed.

While I was not able to find an example based on your feedback (my lack of overall understanding), it made sense that I need to check the value of that field.  Not sure if this was the best or the easiest path, but I defined "checkvalid" for the value of field6, then simplified the "if statements" to determine whether is is null or not (Bold and Underlined) below. 

Here is what I would up with:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import java.text.SimpleDateFormat
IssueManager issueManager = ComponentAccessor.getIssueManager()

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
Date date = new Date();
CustomField field1 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
CustomField field2 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Type"}
CustomField field3 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Resume Temp P/N(s)"}
CustomField field4 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume For"}
CustomField field5 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp Resume Qty"}
CustomField field6 = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Special Instructions"}
def checkvalid = issue.getCustomFieldValue(field6)

String field1Value = (String)issue.getCustomFieldValue(field1)
String field2Value = " : Temp " + (String)issue.getCustomFieldValue(field2)
String field3Value = " for " + (String)issue.getCustomFieldValue(field3)
String field4Value = ", " + (String)issue.getCustomFieldValue(field4)
String field5Value = ", QTY: " + (String)issue.getCustomFieldValue(field5) + " units"
String field6Value = ", Spcl Instructions: " + (String)issue.getCustomFieldValue(field6)
String field7Value = ""
String lineSeparator = System.getProperty("line.separator")

def tgtField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Temp/Partial Resume Request"}
def changeHolder = new DefaultIssueChangeHolder();

if (checkvalid == null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field7Value + date + field2Value + field3Value + field4Value + field5Value),changeHolder);
}
if (checkvalid != null) {
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), field7Value + date + field2Value + field3Value + field4Value + field5Value + field6Value),changeHolder);
}

Joshua Yamdogo @ Adaptavist
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.
March 12, 2018

Hi Wayne,

That should be fine. Generally, what you suggest is preferable over what I suggested. :)

Are you needing any more help or do you have it all sorted out now?

Thanks,

Josh

Wayne Cranford March 12, 2018

Joshua,

This has resolved my current need and allowed for us to proceed with scheduling a demostration of a new workflow to our Leadship Team. We are starting to add items like this to workflows to help with the user experience, and there will be additional questions in future, but this can be considered closed/resolved,

Thanks,
Wayne

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events