1

I got a situation that if I pass the controlling picklist name then I have to find the dependent picklist field name using APEX. I know the vice versa can be achieved though ie , Dependent ----> Controller using

 Schema.sObjectField controllerField = dependentField.getController();    

but I need to find using controller ---> Dependent

Eg , I am having Program__C as controller picklist and ProgramType__c as dependent picklist

through

Schema.sObjectField controllerField = ProgramType__c .getController();    

I can able to get Program__C but I need help for vice versa .

2

One field may control multiple fields, so you can't just call a method that returns the "single" field that's being controlled. You could do something like this:

Map<sObjectField, List<sObjectField>> dependencies = new Map<sObjectField, List<sObjectField>>();
for(sObjectField field: sObjectType.Account.fields.getMap().values()) {
  DescribeFieldResult res = field.getDescribe();
  if(res.isDependentPicklist()) {
    sObjectField[] deps = dependencies.get(res.getController());
    if(deps == null) {
      dependencies.put(res.getController(), deps = new sObjectField[0]);
    }
    deps.add(field);
  }
}

The resulting map will have each controlling field as the key, and a list of field tokens as the controlled fields.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.