Hi,
I have a requirement to make a number field "Ranking" read only if the due date has passed. I am trying to achieve it through behaviors but unable to do so. Below is the code snippet for behavior. Any pointers please?
import com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScriptimport com.onresolve.jira.groovy.user.FormFieldimport java.util.Date;@BaseScript FieldBehaviours fieldBehavioursFormField dueField = getFieldById("duedate")def currentDate = new Date()def desc = getFieldByName("Ranking")def duedateval=dueField.getFormValue() as Dateif (duedateval.before(currentDate)){ desc.setReadOnly(true)}else{desc.setReadOnly(false)}
Hi @Aditya Sastry
From the code that you have shared, I am going to assume you are using a Server-Side Behavior for the Due Date field.
If so, the approach you are using to declare the Due Date field is incorrect.
You should declare it as:-
import com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScript@BaseScript FieldBehaviours fieldBehavioursdef dueField = getFieldById(fieldChanged)
When using a Server-Side Behaviour, you need to make sure the fieldChanged option is used for the field the Server-Side Behaviour is configured for. This is to ensure the Behaviour will trigger when that field changes.
I hope this helps to solve your question. :-)Thank you and Kind regards,
Ram
No, fieldChanged has nothing to do with it, it's just a "generic" getter instead of typing out the field name. The issue is the script is likely not attached to the Due Date so the behaviour does not run when the value is being changed.
Another tip:
duedateval.before(currentDate)// change toduedateval != null && duedateval.before(currentDate)
Because your duedateval may be null, this could throw a nullpointer exception. So let's just ensure the field has any value before we do date comparison.
@Ram Kumar Aravindakshan _Adaptavist_ I want the script to trigger as soon as page loads since, it depends on the comparison between due date and current date.
@Radek Dostál I want this behaviour to run on intializer as well since this needs to be triggered based on the comparison between due date and current date. If the due date has crossed the current date then the field needs to be read only.
It looks like you're new here. Sign in or register to get started.