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
rashid minhasrashid minhas 

can I change the opportunity picklist field from trigger which control another field in field dependency ? if yes than how

I created before insert trigger on opportunity which gets the account type and passes the account type to the custom field(picklist) in the opportunity object. this works fine but when I use field dependency and add this custom picklist as a controlling field and want to manage another picklist. then the trigger is not passing the account type to opportunity can anyone tell me why? and how I will do it.
Thanks In advance
Shri RajShri Raj
To resolve this issue, you need to modify your trigger to include the logic for updating the dependent field as well. You can use the Apex Describe API to determine the field dependency relationship and update the dependent field as needed. Here's an example:
 
trigger OpportunityTrigger on Opportunity (before insert) {
  // Get the account type
  // ...

  // Get the field dependency relationship
  Map<String, Schema.PicklistEntry> controllingFieldValues = 
    Opportunity.Custom_Field__c.getDescribe().getPicklistValues();
  List<String> dependentFieldValues = new List<String>();
  for (Schema.PicklistEntry entry : controllingFieldValues.values()) {
    if (entry.getValue() == accountType) {
      dependentFieldValues = entry.getDependentValues();
      break;
    }
  }

  // Update the dependent field
  for (Opportunity opp : Trigger.new) {
    opp.Custom_Field__c = accountType;
    if (dependentFieldValues.size() > 0) {
      opp.Dependent_Field__c = dependentFieldValues[0];
    }
  }
}