Foundations Apex API Developer Reference

fferpcore.DataSourceBase

global with sharing abstract class DataSourceBase extends DataSource

Base support for DataSource, specifically handling relations.

This class extends fferpcore.DataSource

Methods

DataSourceBase

global DataSourceBase()

Legacy constructor

DataSourceBase

global DataSourceBase(SObjectType objectType)

Input Parameters

Name Type Description
objectType SObjectType The object type that this data source queries.

getSObjectType

global virtual SObjectType getSObjectType()

Return Value

The object type that this data source queries.

requireLookupField

global virtual override fferpcore.DataSource requireLookupField(SObjectField field)

Implements requireLookupField from the fferpcore.DataSource interface.

Input Parameters

Name Type Description
field SObjectField The field representing the lookup

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class MyDataSource extends fferpcore.DataSourceBase
{
    //Doesn't override requireLookupField

    // ...
}

// Given a Worker__c that has a Department__c, find the department name.
MyDataSource workerDataSource = createWorkerDataSource(); //Make a MyDataSource object.
fferpcore.DataSource departmentDataSource = workerDataSource.requireLookupField(Worker__c.Department__c);
departmentDataSource.requireField(Department__c.Name);

requireLookupField

global virtual override fferpcore.DataSource requireLookupField(String fieldName)

Implements requireLookupField from the fferpcore.DataSource interface.

Input Parameters

Name Type Description
fieldName String The field name representing the lookup

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class MyDataSource extends fferpcore.DataSourceBase
{
    //Doesn't override requireLookupField

    // ...
}

// Given a Worker__c that has a Department__c, find the department name.
MyDataSource workerDataSource = createWorkerDataSource(); //Make a MyDataSource object.
fferpcore.DataSource departmentDataSource = workerDataSource.requireLookupField(Worker__c.Department__c);
departmentDataSource.requireField(Department__c.Name);

requireOneToManyField

global virtual override fferpcore.DataSource requireOneToManyField(fferpcore.DataSource.BackReference backReference)

Implements requireOneToManyField from the fferpcore.DataSource interface.

Input Parameters

Name Type Description
backReference fferpcore.DataSource.BackReference The link between the child object and the parent object.

Return Value

An fferpcore.DataSource for the related records.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class MyDataSource extends fferpcore.DataSourceBase
{
    //Doesn't override requireOneToManyField

    // ...
}

// Given a Department__c, find the Worker__c in and then the payroll number.
MyDataSource departmentDataSource = createDepartmentDataSource(); //Make a MyDataSource object.
fferpcore.DataSource workerDataSource = departmentDataSource.requireOneToManyField(new fferpcore.DataSource.BackReference(Worker__c, Worker__c.Department__c));

workerDataSource.requireField(Worker__c.PayrollNumber__c);

requireField

global virtual override void requireField(String fieldName)

Implementation of requireField which parses the supplied fieldName using parseFieldName().

runQuery

global virtual override Iterator<fferpcore.DataSource.Row> runQuery()

Implements runQuery from the fferpcore.DataSource interface.
This implementation fetches records using the queryRecords abstract method, then uses these to ask the support classes to query their related results. The result is used to return fferpcore.DataSource.Row instances created using the virtual createRow() method.

Return Value

An instance of fferpcore.DataSource.Row objects.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class ExampleDataSource extends fferpcore.DataSourceBase
{
    private Set<SObjectField> m_fields;

    public ExampleDataSource()
    {
    }

    public override void requireField(SObjectField field)
    {
        m_fields.add(field);
    }

    public override Iterator<fferpcore.DataSource.Row> runQuery()
    {
        // Run SOQL to retrieve the data in m_fields and related records.
    }

}

parseFieldName

global virtual SObjectField parseFieldName(String spec)

Determines the object field from the field specification provided.
This method defines the contract of the field accessor methods that accept string keys. A field specification can be 'SObjectType.fieldName' or 'fieldName'. The latter requires the fferpcore.DataSourceBase to have been constructed with an SObjectType.

Input Parameters

Name Type Description
spec String The field specification of the form 'SObjectType.fieldName' or 'fieldName'.

fferpcore.DataSourceBase.RelatedDataSource

global interface RelatedDataSource

Contract with data sources created for lookups. DataSources may implement this interface directly, or an Adapter Pattern may be used to connect the related DataSource.
Implemented by fferpcore.SObjectByIdDataSource

Methods

asDataSource

DataSource asDataSource()

Return this datasource as an instance of DataSource. In most cases implementors will implement both interfaces and return themselves from this method, though the returned fferpcore.DataSource does not have to be the same instance as the RelatedDataSource.

runLookupQuery

RelatedQueryResult runLookupQuery(SObjectField lookupField, fferpcore.DataSourceBase.DirectQueryResult source)

Perform the query given the set of foreign keys from the parent DataSource.

Input Parameters

Name Type Description
lookupField SObjectField The foreign key field on the source Rows
source fferpcore.DataSourceBase.DirectQueryResult The result of the owner DataSource's query.

fferpcore.DataSourceBase.RelatedQueryResult

global interface RelatedQueryResult

The result from calling runLookupQuery on a RelatedDataSource.

Methods

getRelatedRow

DataSource.Row getRelatedRow(SObjectField lookupField, fferpcore.DataSource.Row parentRow)

Return the row corresponding to the given key.

Input Parameters

Name Type Description
lookupField SObjectField The field on the parent row in which to find the key.
parentRow fferpcore.DataSource.Row The row to look up the key on.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class ExampleRelatedQueryResult implements fferpcore.DataSourceBase.RelatedQueryResult
{
    private Map<Id, fferpcore.DataSource.Row> m_rowsById;

    //... Has a constructor.

    public DataSource.Row getRelatedRow(SObjectField lookupField, fferpcore.DataSource.Row parentRow)
    {
        return m_rowsById.get( (Id) parentRow.getFieldValue(lookupField) );
    }
}

fferpcore.DataSourceBase.OneToManyRelatedDataSource

global interface OneToManyRelatedDataSource

Contract with data sources acting as one-to-many relationships with this DataSource. DataSources may implement this interface directly, or an Adapter Pattern may be used to connect the related DataSource.
Implemented by fferpcore.SObjectByIdDataSource

Methods

asDataSource

DataSource asDataSource()

Return this datasource as an instance of DataSource. In most cases implementors will implement both interfaces and return themselves from this method, though the returned fferpcore.DataSource does not have to be the same instance as the OneToManyRelatedDataSource.

runOneToManyQuery

OneToManyQueryResult runOneToManyQuery(fferpcore.DataSource.BackReference backReference, fferpcore.DataSourceBase.DirectQueryResult parentSource)

Perform the query given the set of foreign keys from the parent DataSource.

Input Parameters

Name Type Description
backReference fferpcore.DataSource.BackReference The reference to query against
parentSource fferpcore.DataSourceBase.DirectQueryResult The result of the query for parent records.

fferpcore.DataSourceBase.OneToManyQueryResult

global interface OneToManyQueryResult

Query result from an OneToManyRelatedDataSource.

Methods

getOneToManyResult

Iterator<DataSource.Row> getOneToManyResult(fferpcore.DataSource.BackReference backReference, fferpcore.DataSource.Row masterRow)

Return the rows corresponding to the given BackReference on the given Master Row.

Input Parameters

Name Type Description
backReference fferpcore.DataSource.BackReference The link between the child records and the parent.
masterRow fferpcore.DataSource.Row The parent record.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public class ExampleOneToManyQueryResult implements fferpcore.DataSourceBase.OneToManyQueryResult
{
    private final Map<Id, List<fferpcore.DataSource.Row>> m_rowsByForeignKey;

    // ... has a constructor

    /**
     * Return the rows corresponding to the given key.
     */
    public Iterator<fferpcore.DataSource.Row> getOneToManyResult(fferpcore.DataSource.BackReference backReference, fferpcore.DataSource.Row masterRow)
    {
        SObjectField parentPrimaryKeyField = backReference.getPrimaryKeyField();
        Id masterPrimaryKey = (Id) masterRow.getFieldValue(parentPrimaryKeyField);
        List<fferpcore.DataSource.Row> rows = m_rowsByForeignKey.get(masterPrimaryKey);          
        return rows != null ?
            rows.iterator() : new List<fferpcore.DataSource.Row>().iterator();
    }
}

fferpcore.DataSourceBase.DirectQueryResult

global interface DirectQueryResult

Means by which the queryRelatedDataSources method can access keys to query by.
Implemented by DataSourceBase.SimpleDirectQueryResult.

Methods

getRecords

List<SObject> getRecords()

Return the result of this query.

getPrimaryKeys

Set<Id> getPrimaryKeys()

Used by the default implementation of OneToManyRelatedDataSource to find the set of what to it are foreign keys to look up.

Return Value

The set of primary keys for all records in this source not including null. These are the foreign keys in the one to many related DataSource.

getKeysForField

Set<Id> getKeysForField(SObjectField field)

Used by the default implementation of RelatedDataSource to find the set of keys to query for.

Input Parameters

Name Type Description
field SObjectField The field to query.

Return Value

The set of keys, not including null, for the given field.

fferpcore.DataSourceBase.SimpleDirectQueryResult

global class SimpleDirectQueryResult implements DirectQueryResult

An implementation of DirectQueryResult encapsulates a list of SObject records.

This class implements the following interfaces:

Methods

SimpleDirectQueryResult

global SimpleDirectQueryResult(List<SObject> records)

Input Parameters

Name Type Description
records List<SObject> The records to encapsulate.

getRecords

global List<SObject> getRecords()

Return Value

The list of records in this result.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public void checkGetRecords(List<SObject> records)
{
    fferpcore.DataSourceBase.DirectQueryResult directResult = new fferpcore.DataSourceBase.SimpleDirectQueryResult(records);
    List<SObjects> moreRecords = directResult.getRecords();
    System.assertEquals(records, moreRecords);
}

getPrimaryKeys

global Set<Id> getPrimaryKeys()

Return Value

The set of IDs, not including null, for all records in this source.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

public checkAccountKeys(Set<Id> idsToQuery)
{
    List<Account> accounts = [SELECT Id, AccountNumber, BillingCity WHERE Id IN :idsToQuery]

    fferpcore.DataSourceBase.DirectQueryResult directResult = new fferpcore.DataSourceBase.SimpleDirectQueryResult(accounts);

    Set<Id> moreIds = directResult.getPrimaryKeys();
    System.assertEquals(idsToQuery, moreIds);
}

getKeysForField

global Set<Id> getKeysForField(SObjectField field)

Input Parameters

Name Type Description
field SObjectField The field to query.

Return Value

The set of keys, not including null, for the given field.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

//Get some workers, then find all their departments.
List<Worker__c> workers = [SELECT Id, Name, Department__c FROM Worker__c];
fferpcore.DataSourceBase.SimpleDirectQueryResult simpleResult = new fferpcore.DataSourceBase.SimpleDirectQueryResult(workers);
Set<Id> departmentIds = simpleResult.getKeysForField(Worker__c.Department__c);
//You now have all the department Ids for all your workers. You can now go ahead and run soql 
//to retrieve their email addresses, and email them all to tell them how great their staff are.

fferpcore.DataSourceBase.Row

global class Row extends DataSource.Row

Implementation of the fferpcore.DataSource.Row abstract class. Represents the result of reading a Row from the database.

This class extends fferpcore.DataSource.Row

Methods

Row

global Row(SObject record, Map<SObjectField, fferpcore.DataSourceBase.RelatedQueryResult> relatedResults, Map<fferpcore.DataSource.BackReference, fferpcore.DataSourceBase.OneToManyQueryResult> oneToManyResults)

Input Parameters

Name Type Description
record SObject The record to encapsulate
relatedResults Map<SObjectField, fferpcore.DataSourceBase.RelatedQueryResult> Map of RelatedQueryResult by the field that looks them up.
oneToManyResults Map<fferpcore.DataSource.BackReference, fferpcore.DataSourceBase.OneToManyQueryResult> Map of OneToManyQueryResult by the DataSource.BackReference that looks them up.

getFieldValue

global override Object getFieldValue(SObjectField field)

Implementation of getFieldValue(field) on DataSource.Row.

Input Parameters

Name Type Description
field SObjectField The field to be queried

Return Value

The value of the requested field. May give an exception if the field was not queried.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

trigger ExampleTrigger on Order (before insert) 
{
    fferpcore.TriggerDataSource source = new fferpcore.TriggerDataSource(Records);
    fferpcore.DataSource userDataSource = source.requireField(Order.CreatedBy); 
    userDataSource.requireField(User.Email);

    Iterator<fferpcore.DataSource.Row> dataSourceRowIterator = source.runQuery(); // runQuery should call runQuery on related data sources. E.g. on userDataSource

    while(dataSourceRowIterator.hasNext())
    {
        fferpcore.DataSource.Row orderSourceRow = dataSourceRowIterator.next();
        fferpcore.DataSource.Row userSourceRow = orderSourceRow.getRelation(Order.CreatedBy);
        String createdByEmail = userSourceRow.getField(User.Email);

        //Create and send a message with the email in it.
    }
}

getFieldValue

global virtual override Object getFieldValue(String fieldName)

Implementation of getFieldValue(field) on DataSource.Row.

Input Parameters

Name Type Description
fieldName String The field to be queried

Return Value

The value of the requested field. An exception might occur if the field was not queried.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

trigger ExampleTrigger on Order (before insert) 
{
    fferpcore.TriggerDataSource source = new fferpcore.TriggerDataSource(Records);
    fferpcore.DataSource userDataSource = source.requireField(Order.CreatedBy); 
    userDataSource.requireField(User.Email);

    Iterator<fferpcore.DataSource.Row> dataSourceRowIterator = source.runQuery(); // runQuery should call runQuery on related data sources. E.g. on userDataSource

    while(dataSourceRowIterator.hasNext())
    {
        fferpcore.DataSource.Row orderSourceRow = dataSourceRowIterator.next();
        fferpcore.DataSource.Row userSourceRow = orderSourceRow.getRelation(Order.CreatedBy);
        String createdByEmail = userSourceRow.getField(User.Email);

        //Create and send a message with the email in it.
    }
}

getRelation

global override fferpcore.DataSource.Row getRelation(SObjectField field)

Implementation of getRelation(field) on DataSource.Row.

Input Parameters

Name Type Description
field SObjectField The foreign key of the relation.

Return Value

The DataSource.Row that represents the given relation. Returns null if the relation is missing or was not queried.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

trigger ExampleTrigger on Order (before insert) 
{
    fferpcore.TriggerDataSource source = new fferpcore.TriggerDataSource(Records);
    fferpcore.DataSource userDataSource = source.requireField(Order.CreatedBy); 
    userDataSource.requireField(User.Email);

    Iterator<fferpcore.DataSource.Row> dataSourceRowIterator = source.runQuery(); // runQuery should call runQuery on related data sources. E.g. on userDataSource

    while(dataSourceRowIterator.hasNext())
    {
        fferpcore.DataSource.Row orderSourceRow = dataSourceRowIterator.next();
        fferpcore.DataSource.Row userSourceRow = orderSourceRow.getRelation(Order.CreatedBy);
        String createdByEmail = userSourceRow.getField(User.Email);

        //Create and send a message with the email in it.
    }
}

getRelation

global virtual override fferpcore.DataSource.Row getRelation(String fieldName)

Implementation of getRelation(fieldName) on DataSource.Row.

Input Parameters

Name Type Description
fieldName String The foreign key of the relation.

Return Value

The DataSource.Row that represents the given relation. Returns null if the relation is missing or was not queried.

getOneToMany

global override Iterator<fferpcore.DataSource.Row> getOneToMany(fferpcore.DataSource.BackReference backRef)

Implementation of getOneToMany(backRef) on DataSource.Row. Return an Iterator of fferpcore.DataSource.Row that represent the given one to many relationship. Returns an empty Iterator if the relationship has no related Rows or was not queried.

Input Parameters

Name Type Description
backRef fferpcore.DataSource.BackReference The link between the parent object and the child record.

Return Value

An iterator of Row objects.

Sample Code

//Note: This sample code is for demonstration purposes only. It is not intended for
//use in a production environment, is not guaranteed against defects or errors, and
//is in no way optimized or streamlined.

// Given a Deparment__c find the Employee payroll numbers.
new fferpcore.DataSource.BackReference workerDepartmentBackRef = new new fferpcore.DataSource.BackReference(Worker__c.SObjectType, Worker__c.Department__c);
fferpcore.DataSource employeeDataSource = departmentDataSource.requireOneToManyField(workerDepartmentBackRef);
employeeDataSource.requireField(Worker__c.PayrollNumber__c);

Iterator<fferpcore.DataSource.Row> departmentRows = departmentDataSource.runQuery();
while(departmentRows.hasNext())
{
    fferpcore.DataSource.Row departmentRow = departmentRows.next();
    Iterator<fferpcore.DataSource.Row> workerRows = departmentRow.getOneToMany(workerDepartmentBackRef);
    //Do something with all the workers in this department.
}

parseFieldName

global virtual SObjectField parseFieldName(String spec)

Determines the contract for the string based field accessor methods. Accepts either a single field name or the combination objectType.fieldName as used by the fferpcore.Path class.

© Copyright 2009–2022 FinancialForce.com, inc. All rights reserved. Various trademarks held by their respective owners.