Need help with Script Listeners to copy and lock a field value

Aisha M October 9, 2019

Hi,

Can anyone please help me with how to use Script Listeners for the below scenario,

Field 1) Epic Due Date

Field 2) Baseline Due Date (Date picker)

I want to make sure that Field 1 copies the value of Field 2 the FIRST time its filled while the issue is created. Later , even thought the Field 2 value might be changed, the field 1 must not change its value and must be non editable (only viewable).

 

Thank you !

2 answers

1 accepted

1 vote
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 15, 2019

Hi @Aisha M ,

I would advise to create a Script Listener on the Create Issue event in appropriate projects. Make sure that this event is triggered in the workflows.image.pngUse this script (update the ids) : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def customFieldManager = ComponentAccessor.getCustomFieldManager()

int date1id = 12500
int date2id = 12501

def date1 = customFieldManager.getCustomFieldObject(date1id)
def date2 = customFieldManager.getCustomFieldObject(date2id)

def date1Value = issue.getCustomFieldValue(date1)
def date2Value = issue.getCustomFieldValue(date2)

date1.updateValue(null, issue, new ModifiedValue(date1Value, date2Value), new DefaultIssueChangeHolder())

Make sure field 1 is only on the View Issue screen so users cannot edit it.

Antoine

Aisha M October 15, 2019

@Antoine Berry  Thanks a million Antoine. Will test this out and let you know.

Aisha M October 18, 2019

@Antoine Berry Hi Antoine, I couldn't find a script listener option either at validators/post functions of the Create Issue transition . . Do i have to add he listener from the Add-on page or something ?

Thank you

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 18, 2019

Hi, just type GG > script listeners

image.png

You will end up here : 

image.png

Antoine

Aisha M October 22, 2019

@Antoine Berry  That worked !!!! Thank you sooooooo much !!! :) 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 22, 2019

You're welcome, always glad to help !

Aisha M October 23, 2019

@Antoine Berry  Hi Antoine. Just a quick help, how do I make sure, this is available for the issues which was existing before the listener was created ? 

I tried adding "Issue updated' in the events, but it gives an error. But the script works wonderful for newly created issues. 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 23, 2019

Hi @Aisha M , I do not see any reason why this should not work on Issue Updated event. This event triggers when you edit the issue. You might want to create custom events for your needs.

Also you should investigate the logs to check why it is failing.

Antoine

Aisha M October 25, 2019

@Antoine Berry  Thank for the help :) But unfortunately for me, they have changed the requirement to a completely different one and it sounds much more complicated :(

Now its like the uneditable field must capture the dates on when the BASELINE DUE DATE was entered and later whenever it gets modified . Planning to post about it and get help from the members

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 25, 2019

Would you mind opening a new ticket then ? It would be easier to track which post answers which question.

Aisha M October 25, 2019

@Antoine Berry  Yes, I have :) If you are interested to have a look at the new question , please use the link below

https://community.atlassian.com/t5/Jira-questions/Need-help-with-listeners-on-a-date-field-which-tracks-changes-on/qaq-p/1212095#M387129

Aisha M July 3, 2020

Hi @Antoine Berry , LOL, this is the exact same requirement I have again. Only difference is,

Field 1) Baseline Due Date (Date picker)

Field 2) Due Date (At the Epic issue Type)

I want to make sure that Field 1 copies the value of Field 2 the FIRST time its filled (When ever a user fills in the Due Date). Later , even thought the Field 2 value might be changed, the field 1 must not change its value and must be non editable (only viewable).

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2020

Hi @Aisha M ,

The logic behind is exactly the same. Create a listener and link it to each event where users might update the due date (field 2). Is the field is mandatory on creation, only use the createIssue event.

Use this script : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issueType = issue.getIssueType().getName()
if (issueType == "Epic"){
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def dueDateValue = issue.getDueDate()
int date1id = 12501

def date1 = customFieldManager.getCustomFieldObject(date1id)
def date1Value = issue.getCustomFieldValue(date1)

if (!date1Value && dueDateValue){
date1.updateValue(null, issue, new ModifiedValue(date1Value, dueDateValue), new DefaultIssueChangeHolder())
}
}
Aisha M July 7, 2020

Hi @Antoine Berry , I will test this out :) Thank you a million times for always taking the effort to help ;( 

But, what if an user doesn't add a Due Date while creating an Epic, and do it at a later point of time ? 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2020

That is why you need to link this script to any event that might be triggered when the user changes/adds a value.

You need to check the workflow transitions post functions. By default on creation the event "issue created" is triggered. Then in others transitions it is most likely a "Generic event", or "Issue Resolved" on resolution, and so on. You can replace them by existing or custom events if needed.

So, on issue creation, it will most likely trigger "issue created", on edition "Issue updated",  and "generic event" if the user adds a value using a transition screen.

Aisha M July 7, 2020

@Antoine Berry Okay got it. Aldo, this listener should be created at the workflow or throught the Add On page ?

We now have a separate workflow for the EPIC issue type. I have defined the status flow below,

Create > BackLog >Pre Planning > Ready for Planning > Planning > Ready & Planned > Working >Validated>Delivered , also a Cancelled status

So, all these status are global transitions. (So, an Epic can be moved to any status at any point)

They also have another point here, that if an Epic does back to the status Ready for Planning from Working, they want the Baseline Due Date it should clear and start fresh

Is it possible incorporate this ?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 7, 2020

For the listener, just type G+G > Script listeners. Youh should only check transitions with an associated screen which contains the due date (otherwise the user cannot change it). Then, check which event is fired : 

image.pngOnce you have collected all the events which might cause an update of the due date field, create the script listener and link it to these events.

Finally, add a scriptrunner post function to clear the due date on the Ready for planning transition. This script should work : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def date1 = customFieldManager.getCustomFieldObject(date1id)
def date1Value = issue.getCustomFieldValue(date1)

date1.updateValue(null, issue, new ModifiedValue(date1Value, null), new DefaultIssueChangeHolder())
Aisha M July 7, 2020

@Antoine Berry , Okay, I think I got the steps.
1) Create a script listener with the script for every event a user might update Due Date - Issue created, Issue updated etc

2) Then  go to that particular workflow step and add the above post function to clear Baseline Due Date.

(But I want this to happen only if an Epic is moving back from Working to Ready for Planning)

Will test this out first and see if I'm able to make it work. 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 8, 2020

This is correct ! 

You could add this snippet to check if due date has been changed : 

def dueDateChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "duedate"}
if (dueDateChanged) { ...

at the top of the script but unfortunately this does not work at creation, so you would need two listeners.

You could achieve the due date clear on only one transition by having two transitions and hiding one or the other using a condition in the post function.

Aisha M July 10, 2020

@Antoine Berry  Hi, I am able to complete the first part of the requirement. I created the listener for "Issue Created" & "Issue Updated" events, and the script is working great. Its copying and locking the first value of the Epic Due Date. *happy dance* Thank you million million times !

Now, I need to figure how to implement the second part that is clear the script when the issue moves back to an older status, , lol, thats what's confusing me so much :) I ll test that let you know :)

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 13, 2020

Hi @Aisha M ,

So I think you got most of the requirement right. You could make the last part working with this method. Have two transitions going the "Ready for planning" status : 

  • The global "From all statuses" transition. Add this custom groovy condition
    passesCondition = (issue.getStatus().getName() != "Working")
  • A new one going from "Ready for planning" to "Working". Add the custom groovy post function that i suggested above to clear the field (I think I made a mistake previously, I just corrected it).

Let me know if that helped.

Aisha M July 14, 2020

Hi @Antoine Berry , I don't understand the second part. You have given the below three codes, which one should I use, I m so confused :(

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def date1 = customFieldManager.getCustomFieldObject(date1id)
def date1Value = issue.getCustomFieldValue(date1)

date1.updateValue(null, issue, new ModifiedValue(date1Value, null), new DefaultIssueChangeHolder())

def
dueDateChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "duedate"}
if (dueDateChanged) { ...

 

passesCondition = (issue.getStatus().getName() != "Working")
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 15, 2020

Hi @Aisha M , I understand that can be confusing. You are already using this script successfully if I understood correctly : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def issueType = issue.getIssueType().getName()
if (issueType == "Epic"){
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def dueDateValue = issue.getDueDate()
int date1id = 12501

def date1 = customFieldManager.getCustomFieldObject(date1id)
def date1Value = issue.getCustomFieldValue(date1)

if (!date1Value && dueDateValue){
date1.updateValue(null, issue, new ModifiedValue(date1Value, dueDateValue), new DefaultIssueChangeHolder())
}
}

It copies the due date value into the baseline due date if the baseline due date is empty.

This part is just a snippet you can add to check if the due date has been changed : 

def dueDateChanged = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "duedate"}
if (dueDateChanged) { ...

but it does not work for the Issue Created event, so you might ignore it.

This snippet is used in the global transition to the "Ready for planning" status as a custom groovy condition.

passesCondition = (issue.getStatus().getName() != "Working")

it ensures that when the issue is on the "Working" status, the user only sees the additional transition from "Working" to "Ready for planning" (I switched the two statuses in the previous comment by mistake). In this specific transition use this script : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

def date1 = customFieldManager.getCustomFieldObject(date1id)
def date1Value = issue.getCustomFieldValue(date1)

date1.updateValue(null, issue, new ModifiedValue(date1Value, null), new DefaultIssueChangeHolder())

It will clear the baseline due date.

I guess this should fulfill the requirement. Let me know if that is not clear.

Aisha M July 15, 2020

Hi @Antoine Berry , Thank you for patiently explaining me the steps :)

So, I need to create a new transition from "Working" to "Ready for planning", and then add the above final script right ? If yes, should this be a post function script runner script ?

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 15, 2020

Yes ! 

So in total you would need one listener, one groovy condition and one groovy post function. :)

Aisha M July 15, 2020

@Antoine Berry Thank you million times :) Okay, I will test this out and update you asap :)

Aisha M July 15, 2020

Hi @Antoine Berry , I was able to set up the second part of the requirement using Project Automation too. I'm scared it looks way too simple :D 

Anyway, I ll try out the script condition mentioned above as well. I ll see what the users prefer. But thank you sooo much for taking the time :):):)

Aisha M July 28, 2020

@Antoine Berry  Hellooo, can you please post the above answer to the below link , so the answer can be accepted :)

https://community.atlassian.com/t5/Jira-questions/How-create-a-Scripted-field-to-copy-and-lock-the-first-value/qaq-p/1422963

Thank you soo sooo much for your constant help ^_^ 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 28, 2020

Hi @Aisha M ,

You might want to create a new question with the full requirement otherwise it would be a bit odd. Meaning the part where you need to clear the value when transitionning to "Ready for planning". Also I feel a bit uncomfortable answering after the all mighty Nick lol :)

Aisha M July 28, 2020

@Antoine Berry I will modify the question to the exact outcome. I just don't want to miss marking your answer ! :) Sorry about that

Aisha M July 28, 2020

@Antoine Berry Do you want to create a separate question without Nick's comment ?? I personally feel you are more smarter and infact much more helpful than anyone on this community !!!!

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 28, 2020

Yeah you could also edit your question to include the whole requirement. It is just that I felt a bit rude to add an answer that would fit more than the original question would ask for when Nick has given a valid answer already. :)

(Also I cannot rival with Nick, he provided more than 60000 answers ! He's our guru :D)

Aisha M July 28, 2020

@Antoine Berry Lol, okay :) I ll create a new detailed question for it & share you the link. I just updated the details for the previous question. 

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 3, 2020

Hi @Aisha M ,

So new question or old one with updated requirement ? :)

Aisha M August 17, 2020

@Antoine Berry  Hello !! I m sooo sorry for being gone :D I had to take off due to personal reasons :) so so sorry.  Anyway back to work :(

I have created a new post for the above solution :) Can you please post the answer to the below link ? I ll modify the question more if required :)

https://community.atlassian.com/t5/Jira-Software-questions/Need-listener-to-copy-and-lock-the-first-value-from-a-different/qaq-p/1458207#M93367

Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 17, 2020

Hi @Aisha M ,

Do not be sorry, I hope you are doing ok and everything is fine on your side now.

Good luck with work :) I have answered your question, thanks for having taken the time to create one.

Antoine

Aisha M August 18, 2020

@Antoine Berry Absolutely nott ! Thanks to YOU for taking the time & effort to answer my frequently changing questions :):) I m sure no one would have helped as much ! 

Like Antoine Berry likes this
0 votes
Mihai Schwarz
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.
October 9, 2019

Suggest an answer

Log in or Sign up to answer