The Apex Data Provider is designed to enhance the functionality of lookup and list form elements by enabling the integration of data obtained through custom Apex classes.
This feature is particularly beneficial when data needs to be curated or processed using specific logic tailored to your unique requirements. Additionally, it offers the flexibility to fetch data from external sources such as web services or file contents, making it an indispensable tool for dynamic data integration in your applications.
Your Apex Data Provider class must implement the following interface:
FLX_KB.KanBanInterfaces.FormBuilderApiInterface
This code demonstrates how to dynamically load and manipulate data for a Salesforce UI component, like a list or a lookup field, using Apex.
publicclasswith sharing implementing the interface FLX_KB.KanBanInterfaces.FormBuilderApiInterface{// Define List datapublicObjectloadListData(Map<String,Object> parameters) { Integer pageNumber =Integer.valueOf(parameters.get('pageNumber'));Integer pageSize =10;Integer offset = (pageNumber -1) * pageSize;Integer sizeTotal = [select count() from Opportunity];List<Map<String,Object>> opportunitiesList =getOpportunitiesList(pageSize, offset);// Return list properties and recordsreturnnewMap<String,Object>{'pageNumber'=> pageNumber,'pageSize'=> pageSize,'hasNext'=> sizeTotal > pageSize * pageNumber,'total'=> offset +opportunitiesList.size(),'records'=> opportunitiesList,// Define List Columns'columns'=>newList<Map<String,Object>>{// Column definitions {'name'=>'name','label'=>'Opportunity Name','sortable'=>'false'}, {'name'=>'contractName','label'=>'Contract Name','sortable'=>'false'}, {'name'=>'contractOwner','label'=>'Contract Owner','sortable'=>'false'}, {'name'=>'campaignName','label'=>'Campaign Name','sortable'=>'false'}, {'name'=>'campaignActualCost','label'=>'Campaign Actual Cost','sortable'=>'false'} },// Define actions'actions'=> {'view'=> {}} }; }// Collect records for the ListprivateList<Map<String,Object>> getOpportunitiesList(Integer pageSize,Integer offset) {List<Map<String,Object>> result =newList<Map<String,Object>>();// Query for opportunitiesfor (Opportunity opp : [ select Id, Name,Contract.Name,Contract.Owner.Name,Campaign.Name,Campaign.ActualCost from Opportunity order by id limit :pageSize offset :offset ]) {// Adding each opportunity to the resultresult.add({'id'=>opp.Id,'name'=>opp.Name,'contractName'=>opp.Contract.Name,'contractOwner'=>opp.Contract.Owner.Name,'campaignName'=>opp.Campaign.Name,'campaignActualCost'=>String.valueOf(opp.Campaign.ActualCost==null?0:opp.Campaign.ActualCost) }); }return result; } // Define Lookup datapublicObjectloadLookupData(Map<String,Object> parameters) {system.debug('parameters '+ parameters);List<Map<String,Object>> result =newList<Map<String,Object>>();String defaultValue = (String)parameters.get('value'); String searchString = (String)parameters.get('filter'); // Build query based on default valueString request;if (String.isBlank(defaultValue)) { request ='SELECT id, name, ActualCost from Campaign'; } else { request ='SELECT id, name, ActualCost from Campaign where id = \''+ defaultValue +'\''; }Map<Id,Campaign> campaignItems =newMap<Id,Campaign>( (List<Campaign>)Database.query(request));// Process and add campaign items to resultfor (Campaign campaignItem :campaignItems.values()) {String campaignNameAndAmount =campaignItem.Name+' - $'+ (campaignItem.ActualCost==null?'0':String.valueOf(campaignItem.ActualCost));if (String.isBlank(searchString))result.add({'Id'=>campaignItem.Id,'Name'=> campaignNameAndAmount});elseif (campaignNameAndAmount.containsIgnoreCase(searchString))result.add({'Id'=>campaignItem.Id,'Name'=> campaignNameAndAmount}); }system.debug(result);return result; }// Load related list data (currently not implemented)publicObjectloadRelatedListData(Map<String,Object> parameters) {returnnull; }}