Skip to content

DTO_Base ​

Class · Group: Data Transfer Objects

Learn it: DTOs guide

apex
@JsonAccess(serializable='always' deserializable='always') global inherited sharing virtual class DTO_Base

Known Derived Types: DTO_BaseTable, DTO_JsonBase, DTO_NameValues, DTO_ScheduledParameterDefinition

An empty base class for Data Transfer Objects (DTOs). A DTO is a small class that holds the fields you move in or out of your org. This class only declares the methods; on their own they do nothing: serialize() returns an empty string, deserialize() returns the same instance, and populate() and transform() change nothing. Your subclass overrides the methods it needs. For DTOs that convert to and from JSON, extend DTO_JsonBase instead.

Example

apex
public class DTO_AccountSummary extends DTO_Base
{
    public String accountName;
    public override void populate(Id recordId)
    {
        accountName = 'Name read for ' + recordId;
    }
}
DTO_AccountSummary summary = new DTO_AccountSummary();
summary.populate(recordId, new DTO_NameValues());

Methods ​

MethodDescription
global virtual DTO_Base deserialize(String dtoString)Turns a string back into a DTO.
global virtual void populate(Id recordId)Populates the current DTO instance using the specified triggering object ID.
global virtual void populate(Id recordId, DTO_NameValues dtoRequestParameters)Populates the current DTO instance using the specified triggering object ID and additional request parameters.
global virtual String serialize()Returns the DTO as a string.
global virtual void transform(DTO_Base dtoBase)Transforms the current DTO using another DTO as input.

deserialize ​

apex
global virtual DTO_Base deserialize(String dtoString)

Turns a string back into a DTO. In this base class it ignores the string and returns the same instance; override it in your subclass, or extend DTO_JsonBase, to read JSON.

Parameters

ParameterTypeDescription
dtoStringStringThe string to read. This base class does not read it.

Returns DTO_Base — The same instance, unchanged, in this base class.

Example

apex
DTO_Base result = instance.deserialize('value');

populate ​

apex
global virtual void populate(Id recordId)

Populates the current DTO instance using the specified triggering object ID. This method enables retrieval of relevant data using an ID parameter.

Parameters

ParameterTypeDescription
recordIdIdThe ID of the object to retrieve data for population.

Example

apex
instance.populate(recordId);
apex
global virtual void populate(Id recordId, DTO_NameValues dtoRequestParameters)

Populates the current DTO instance using the specified triggering object ID and additional request parameters. Allows customized data retrieval and population.

Parameters

ParameterTypeDescription
recordIdIdThe ID of the object to retrieve data for population.
dtoRequestParametersDTO_NameValuesA list of name values that can be used to change the query or the population of the request DTO

Example

apex
instance.populate(recordId, new DTO_NameValues());

serialize ​

apex
global virtual String serialize()

Returns the DTO as a string. In this base class it returns an empty string; override it in your subclass, or extend DTO_JsonBase, to get JSON.

Returns String — An empty string in this base class.

Example

apex
String result = instance.serialize();

transform ​

apex
global virtual void transform(DTO_Base dtoBase)

Transforms the current DTO using another DTO as input. This method allows flexibility for child classes to implement their own transformation logic.

Parameters

ParameterTypeDescription
dtoBaseDTO_BaseThe input DTO to transform from.

Example

apex
instance.transform(new DTO_Base());