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,369,454
Community Members
 
Community Events
168
Community Groups

Scriptrunner doesnt work properly in create transition to clear fields

I have the same postfunction that I attach to every transition in a workflow that checks what status the transition is going into and manipulates a couple of Date Picker custom fields depending on which status.  It works great for all of the transitions EXCEPT for the create one (example it should work after a clone to clear the fields).  Essentially we have a status for when a design change is complete and it's ready to get verified (RdyToVerify) and one when the verification of that design change is complete (Verified) and upon entry into those statuses we set a custom field recording when that occurred (RTL Resolved Date , DV Resolved Date resp.). 

If someone clones a bug that has the values set it will activate the code on the create transition (I see the log messages telling me like "now going into New status" so I know it's running) but it refuses to set or clear the custom field values in that transition.  The same if I set the values in the create screen and hit create, it wont change the custom field values. (my logic is that in those states of "New" or "In Progress" those should be cleared so my code attempts to clear them).  The setting to null works great when i transition OUT of New and INTO In Progress but it just refuses to work on the New transition INTO the New state as part of create/clone.  I've also tried moving it to every position in the list of postfunctions.  The code is exactly the same on all transitions (all point to same file).

 

Code is below : 

 

 

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.fields.CustomField

import java.sql.Timestamp

 

// enable logging so we can see 'info' type and not just 'error'

import org.apache.log4j.Logger

import org.apache.log4j.Level

log.setLevel(Level.INFO)

 

 

 

 

 

def customFieldManager = ComponentAccessor.getCustomFieldManager()

 

def the_status = issue.getStatusObject().getName()

log.info("The status is "+the_status)

 

 

// RTL

def RTL_Date_Object  = customFieldManager.getCustomFieldObjectByName("RTL Resolved Date")

def RTL_Date_Value   = issue.getCustomFieldValue(RTL_Date_Object)

 

//DV

def DV_Date_Object   = customFieldManager.getCustomFieldObjectByName("DV Resolved Date")

def DV_Date_Value    = issue.getCustomFieldValue(DV_Date_Object)

 

// get today's date

def today = new java.sql.Timestamp(new Date().getTime())

 

// clear it if you're going to an early status, up to In Progress

if (the_status == "New" || the_status == "In Progress") {

    log.info("Going into either New or In Progress : "+the_status)

    if (RTL_Date_Value) {

       log.info("Clearing RTL_Date_Value")

       issue.setCustomFieldValue(RTL_Date_Object, null)

    }

    if (DV_Date_Value) {

         log.info("Clearing DV_Date_Value")

        issue.setCustomFieldValue(DV_Date_Object, null)

    }

    log.info("done with any clearing of RTL or DV date values")

}

 

// entering RdyToVerify, set the RTL resolved and clear the DV resolved

if (the_status == "RdyToVerify") {

    if (! RTL_Date_Value) {

       issue.setCustomFieldValue(RTL_Date_Object, today)

    }

    if (DV_Date_Value) {

        issue.setCustomFieldValue(DV_Date_Object, null)

    }

}

 

// set both if you're in a late status, Verified or after

if (the_status == "Verified" || the_status == "Closed") {

    if (! RTL_Date_Value) {

       issue.setCustomFieldValue(RTL_Date_Object, today)

    }

    if (! DV_Date_Value) {

        issue.setCustomFieldValue(DV_Date_Object, today)

    }

}

 

1 answer

Hi @Matthew Page

For your requirement, i.e. to reset the field when you Create or Clone an issue, it would be better to use the ScriptRunner Behaviour instead.

You could try something like this:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

// RTL
def RTL_Date_Object = getFieldByName("RTL Resolved Date")

//DV
def DV_Date_Object = getFieldByName("DV Resolved Date")


if(actionName == "Create") {
RTL_Date_Object.setFormValue(null)
DV_Date_Object.setFormValue(null)
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question. :)

 

Thank you and Kind Regards,
Ram 

But that would only cover the case where they set it during create form but I don’t think applies to cloning an issue

gentle bump here, I guess I'm asking if my assertion is true that the behavior wouldnt work for cloning?

Hi @Matthew Page

Sorry, I missed that part out. 

For Cloning Issues, you will need to use a Listener. The event type you will need to use is the IssueLinkCreatedEvent

Below is a print screen of the Listener configuration:-

listener_configuration.png

And in the listener, you will need to specify which fields you want to reset.

Below is an example working code for your reference:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def issue = event.issueLink.sourceObject as MutableIssue
def issueLinkType = event.issueLink.issueLinkType

def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def customFieldManager = ComponentAccessor.customFieldManager
def issueManager = ComponentAccessor.issueManager

def list1 = customFieldManager.getCustomFieldObjectsByName("List 1")[0]

if(issueLinkType.name == "Cloners") {

if(issue.getCustomFieldValue(list1) != null) {
issue.setCustomFieldValue(list1, null)
issueManager.updateIssue(loggedInUser,issue, EventDispatchOption.DO_NOT_DISPATCH,false)
}
}

Please note, this example code is not 100% exact to your environment. Hence, you will need to modify it accordingly.

If you have multiple fields that you want to reset, you will need to modify the if condition accordingly.

To recap, to reset the field when you are creating a new issue, you will need to use the Behaviour as per the example provided in the previous answer.

For the Cloned issues, you will need to use the Listener with the IssueLinkCreatedEvent as per the sample code provided above.

Below are some print screens for the Issue Cloning test.

1) Create the Original Issue, the list is reset:-

create_original_issue.png

 

2) Once the Issue is created, and the List is selected:-

original_issue_created.png

3) Cloning the original Issue

cloning_original.png

 4) Issue is cloned, and the List is reset. Hence it doesn't display because no value is selected from it:-

clone_created.png

5) When the cloned issue is edited, the List is none since no value was selected:-

cloned_issue_edit_screen.png

I hope this helps to answer your question. :)

 

Thank you and Kind regards,

Ram

Ok, I can take a look if a listener can maybe do it but are you confirming that the groovy code when run in the create transition cant do this action for some reason even though every other transition can?  My preference isnt to have some of the code on the transition and some in a listener.

thanks!

Matt

Hi @Matthew Page,

To answer your question, yes. When using the create transition, i.e. Post-Function, it will not work as expected. 

For some cases, once the issue is created, the create transition will not work. However, if you transition the issue, for example, to In Progress and back to To Do, the transition will trigger.

The alternative to the Post-Function is the Listener, which will trigger immediately once the Issue Link is created using the IssueLinkCreatedEvent.

I hope this helps to answer your question. :)

Thank you and Kind Regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events