Hello Team,
I am trying to write a code on scriptrunner to restrict the change of a custom field. The user must not be able to change the value lesser than the actual value. For e.g., if the field's value is 20%, the user cannot change it to 10%. He should be able to change it to values which is greater than 20%. The custom field is a Select List (single choice) type and the name of the custom field is 'Percentage Completion'.
Can you help please?
Thanks,
Narsha
Hey @Narsha Bachoo ,
This looks like something which could be achieved using ScriptRunner's behaviours. The script will need some editing to match your instance needs but do give it a play around.
The setup used (On load , Issue View) because you are editing the issue right?
const percentageCompletionField = getFieldById('YOUR_CF_ID');
const currentPercentageValue = percentageCompletionField.getValue().value.replace('%', '');
//I have % in my context values so this is just removing it for comparison
const currentPercentage = parseInt(currentPercentageValue);
//This portion is about your option ID and converted values..examples..
const optionVisibilities = {
'10101': 10, // this is for 10%
'10102': 20, // this is for 20%
'10103': 30, // this is for 30%
//and so forth...it would absolutely be possible for you to just use numbers instead of how I did mine
};
//This hides the visibility of the options available on load based on the original select list value (eg: 30%, then wont be able to see 10% and 20%)
const optionsToHide = Object.keys(optionVisibilities).filter(optionId => optionVisibilities[optionId] < currentPercentage);
percentageCompletionField.setOptionsVisibility(optionsToHide, false);
This is what my Select List CF looks like when the issue already have 30% selected and I am trying to change it:
One thing to be aware, this applies for everyone. So, if you want to restrict it to some user groups or users etc, then check out this sample Make field read-only based on user groups and adapt it to your needs.
Hope that helps!
Sean
Hello Sean,
Thanks for the code. I tried it and changed it using my custom field ID, but it is not working.
I am not sure what is wrong as I am not getting any error.
This is the message I found in the logs,
Timestamp: 2024-04-29T18:56:59.913Z CorrelationId: 1f7b4bf4-4f9a-47e2-9a7f-9a52c067be2c Field UUID: fe2fafc3-535b-49aa-b064-8fd18c3f846e Field Name: customfield_13867 Event: On Load Message: Added new tracking event: {"event":"Behaviours API call","apiMethod":"setOptionsVisibility","targetField":"customfield_13867","count":1,"fieldId":"fe2fafc3-535b-49aa-b064-8fd18c3f846e","affectedField":"customfield_13867"} Level: info View: Issue view
Timestamp: 2024-04-29T18:56:59.912Z CorrelationId: 1f7b4bf4-4f9a-47e2-9a7f-9a52c067be2c Field UUID: fe2fafc3-535b-49aa-b064-8fd18c3f846e Field Name: customfield_13867 Event: On Load Message: Added new tracking event: {"event":"Behaviours API call","apiMethod":"getValue","targetField":"customfield_13867","count":1,"fieldId":"fe2fafc3-535b-49aa-b064-8fd18c3f846e","affectedField":"customfield_13867"} Level: info View: Issue view
Timestamp: 2024-04-29T18:56:59.911Z CorrelationId: 1f7b4bf4-4f9a-47e2-9a7f-9a52c067be2c Field UUID: fe2fafc3-535b-49aa-b064-8fd18c3f846e Field Name: customfield_13867 Event: On Load Message: Added new tracking event: {"event":"Behaviours API call","apiMethod":"getFieldById","targetField":"customfield_13867","count":1,"fieldId":"fe2fafc3-535b-49aa-b064-8fd18c3f846e","affectedField":"customfield_13867"} Level: info View: Issue view
Thanking you in advance.
Regards.
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.
Hi @Narsha Bachoo , apologies for the late late late response!
Just for clarity, have you also made changes to match your instance at this part:
//This portion is about your option ID and converted values..examples..
const optionVisibilities = {
'10101': 10, // this is for 10%
'10102': 20, // this is for 20%
'10103': 30, // this is for 30%
//and so forth...it would absolutely be possible for you to just use numbers instead of how I did mine
For this part, you will need to input the Custom Field's Context ID for the 10/20/30/etc.
To get the context ID, go to the Custom Field's ... > Context and default value > Edit Options > Edit.
Hope this helps, if you still can't get it to work - I would recommend you to open a Support Ticket with us for one of our Support Engineers to investigate and work with you.
Regards,
Sean
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello @Sean Chua _Adaptavist_
This works fine now when I edited the options ID.
Thanks a lot! Appreciate it.
Regards,
Narsha
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.