Advanced: Retrieve Data Using Apex Classes
Last updated
Last updated
Use the Advanced DataSource feature when you need to fetch data from external API servers or perform complex logic for data retrieval and merging. To set up Advanced DataSource, you must write an Apex Class. This approach provides greater flexibility compared to the Basic DataSource for fetching and processing data.
To use an Apex Class with Advanced DataSource, you must implement the SBLI.DataSourceService.Fetchable
interface.
This interface includes the execute
method, where you implement logic for processing data and returning an SBLI.DataSourceService.Result
object. The result contains mapping information between the fetched data and the Lookup or Line Item objects.
The following example assumes an external API is called to retrieve inventory quantities by product code. The data is then mapped to the Inventory__c
field in the Lookup or Line Item object.
The SBLI.DataSourceService.Fetchable
interface is provided by the package. Both the Apex class and the methods must be declared as global
to allow external access.
Fetch Parent and Lookup Records
params.getParentRecord()
: Retrieves the parent record of the object.
params.getLookupRecords()
: Retrieves the list of records from the Lookup object.
Prepare DataSource Values
Data is fetched from an external API server (e.g., inventory information).
In the example, inventory quantities are mapped based on ProductCode.
Generate Result
An SBLI.DataSourceService.Action
object is created to map the fetched data (e.g., inventory quantity) to the target field (Inventory__c).
The action is added to the SBLI.DataSourceService.Result
object.
Return the Result
The Result object, containing all mapping information, is returned to the editor.
An object containing information about the parent and lookup parameters configured in the builder.
SObject getParentRecord()
Returns the parent record. If no parent parameter is configured in the builder, the value is empty.
List getLookupRecords()
Returns the lookup records. If no lookup parameter is configured in the builder, the value is empty.
An object that contains all mapping information between the DataSource and the Lookup or Line Item objects, which is returned to the editor.
Mapping information is stored in SBLI.DataSourceService.Action
objects, which are then added to the SBLI.DataSourceService.Result
object and returned.
addAction(SBLI.DataSourceService.Action action)
Adds an Action object containing mapping information to the Result object.
action
The object containing mapping information between the Lookup/Line Item and the DataSource values.
An object that stores mapping information between the Lookup or Line Item and the DataSource values.
setKeyValue(Object keyValue)
Sets the DataSource Key value that matches the Lookup or Line Item Key.
keyValue
Object
The Key value from the DataSource.
putSourceToTargetField(Object sourceValue, String targetFieldApiName)
Assigns the DataSource value to the specified Lookup or Line Item field.
sourceValue
Object
The value from the DataSource.
targetFieldApiName
String
The API name of the target field in the Lookup or Line Item object.
After selecting the Apex Class, specify the fields to be passed as parameters if the class requires parent or lookup information.
Ensure the Apex Class is written before configuring the DataSource.
1
Apex Class
Select the Apex Class to be used for the DataSource.
β
2
Key Field
β
3
Parameter - Parent Fields
Add a parent objectβs field as a parameter if the Apex Class implementation for the DataSource requires a field value from the parent object.
4
Parameter - Lookup Fields
Add a lookup objectβs field as a parameter if the Apex Class implementation for the DataSource requires a field value from the lookup object.
5
Evaluation Event
Set the execution timing for the DataSource.
On Initialization: Executes when a new Line Item is added.
On Loading: Executes when the Line Item screen is loaded.
Before Save: Executes before a Line Item is saved.
6
Active
Turns the DataSource on or off. It must be active to be used.
β
Specify the Key Field to map data retrieved via the DataSource to the Lookup or Line Item records. The Key Field must be a field from the Lookup object. Why use a Lookup objectβs field as the Key for Line Items? Since Line Items may include unsaved records, determining a unique Key value for mapping is not feasible. Using a field from the Lookup object ensures reliable mapping.
Available only for Line Items.