Can i update custom field value of Parent Link type via workflow?

Jonas Gomes June 16, 2017

Hi,

 

i'm trying copy value of a custom field to another custom field, this another is a Parent Link custom field. 
I tried of 2 methods: 
1) i tried a copy value of custom field to another custom field using post function, no success...

2) same method, but using sample groovy with updateCustomFieldValue()

using this code: 

def issue = ComponentAccessor.getIssueManager().getIssueObject("T-7665") as MutableIssue
def cfEpicLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(13982L)
def cfParentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(23280L)

def changeHolder = new DefaultIssueChangeHolder();
cfParentLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(cfParentLink), cfEpicLink.getValue(issue)),changeHolder);

 

I got the same error using this 2 ways. Check Out

 

2017-06-16 18:59:18,552 WARN [common.UserScriptEndpoint]: Script console script failed: java.lang.ClassCastException: com.atlassian.jira.issue.IssueImpl cannot be cast to com.atlassian.rm.jpo.env.issues.SimpleIssueAttributes at com.atlassian.rm.jpo.customfields.parent.ParentCustomFieldType.createValue(ParentCustomFieldType.java:17) at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:692) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:409) at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:395) at com.atlassian.jira.issue.fields.OrderableField$updateValue.call(Unknown Source)

Is impossible to do this or have a way ?

Gratz

4 answers

1 vote
alexey_pelykh December 24, 2019

There's a more straightforward way to set Parent Link via script:

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLinkField = customFieldManager.getCustomFieldObject(10200L)
def parentLinkFieldType = parentLinkField.getCustomFieldType()

def newParentLink = some issue
if (newParentLink) {
newParentLink = parentLinkFieldType.getSingularObjectFromString((newParentLink as Issue).key)
}

def mutableIssue = issueManager.getIssueObject(event.issue.id)
mutableIssue.setCustomFieldValue(parentLinkField, newParentLink)
issueManager.updateIssue(opsbot, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
1 vote
Steven F Behnke
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.
June 16, 2017

It's very possible. You're trying to push a round peg into a square hole though. That's why you're getting a Class Cast Exception.

I'm not sure, you should log the values so you can get a better idea of what's happening. It appears that Epic Link field from JIRA Software accepts/returns an IssueImpl object. However, this contrasts with the Parent Link from Portfolio: This field accepts/returns a "SimpleIssueAttributes" object.

You'll have to figure out how to map between the two of them yourself. If you post some results of logging the values of the objects, perhaps I can help more.

Jonas Gomes June 19, 2017

Thanks for asnwer Steven

I managed via rest, after so many attempts it was.

 

Thanks for attention anyway

Katya Wegner June 30, 2017

Hi Steven, Jonas,

I have the same problem, but it seems like I am not able to solve that problem on my own :(.

I can't find any documentation about the SimpleIssueAttributes class and so, I am not able to find out the way to cast correcty... 

Jonas, how did you solve this via rest?

We want to use different issuetypes for different departments and I want to make it as easy as possible to use the issues and Portfolio so there can't be any excuses for not using it.... ;)

Would be great if you have an advice or something...

Regards

Katya

 

JonasGomes June 30, 2017

Hi Katya, 

I relied on this documentation, where we talk about the update custom field via rest. Look at my code that solved the problem. 

Check this documentation: https://docs.atlassian.com/jira/REST/cloud/

import groovy.json.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def newParentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(0000L) <- here is ID custom field what u have to update.
def jsonObj = new JsonSlurper().parseText('{ "fields": { "customfield_23280": "'+newParentLink+'"} }') //customfield_23280 is Parent Link ID
def parentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(23280L) <- id parent link custom field

def userName = "user"
def password = "password"
def fullAddress = "youadress/jira/rest/api/2/issue/${issue.key}"
def authString = "${userName}:${password}".getBytes().encodeBase64().toString()

def client = new RESTClient(fullAddress)

try{
//necessary to overwrite the value of custom field, via rest only update if custom field have value null.
parentLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(parentLink), null),new DefaultIssueChangeHolder())
client.put(path: fullAddress,
contentType: groovyx.net.http.ContentType.JSON,
body: jsonObj,
headers: [Accept: 'application/json', Authorization: "Basic ${authString}"]
)
}catch(final HttpResponseException e){
log.warn e
}

i hope this helps you get the solution. 

Good Luck :)

Mamta Verma October 1, 2018

@Jonas Gomes Hi Jonas , We are facing the same issue as above and trying out your code to resolve it, but we are getting a parsing error exception as below:

Error parsing 'text/html;charset=UTF-8' response
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

The current character read is '<' with an int value of 60
Unable to determine the current character, it is not a string, number, array, or object
line number 11
index number 10
<html>
^
at groovy.json.internal.JsonParserCharArray.decodeValueInternal(JsonParserCharArray.java:207)
at groovy.json.internal.JsonParserCharArray.decodeValue(JsonParserCharArray.java:158)
at groovy.json.internal.JsonParserCharArray.decodeFromChars(JsonParserCharArray.java:46)
at groovy.json.internal.JsonParserCharArray.parse(JsonParserCharArray.java:386)
at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:125)
at groovy.json.JsonSlurper.parse(JsonSlurper.java:221)
at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:280)

link for code and error: https://community.atlassian.com/t5/Marketplace-Apps-questions/Updating-parent-link-of-portfolio-through-Groovy-Script-by/qaq-p/901903#M48097

Do you have any idea , how to fix this error. We tried different approaches but no luck!!!

Any help would be appreciated. Thanks in advance

JonasGomes October 1, 2018

Hi Mamta, have you tried put that URL in the browser? I think that response is returning a HTML instead JSON, because u have this Exception 

Error parsing 'text/html;charset=UTF-8' response
groovy.json.JsonException: Unable to determine the current character, it is not a string, number, array, or object

U have to test using CURL before put in the Code, my first idea :D

 

Have a nice day Mamta.

Mamta Verma October 3, 2018

@Jonas Gomes Thanks Jonas it worked for us :)

zaharovvv_suek_ru
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.
September 30, 2019

@Jonas Gomes   @Mamta Verma 

Hey, guys!

How did you solve your problem?

 

I've tried to insert my fullAddress into browser and browser shows correctly JSON response.

My fullAddress looks like this:

https://onetwo.com/rest/api/2/issue/${issue.key}

My code looks like this:

 

import groovy.json.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Logger

def log = Logger.getLogger("com.acme.CreateSubtask")

def issue = ComponentAccessor.getIssueManager().getIssueObject("TS-74")
log.info('Current issue is: ' + issue)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

//def newParentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(0000L) // <- here is ID custom field what u have to update.
def jsonObj = new JsonSlurper().parseText('{ "fields": { "customfield_10976": "GP-259"} }') //customfield_23280 is Parent Link ID
def parentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(10976L) // <- id parent link custom field

def userName = "user"
def password = "password"
def fullAddress = "https://onetwo.com/rest/api/2/issue/${issue.key}"
def authString = "${userName}:${password}".getBytes().encodeBase64().toString()

log.info(fullAddress);
log.info("parentLink ${parentLink}")

def client = new RESTClient(fullAddress)

try{
//necessary to overwrite the value of custom field, via rest only update if custom field have value null.
parentLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(parentLink), null),new DefaultIssueChangeHolder())
client.put(path: fullAddress,
contentType: groovyx.net.http.ContentType.JSON,
body: jsonObj,
headers: [Accept: 'application/json', Authorization: "Basic ${authString}"]
)
} catch (final HttpResponseException e) {
log.warn e
}







0 votes
JonasGomes October 1, 2018

Nice Job Kevin. 

0 votes
Kevin Dalton September 27, 2018

Has anyone been able to figure out how to get IssueImpl to cast to SimpleIssueAtributes?

 

We are trying to set the Parent Link based on an issue link and continue to run into this issue.

Kevin Dalton October 1, 2018

We were able to do this through the API as well.

 

Snippet:

 

 def userName = "username"
def password = "password"
def authString = "${userName}:${password}".getBytes().encodeBase64().toString()
def jiraBaseURL = 'https://jira.mycompany.com'
def jsonSlurper = new JsonSlurper()
def jsonObj = new JsonSlurper().parseText('{ "fields": { "customfield_17617": "'+ParentIssue+'"} }') //customfield_17617 is Parent Link ID
log.debug ("jsonStr..." + jsonObj)
def client = new RESTClient(jiraBaseURL)

try{
PL.updateValue(null, uissue, new ModifiedValue(uissue.getCustomFieldValue(PL), null),changeHolder)
client.put(path: "/rest/api/latest/issue/"+uissue,
contentType: groovyx.net.http.ContentType.JSON,
body: jsonObj,
headers: [Accept: 'application/json', Authorization: "Basic ${authString}"]
)
}catch(final HttpResponseException e){
log.warn e
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events