I have a object that has more than 40 fields. There are 3 record types and each layout shows different fields.
Now, I want to write a validation rule which allows editing of limited fields only when the stage of record is progress.
For e.g.
If my School object have following fields:
School__c
Location__c
Name
Color__c
Class_c
Students__c
Teacher__c
Faculties__c
Department__c
stage__c
Now, If i have 3 record types called Type A, Type B, Type C.
In Type A, I want user be able to edit Color__c and Class__c fields.
In Type B, I want user to be able to edit Students__c and Teacher__c fields
In Type C, I want user to Edit Faculties__c, Department__c fields.
In all record type Name should be editable.
User should not be able to edit School__c, Location__c fields. The whole validation should run when stage__c = progress.
Can i write a validation rule that satisfies above condition.
Best Answer
You likely could write a validation rule to satisfy your conditions, but you might not be able to save that validation rule (because it'd likely go beyond the compiled character limit).
Validation rules aren't really meant to do what you're looking to do. Preventing certain fields from being edited is easy. The inverse of that, allowing certain fields to be edited, is hard.
A better declarative solution is to create new record types and page layouts, and mark the fields that you want to prevent from being edited as "read only" on those new layouts. You could use a workflow rule or process builder to automatically move your records into/out-of these special recordTypes.
Other than that, this would be a relatively simple trigger.
Map<String, Set<String>>
since you want different fields on different recordTypes)trigger.new
, and check the record against its companion intrigger.old
some example code to get you going
The big caveat here (besides me not testing for a particular stage) is that I assume you have a
recordTypeName__c
field (a formula field) to pull the name of your recordType into the object you're working on.Trigger context variables don't contain any "related data" (data on objects other than the one you're working on). If you need to use more than one period/dot/full-stop to access a field, like
myRec.RecordType.Name
, that information is not available in trigger context variables.To get that information, you either need to explicitly query for it, or have a formula field. There are other ways around that, but those are the easiest and most common approaches.