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
System Admin 949System Admin 949 

Make a Picklist Field Read Only based on another Object Picklist Value

Hi All,
I have two objects one Customer and Quote.Customer is the Parent and Quote is Child.I have a picklist field in Customer i.e. Country it has some values like Ind,US,UK,Europe....etc.In Quote object also have picklist field called SalesType,it has some values.
My Scenario is when iam selecting the Country as 'Ind' in Customer object at that time the picklist field in Quote object as SalesType should be visible.if i select any picklist value in Customer except 'Ind' it should be read only .
how to achieve this??can any one suggest or provide some examples on this.
t​​​​​​​hanks in advance
 
Raj VakatiRaj Vakati
You cannt make it with configration .. you need to write the code .. 

But you can controll the field editability with validation rules .. 

 
NitishNitish
Hi System Admin,
You can do this using apex FieldPermission Setup Object and PermissionSet as Its only work on Permission set not on profile. Refer the below code.
Trigger Code:-
trigger QoutePickListReadOnly on Customer__c(After Update) {
	
    If(Trigger.isAfter && Trigger.isUpdate){
      	String Ind;
        for(Customer__c ac:Trigger.new){
           
            
            if( Trigger.newMap.get(ac.Id).Country=='Ind'){
                ContactPicklistReadonlyFuture.updateFieldPermission();
            }
        }
        
    }
    
    
}
Future Class
public class ContactPicklistReadonlyFuture {
	@future
    public static void updateFieldPermission(){
        List<FieldPermissions> fp=[SELECT Field,Id,ParentId,PermissionsEdit,PermissionsRead,SobjectType FROM FieldPermissions WHERE SobjectType = 'Qoute' and Id='FieldID' and parentId='PermissionSetID'];
		List<FieldPermissions> fpNew=new List<FieldPermissions>();
        for(FieldPermissions fp1:fp){
            
    			fp1.PermissionsEdit=false;
                fpNew.add(fp1);
            
		}
        update fpNew;
    }
}

Thanks,
Nitish​
 
System Admin 949System Admin 949
Hi Nitish, thanks for your reply,can you explain it clearly.in Quote object fields where you mentioned like SalesType__c
thanks in advance
NitishNitish
You need to pass the Salestype__c Salesforce ID in the place where i have mentioned fieldID in SOQL query.
The Field ID you can get from Wrokbench.  Run the below Query in Workbench in FieldPermissions Object.
 
SELECT Field,Id,ParentId,PermissionsEdit,PermissionsRead,SobjectType FROM FieldPermissions WHERE sObjectType='Quote' and parentID='PermissionSetID'
PermissionSetID is the permission set through which you have given edit acces to that field to the user.
 
System Admin 949System Admin 949
For this have to create any permission Set??i don't create any permission set .
thanks
NitishNitish
I think this is the only way to change field permission using Apex but not sure. Through FieldPermissions you can only use permission set not profile.
Other than that you can controll the field using Validation Rule.