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.FieldBehaviours
import groovy.transform.BaseScript
import com.onresolve.jira.groovy.user.FormField
import java.util.Date;
@BaseScript FieldBehaviours fieldBehaviours
FormField dueField = getFieldById("duedate")
def currentDate = new Date()
def desc = getFieldByName("Ranking")
def duedateval=dueField.getFormValue() as Date
if (duedateval.before(currentDate)){
desc.setReadOnly(true)}
else{desc.setReadOnly(false)}
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.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def 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 to
duedateval != 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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm sorry, but you are incorrect.
When using Server-Side Behaviour, to ensure that the Behaviour will only trigger when the field the Server-Side Behaviour is configured for is updated, you must use fieldChanged.
Else there is a chance the Behaviour will not respond correctly.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ ,
No, I'm sorry, but you are incorrect.
Test1:
Attached: Due Date
def formField = getFieldById("duedate")
getFieldById("description").setFormValue(formField.getValue().toString())
Value is always printed exactly as I set it in Due Date.
Test2:
Attached: Due Date
def formField = getFieldById(getFieldChanged())
getFieldById("description").setFormValue(formField.getValue().toString())
Value is always printed exactly as I set it in Due Date. No difference.
Why?
Because the getFieldChanged() method returns a String, just the same if you access it in a static way via "fieldChanged".
The getter in this instance is getFieldById(), the fieldChanged is absolutely meaningless and simply represents a convenience String value based off the attached field's id.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You have misunderstood how the fieldChanged value works on the Server-Side Behaviour.
Obviously, this will work:
def formField = getFieldById("duedate")
getFieldById("description").setFormValue(formField.getValue().toString())
just the same as this:-
def formField = getFieldById(getFieldChanged())
getFieldById("description").setFormValue(formField.getValue().toString())
Because there is no condition to predetermine when the value should be set. For that matter, it is pointless to use Server-Side Behaviour for this. This can be done using the Initialiser itself.
In a Server-Side Behaviour, the Behaviour is expected to trigger only when a change is made to the specific field the Server-Side Behaviour is configured for.
In your example, there is no change or condition, it's just setting the value.
A proper example would be the scenario that Aditya Sastry is asking, i.e. when the Due Date field is greater than the Current Date. This is a condition.
The correct use case would be to use only a Server-Side Behaviour for the Due Date field:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def dueField = getFieldById(fieldChanged)
def currentDate = new Date()
def desc = getFieldByName("Ranking")
def duedateval=dueField.formValue as Date
if (duedateval.before(currentDate)){
desc.setReadOnly(true)
}
else{
desc.setReadOnly(false)
}
Also, although the Due Date is a system field, and it has the id "duedate", the if/else condition will only take the correct effect when the fieldChaged condition is used.
Using an Initialiser is not required in this case as the Server-Side Behaviour will already handle it.
@Aditya Sastry, please clarify when you want this Behaviour to take effect. Is it when the Issue is first created, when it is being Edited, or both?
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your previous comment, you mentioned:-
I want the script to trigger as soon as page loads since, it depends on the comparison between due date and current date.
From what you are saying, you want the Due Date to be verified the moment the create screen is loaded. My question when will you set the value for the due date field? By default the due date is empty.
You will need to first set the value before the behaviour can take effect.
Please clarify.
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you really remove my comment out of pettiness? Pretty sure I made a comment not even 6 hours ago.
Anyway pretty sure OP has all the information in the first 2 comments here, this entire measuring contest is meaningless, other than you trying to save face by writing ambiguously off-topic.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please put your answer in a in a different thread.
And I have no need to save my face. Frankly I do not want to waste my time debating this with you if you are not able to understand the actual requirement of this question.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Below is the fully working Server-Side Behaviour code for your reference:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def dueDate = getFieldById(fieldChanged).value as Date
def sampleField = getFieldByName('Sample Number')
sampleField.readOnly = false
if (dueDate) {
def dateValue = dueDate.time
def currentDate = new Date(System.currentTimeMillis()).time
if (dateValue < currentDate) {
sampleField.readOnly = true
}
}
Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.
Below is a screenshot of the Behaviour configuration:-
I am also including a couple of test screenshots for your reference:-
1. When the issue is being created, if the value set for the Due Date is before the current date, the Sample Number field will not be editable as shown in the screenshot below:-
2. If the Due Date field is later than the current date, the Sample Number field is editable as shown in the screenshot below:-
I hope this helps to solve your question. :-)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Ram and Sorry for the delay but I followed your approach and was able to achieve the ask,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.