function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SKBSKB 

How to create DAO, DTO, Delegate class models using APEX - URGENT?

Hi,

 

My client is a salesforce consultant who insisted me to follow the OOP standards given below. The requirement is to create REST API services which would be called from external web application (Drupal website).

 

As per the client word....

 

"The REST services will provide serialised object data in JSON (JavaScript Object Notation). The JSON will be produced from serialised custom DTO classes rather than directly from SalesForce sObjects. These DTO classes will expose properties containing various aggregations, groupings and summary information"

 

Requirement:

 

Method: REST API @HTTPGET

 

Input: UserId (portal user)

 

Returns:

Account Name, Industry, Phone, Billing Address from Account Object

All Contacts [FirstName, LastName, Email, Phone, Department] for the Account

 

 

I am really confused that DTO's are wrapper classes kind of thing. I dont know how to combine the data from two objects (Account and Contacts) using DTO. What is DTO and how to start writing DTO's and how it can be used? Can anyone explain me in detail or provide me some examples would be very much helpful...

 

Thanks in advance

SKB

Emmanuel Bruno 8Emmanuel Bruno 8
Hi,
I guess what your customer wants is actaully a wrapper that you'll serialize. To combine objects you'll tipically add a contact List property to your class like that -minimalist code- :
class AccountContactDTO {

    class ContactWrapper {
        String firstname;
        /* ... */
    }
    public String name;
    /* ... */
    public List<ContactWrapper> contacts;
    
}
Then you'll be able to serialize it to something like 
{ "Name": "Acme Corp", "contacts": [ { "firstname": "john", ... }, {"firstname": "jack", ... }, ... ], ...}
 
AccountContactDTO dto = new AccountContactDTO();
Account acc = [Select Name, (Select FirstName from Contacts) from Account where ... limit 1];

dto.name = acc.Name;
// ...
for (Contact c : acc.Contacts){
    (if dto.contacts ==null) {
         dto.contacts = new List<AccountContactDTO.ContactWrapper>();
    }
    AccountContactDTO.ContactWrapper cw = new AccountContactDTO.ContactWrapper();
    cw.firstname = c.FirstName;
    // ...
    dto.contacts.add(cw);
}

String myExternalApiCallParameter = JSON.serialize(dto);

//... do callout


Hope this helps...
Of course you'd use constructors and setters to get something cleaner...

 
Emmanuel Bruno 8Emmanuel Bruno 8
arf, 2012, I read 2022 :'(