In this blog, I would like to share with you the same reusable custom dependent pick-list component which was explained in my previous part1 blog. Current component is fetching pick-list values from org using
uiObjectInfoApi which was released in winter 21 instead of custom apex class.
Object-info API is flexible enough to provide complete single or multiple information as well as pick-list values based on the record type by consuming dynamic values through wire method. In our current scenario, we have consumed getObjectInfo, getPicklistValues api’s to get the necessary data to bind pick-list values to the component.
The below component code explains everything as it is simple and consumed only the above custom apis to bind the data. ‘dependentValue’ and ‘parentValue’ are the output parameters for providing the dependent and controlling picklist field selected values. Also, the selections will be notified with separate custom events for both the fields, named ‘controlchange’ and ‘dependentchange’.
- getObjectInfo api fetches the record type and dependent field information based on the public attributes through a wire method objectInfo
- if the recordtypeid is not given, then assigns it from the objects default recordtypeId
- Next, verify the dependent pick-list fields passed through controlFieldName, dependentFieldName are valid for the given object.
- After successful validation, parentField and dependentField populates necessary field information to pull the pick-list values from salesforce org using getPicklistValues api.
- parentField and dependentField attributes acts as a controller for other two wire methods as we don’t have any option to call the ui apis in an imperative way. If the picklist fields are not valid, then both the attributes have only an empty string which restricts from pulling the data from server.
- fetchControlOption method sets the options for the controlling combobox field. Also, considering the default value selection as well.
- fetchDependentOptions method sets the options, not in a straight forward way as it contains validFor and ControllingFields attributes to indicate its controlling field. Please go through the JSON then you will be understand the logic.
- setDependentOptions is private method. which invokes multiple places to fill the dependent options only if the value is selected in the controlling field and valid data available for dependent field (as we don’t have any control on order of the wire method execution).
Please go through the below links for more details :