Unable to update custom field using scriptrunner for JIRA 6.1.9 to 7.3.6 upgrade

Zubair Patel May 22, 2017

I am working on upgrade JIRA 6.1 to 7.3 and changing scripts.  Here is my complete script, there are no errors but after script execution only numeric fields are updated not the cascadingselect.  Please note not errors are reported by script execution.

//package cname;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;

import org.apache.log4j.Logger
import org.apache.log4j.Category;

import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult

import com.atlassian.jira.issue.customfields.MultipleSettableCustomFieldType
import com.atlassian.jira.issue.customfields.impl.MultiSelectCFType
import com.atlassian.jira.issue.customfields.impl.CascadingSelectCFType
import com.atlassian.jira.issue.customfields.impl.MultiUserCFType
import com.atlassian.jira.issue.customfields.impl.SelectCFType
import com.atlassian.jira.issue.customfields.impl.TextCFType
import com.atlassian.jira.issue.customfields.impl.DateCFType
import com.atlassian.jira.issue.customfields.impl.NumberCFType
import com.atlassian.jira.issue.customfields.option.Option

import java.sql.Timestamp
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

def auto_user = "admin"
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")

MutableIssue issue = ComponentAccessor.issueManager.getIssueObject("DS-4444")
String returnMessage = ""

String ReferenceID = ""
def issueInputParameters = new IssueInputParametersImpl()
//log.warn "HandleDataSourcesEvent ========>>> Create: " + issue.getDescription()
// Set Issue parameter from details received in description (Email Body)
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueService = ComponentAccessor.getIssueService()
def thisUser = ComponentAccessor.getUserManager().getUserByKey(auto_user)
assert thisUser // can't find the user specified
String issueDescription = issue.getDescription()
if (issueDescription) {
def tokens = issueDescription.tokenize("\n")
tokens.each{
def keyValue = it.tokenize(":")
if (keyValue.size() == 2){
def key = keyValue[0].trim()
def value = keyValue[1].trim()
if (key.size() && value.size()){
//log.warn "HandleDataSourcesEvent: ${key} = ${value}"
if (key.toLowerCase().contains("category")){
key = "SDR Category"
}
//returnValue = returnValue + key + "=" + value + "\n"
CustomField customField = customFieldManager.getCustomFieldObjectByName(key)
if (customField){
if (customField.getCustomFieldType() instanceof CascadingSelectCFType){
//Map<String,Object> newValues = new HashMap<String, String>();
def cascadeValue = value.tokenize(";")
//Option parentOptionObj = optionsManager.getOptions(customField.getRelevantConfig(issue)).getOptionForValue(cascadeValue[0].trim(),null)
//Option childOptionObj = optionsManager.getOptions(customField.getRelevantConfig(issue)).getOptionForValue(cascadeValue[1].trim(),parentOptionObj.optionId)
//newValues.put(null, parentOptionObj); //input value in first dropdown
//newValues.put("1", childOptionObj); // input value in second dropdown
//customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), newValues), changeHolder)
issueInputParameters.addCustomFieldValue(customField.getId() + ":0", cascadeValue[0].trim())
issueInputParameters.addCustomFieldValue(customField.getId() + ":1", cascadeValue[1].trim())
returnMessage = value
} else if (customField.getCustomFieldType() instanceof DateCFType){
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
Date date = dateFormat.parse(value);
def date1 = new Timestamp(date.getTime())
//customField.updateValue(null, issue, new ModifiedValue("", date1), changeHolder)
issueInputParameters.addCustomFieldValue(customField.getId(), value)
} else if (customField.getCustomFieldType() instanceof NumberCFType){
//customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), Double.parseDouble(value)), changeHolder)
issueInputParameters.addCustomFieldValue(customField.getId(), value)
} else if (customField.getCustomFieldType() instanceof SelectCFType){
//Option optionObj = optionsManager.getOptions(customField.getRelevantConfig(issue)).getOptionForValue(value,null)
//customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), optionObj), changeHolder)
issueInputParameters.addCustomFieldValue(customField.getId(), value)
} else {
if (key.toLowerCase().contains("reference")){
ReferenceID = value
}
//customField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(customField), value), changeHolder)
issueInputParameters.addCustomFieldValue(customField.getId(), value)
}
}
}
}
}

try {
//issueInputParameters.setRetainExistingValuesWhenParameterNotProvided( true, true )
//UpdateValidationResult updateValidationResult = issueService.validateUpdate(user, 12345L, issueInputParameters);
log.warn "Verify udate${issue.key}"
UpdateValidationResult updateValidationResult = issueService.validateUpdate(thisUser, issue.getId(), issueInputParameters);
if (!updateValidationResult.isValid()) {
log.warn "could not update ${issue.key} " + updateValidationResult.getErrorCollection()
}
log.warn "Update verified ${issue.key}"
IssueResult updateResult = issueService.update(thisUser, updateValidationResult)
if (!updateResult.isValid())
log.warn"Could not set resolution on issue: ${updateResult.errorCollection}"
else
log.warn "${issue.key} updated successfully!!!"

}
catch (e) {
log.warn e.message
}
}

return returnMessage

 

I am running in console and following are log messages

2017-05-22 09:08:26,970 WARN [groovy.PostFunction]: Verify udateDS-4444
2017-05-22 09:08:27,004 WARN [groovy.PostFunction]: Update verified DS-4444
2017-05-22 09:08:27,006 WARN [groovy.PostFunction]: DS-4444 updated successfully!!!

 

Funny thing is I put invalid data and still it is not showing any errors.  I have latest scriptrunner plugin and JIRA 7.3.6.  

1 answer

0 votes
Aidan Derossett [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.
May 22, 2017

Hey Zubair,

I don't really see anything alrmingly incorrect or improper with your code. Would you mind providing a little bit more context concerning the use of this script? How is it being used? Is it a Script Field or just a behavior on some custom field? 

Additionally, it would be really helpful if you could provide an illistration of how the script is meant to function. Could you provide a simple trace of some small example so that I can better understand its use?

Zubair Patel May 22, 2017

Aidan,

I have two diffrent JIRA owned by two diffrent organization behind respective firewall and they need to transfer data back and forth.  So I have email listner on both side which sends data to other side.  When issue gets created via email, I have Listner which needs to fill in field values.  I have custom filed of diffrent type and in this script I am attempting to fill those in.  I am running this script in console to make sure it works before I put the code in listner.  Since no errors are generated, I am not sure where to look. Here are issue details in picture.  Thanks for your help.  

IssueUpdate.PNG

Zubair Patel May 22, 2017

Here is another simple script which increments "Followup Count" (a Numeric custom field).  I am having similar problem, no errors are reported, comment gets inserted but nothing set for "Followup Count" custom field.  Please help.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.*;
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService.CreateValidationResult
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult

MutableIssue issue = ComponentAccessor.issueManager.getIssueObject("DS-4444")

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField cfFollowupCount = customFieldManager.getCustomFieldObjectByName("Followup Count")

def thisUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueService = ComponentAccessor.getIssueService()

Long totalFolloupCount = 0L

if (cfFollowupCount != null)
totalFolloupCount = (Long) issue.getCustomFieldValue(cfFollowupCount) // number of followup so far

if (totalFolloupCount)
totalFolloupCount = totalFolloupCount + 1L // Add one more followup
else
totalFolloupCount = 1L //(Long) Double.parseDouble("1");

log.warn "totalFolloupCount : ${totalFolloupCount}"

def issueInputParameters = new IssueInputParametersImpl()
String strFollowupCount = totalFolloupCount.toString()
log.warn "totalFolloupCount = $strFollowupCount"
issueInputParameters.addCustomFieldValue(cfFollowupCount.getId(), strFollowupCount)
issueInputParameters.setComment("Test")
issueInputParameters.setRetainExistingValuesWhenParameterNotProvided( true, true )
UpdateValidationResult updateValidationResult = issueService.validateUpdate(thisUser, issue.getId(), issueInputParameters);
if (!updateValidationResult.isValid()) {
log.warn "could not update ${issue.key} " + updateValidationResult.getErrorCollection()
}

IssueResult updateResult = issueService.update(thisUser, updateValidationResult)
if (!updateResult.isValid())
log.warn"Could not set resolution on issue: ${updateResult.errorCollection}"
else
log.warn "${issue.key} updated successfully!!!"

return issue.getCustomFieldValue(cfFollowupCount)

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events