Adaptivist Custom Script Listener Not Working, What am I doing wrong?

I am trying to loop through a set of custom fields, and if any of them contain the words "Fail" I want to update another custom field with todays date. HEre is my code.

I must be doing something wrong, so would some like to educate me?

 

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = componentManager.getCustomFieldManager()
 
def myFieldList = ["Official Last Date of Attendance (OLDA) P/F","Date of Determination P/F","Funds Used in Calculation (P/F)", "Rate of Progression (P/F)", "Institutional Charges v2", "CB Amount (P/F)", "GOP Amount (P/F)", "PWD Amount (P/F)", "Return to Lender (P/F)"];
for (myFieldName in myFieldList)
{ def tempCF = customFieldManager.getCustomFieldObjectByName(myFieldName);
def tempVAL = targetIssue.getCustomFieldValue(tempCF);
if (tempVAL?.value=="Fail"){
def paymentDate = new Date()
def tempDFC = customFieldManager.getCustomFieldObjectByName("Date Form Completed");
if (paymentDate){ 
def cal = new java.util.GregorianCalendar();
cal.setTimeInMillis(paymentDate.getTime()); 
cal.add(java.util.Calendar.DAY_OF_MONTH, 0);
def newDFC = return new java.sql.Timestamp(cal.getTimeInMillis()); 
issue.setCustomFieldValue(tempDFC, newDFC)
} 
else { 
return null 
}}
}

1 answer

0 vote
Vasiliy Zverev Community Champion Nov 30, 2016

You should start with types of these custom fields. Are you sure that are all return string?

Yes they do. They literally all have the same options: Pass or Fail

Vasiliy Zverev Community Champion Nov 30, 2016

I refactored your code and comment some lines with strange instructions:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def myFieldList = ["Official Last Date of Attendance (OLDA) P/F"
                   ,"Date of Determination P/F"
                   ,"Funds Used in Calculation (P/F)"
                   , "Rate of Progression (P/F)"
                   , "Institutional Charges v2"
                   , "CB Amount (P/F)"
                   , "GOP Amount (P/F)"
                   , "PWD Amount (P/F)"
                   , "Return to Lender (P/F)"];
//Variables should be defined before loop 
CustomField tempDFC = customFieldManager.getCustomFieldObjectByName("Date Form Completed");
CustomField tempCF;
Option tempVAL; 

for (myFieldName in myFieldList){  
    tempCF = customFieldManager.getCustomFieldObjectByName(myFieldName);
    tempVAL = (Option) targetIssue.getCustomFieldValue(tempCF);
    if (tempVAL?.getValue() == "Fail"){
        def paymentDate = new Date()
        
        if (paymentDate){ //what this line for
            def cal = new java.util.GregorianCalendar();
            cal.setTimeInMillis(paymentDate.getTime());
            cal.add(java.util.Calendar.DAY_OF_MONTH, 0);
            //def newDFC = return new java.sql.Timestamp(cal.getTimeInMillis()); //here is return from methos
            issue.setCustomFieldValue(tempDFC
                    , new java.sql.Timestamp(cal.getTimeInMillis()))
        }
        else {
            return; //what this line for
        }}
}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

102 views 0 2
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you