Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

ScriptRunner Behaviour question about dependency in a Custom Field

Dani
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 25, 2022

Hello,

I would like on the creation screen of an issue, place a custom field that performs a lookup on a custom field of other type of ticket (one that is already created)  getting his value. With that value, it would use it to autoselect an option in the next field of the creation screen.

I would like to know if it is possible to do this in the creation screen since I am having a lot of problems with this.

I'm trying with ComponentAccessor, but I'm not getting the value of the custom field from the other ticket.

Thank you.

Any help would be appreciated. 

2 answers

1 vote
Bobby Bailey
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.
February 27, 2022

Hi Dani, 

The link Radek has provided you is correct, and provides you with the code you will need. 

From my understanding of your case, you are trying to create an Issue (Lets call this Issue A), and while creating this Issue, you need to get a value from an already existing Issue (Issue B). The value you want from Issue B is on a Custom Field (Custom Field 1), and this value will determine a field while creating Issue A (Custom Field 2).

So, from this link: https://library.adaptavist.com/entity/set-form-field-values-from-issue-in-picker

We can extract the code that gets an issue:

def relatedIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueBKey as String) 

However, I am not sure how you are defining Issue B? However you decide this, you will need to get the key and replace issueBKey with this. 

Once you have Issue B in relatedIssue, you can use the following lines to get the value of the Custom Field on the already existing issue:


def myCustomField = customFieldManager.getCustomFieldObjectsByName(customField2Name)[0]

def customFieldValue = relatedIssue.getCustomFieldValue(myCustomField)

Where customField2Name is the name of the custom field you are getting on Issue B

Once you have this, you can then write some code that determines the new value of Custom Field 2, and set it with the following:

getFieldByName("customField2Name").setFormValue(theValueYouDetermine)

I hope this helps,

Bobby

0 votes
Radek Dostál
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.
February 25, 2022

Well, all I can say is all this is possible, but you didn't describe what it is you're trying to do so there's nothing to suggest. What is the data you're working with, what are those fields, how do you find that issue, do you do any conversions/transformation of values, is there any field type difference, is the user allowed to overwrite the value, if the user has already filled in a value, should it be overwritten or left as was. You're telling us a very high level idea but no actual instructions what to do.

This is probably the closest to the idea: https://library.adaptavist.com/entity/set-form-field-values-from-issue-in-picker

Dani
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 25, 2022

Sorry if I didn't explain myself well.

I want to get the value of a custom field in a specific issue (this custom field determines the location of a project), and depends on whether it is one type or another (one or other country), autoselect an option in the next field in the creation screen.

Thanks a lot.

Radek Dostál
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.
February 25, 2022

I still don't understand a thing. The example from adaptavist library however covers the process, given some customization to this use case of course.

 

I want to get the value of a custom field in a specific issue (this custom field determines the location of a project),

What does the location of a project mean?

So you have a Create Issue form (let's call it "source issue") and a Custom Field "A" - based on the value in this Custom Field A (which will be used to obtain some data from "linked issue"), you want to set a value in Custom Field "B".

What is Custom Field A? Is it an issue picker? Is it a select list? Is it a Text field? If it tells you the location of a project, how does it tell it to the script so it can rigidly understand what the "location" is? What is location? Is it a project key, an issue key?

 

and depends on whether it is one type or another (one or other country)

And this is a select list option? Single select? Text field? Checkbox? What is this field?

 

autoselect an option in the next field in the creation screen.

What is the next field, the Custom Field B? Is it a single choice? Is it the same field type as the field on the linked issue? Is there any mapping or transformation required?

 

The whole question is innocently structured in a way that somebody writes the whole thing, rather than it being a question about why a specific piece of code does not do what it is expected to do. Speaking of which, what is the current code? Which part of it doesn't work or you don't know how to make work? Have you tried any logging? It's as simple as 'log.warn("Value in X: " + myVariable)' and behaviours have execution history including log messages.

Can't write any code without knowing exactly what it is supposed to do and with which exact objects, since object-oriented programming generally can't be based around abstract ideas of copying one thing to another thing without knowing what those things are.

Suggest an answer

Log in or Sign up to answer