Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Code to restrict change of custom field

Narsha Bachoo April 12, 2024

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

1 answer

1 accepted

3 votes
Answer accepted
Sean Chua _Adaptavist_
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.
April 24, 2024

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?
image.png

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:

image.png


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

Narsha Bachoo April 29, 2024

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.

 

Narsha Bachoo May 13, 2024

Hi @Sean Chua _Adaptavist_ ,

Can you help please?

Thanks.

Sean Chua _Adaptavist_
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 14, 2024

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.

Pasted Graphic.png

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

Narsha Bachoo May 15, 2024

Hello @Sean Chua _Adaptavist_ 

This works fine now when I edited the options ID. 

Thanks a lot! Appreciate it.

Regards,

Narsha 

Like Sean Chua _Adaptavist_ likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events