Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,557,818
Community Members
 
Community Events
184
Community Groups

ScriptRunner Initalizer Behaviour to not pre-fill if value present

Morning,

I'm setting up a Behaviour with an initalizer that I want to check if a specific custom field has a value or not (null). In the instance where there is value already present in the custom field then I don't want the initalizer to run.

// Retrieve the value of the customfield

def outcome = getFieldByName('Board Summary Outcome for Applicant').getValue()

if(outcome != null ) {return}

// It's null so let's add some default text

getFieldByName('Board Summary Outcome for Applicant').setFormValue("Dummy Text Here.")

 

The code above looks reasonable however what I'm finding is it works perfectly when there is no value in the custom field (tick) but if the field has an entry (i.e. text) then that text is updated with the "Dummy Text Here" which is not the behaviour that I want.

Any help would be much appreciated, for whatever reason the outcome variable always resolves to null

1 answer

1 accepted

2 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 28, 2020

Behavior initializer run before the fields are populated from the data associated with the issue (from the database).

So to check if a field already has a value in the initializer, you want to look at the binding variable "underlyingIssue" and load those values from that issue object

Something like

def fieldName = 'Board Summary Outcome for Applicant'
def outcome
if(underlyingIssue){ 
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(underlyingIssue).find{it.name == fieldName}
def outcome = underlyingIssue.getCustomFieldValue(cf)
} else {
//underlyingIssue is null on issue create screen
outcome = ''
}
if(!outcome){
getFieldByName(fieldName).setFormValue("Dummy Text Here.")
}

Thanks Peter - worked a treat. I've modified your initial code block slightly as you defined outcome twice and the block is missing an import to enable the use of the ComponentAssessor.

 

import com.atlassian.jira.component.ComponentAccessor;

def fieldName = 'Board Summary Outcome for Applicant'
def outcome

if(underlyingIssue){

def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(underlyingIssue).find{it.name == fieldName}
outcome = underlyingIssue.getCustomFieldValue(cf)

} else {
outcome = ''
}
if(!outcome){
getFieldByName(fieldName).setFormValue("Dummy Text Here.")
}
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 28, 2020

Yeah I wrote all that directly in the answer box... not in an actual environment, so it was all from memory. I assumed you might have had the ComponentAccessor import already for other aspects of your script.
The double def was just me deciding to define it outside the if block mid-way through my response and not being very diligent in reviewing.

 

Glad I pointed you in the correct direction. Don't forget to accept the answer.

Legend.. thank you.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events