function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raj R.Raj R. 

How to gray out a custom Opportunity Field through Apex Trigger or validation rule?

Hi,

The current scenario is that a custom Opportunity Field (Cfield__c) needs to be required when the Opportunity.Amount >= $200,000 and Stage is A,B,C, and E. Through the UI, a red bar needs to appear next to the field to indicate it is required when it meets the criteria above, but needs to be "grayed out" (not visible) if the Amount < $200,000.

How may i use an apex trigger or validation rule to do this? If this cannot be done with Apex Trigger or validation rule, what other method may I use? Ideally I would like to stay away from overriding the Opportunity Edit page with a VF page. 

I thought i could do this with Salesforce Flow, but I am new to that and it seems cumbersome to get that to work.

Requirements:
  • Opportunity.Amount >= $200,000
  • Stage = A,B,C, and E.
  • Cfield__c is required (dropdown with Yes or No as values)
  • Red bar appears next to Cfield__c to indicate it is required.
Best Answer chosen by Raj R.
Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi rRup,

You could achieve this using recordtypes, pagelayouts and workflow rules.

For your reference : 
Creating record type and page layout : https://www.youtube.com/watch?v=JUFq1do07fA
Changing Record Type using Workflow Rules : https://help.salesforce.com/apex/HTViewSolution?id=000205890&language=en_US

 

All Answers

Thiyagarajan Selvaraj (SFDC Developer)Thiyagarajan Selvaraj (SFDC Developer)
Hi rRup,

You could achieve this using recordtypes, pagelayouts and workflow rules.

For your reference : 
Creating record type and page layout : https://www.youtube.com/watch?v=JUFq1do07fA
Changing Record Type using Workflow Rules : https://help.salesforce.com/apex/HTViewSolution?id=000205890&language=en_US

 
This was selected as the best answer
KaranrajKaranraj
Hi rRup - You could achieve using validation rule but the conditions will be checked when the user hit the save button. Create two validation rule on the Opportunity Object as mentioned below

Validation rule 1 : To make Cfield__c required
AND(Opportunity.Amount >= 200000,OR(TEXT(Stage)== 'A',TEXT(Stage)=='B',TEXT(Stage)=='C',TEXT(Stage)=='E'),Cfield__c == NULL)
Validation rule 2: To make Cfield__c non editable field when Amount is < 200000
AND(Opportunity.Amount < 200000, NOT(Cfield__c == NULL))

Thanks,
Karan
 
Raj R.Raj R.
Thank you Karanraj and Thiyagarajan.

I am going to try both and I shall update the question as soon as I can get some analysis.