issue with Script runner behaviours plugin

Issa May 27, 2024

Hi Community,

Unfortunately I'm a beginner in scripting.

We have a requirement when "revised date" field is changed the user has be to update "Reason for revised ETA"

I'm using Script runner's Behaviours to achieve.

I have added the field " revised date" in behahour and added server side script as follows:

def fldMyField = getFieldByName("Reason for Revised ETA")
fldMyField.setFormValue(null)
fldMyField.setRequired(true);
The script works fine but it's being executed even if we click on Edit screen where revised date is not changed or empty.
Help would be appreciated!
Thanks,

 

1 answer

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.
May 27, 2024

I assume you just added the code as "initializer", not actually added it as a script to the added field?

I.e. initializer loads on screen load, only once; but the code added onto a field loads "on change", more or less

Issa May 27, 2024

Thanks for the reply Radek,

 

But I have not added as initialiser. It is the server side script on the field. See below screenshots

snap1.png

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.
May 27, 2024

Oh right, also reproduced.. my bad, totally forgot they changed that at some point.

Found this that explains it: https://assist.adaptavist.com/browse/SRJIRA-3418?focusedId=478183&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-478183

 

The workaround there is worth a try. Works locally at least using

import com.atlassian.jira.component.ComponentAccessor

def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjects().findByName("FST Analysis")

if (underlyingIssue != null && getFieldById(getFieldChanged()).getValue() != underlyingIssue.getCustomFieldValue(customField)) {
    //Run some logic when the current fields value is changed to something other than the current saved value
    getFieldById("description").setFormValue("HAHAHA")
    getFieldById("description").setRequired(true)

}

 

On create, underlyingIssue is null, so it can be used as a check whether the issue exists or not. Though it's kind of dirty Adaptavist doesn't appear to think of this as a bug, which I disagree with, until there is a checkbox to ensure it only runs on field changed events.

Issa May 27, 2024

Hi Radek,

 

getFieldById("description").setFormValue("HAHAHA")

 How to set the formvalue to user input? "HAHAHA" is hardcoded value

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.
May 27, 2024

What do you mean by user input? That's just a test value I used so I can see it when the behaviour works out the condition.

 

I would guess you should reuse what you had

def fldMyField = getFieldByName("Reason for Revised ETA")
fldMyField.setFormValue(null)
fldMyField.setRequired(true);

 

Issa May 28, 2024

Hi Radek,

 

That doesn't work as well.

After some googling and little modifications  the below code works fine for me.

Thank you for your time Radek. Really approceiate it.

 

import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import org.apache.log4j.Logger
import org.apache.log4j.Level
//grab reason box
def reasonBox = getFieldByName("Customer Environment");
//grab due date in edit view
def revisedDate = getFieldById(getFieldChanged());
//grab current due date from database
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField currentrevisedDate = customFieldManager.getCustomFieldObject('customfield_12237');
String currentDueDateAsString = (underlyingIssue?.getCustomFieldValue(currentrevisedDate) as Date)?.dateString;
//see date was changed in edit view (note null safe operator [?] which allows dueDate to be null without throwing an exception)
if((revisedDate?.value as Date)?.dateString != currentDueDateAsString) {
 //clear reason box
 reasonBox.setFormValue(null);
 //require reason box so that user is forced to enter text
 reasonBox.setRequired(true);  
}
else {
    reasonBox.setRequired(false);
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.12.1
TAGS
AUG Leaders

Atlassian Community Events