Skip to content

Instantly share code, notes, and snippets.

@jkentjnr
Last active August 29, 2015 14:16
Show Gist options
  • Save jkentjnr/d6391238652e96d79323 to your computer and use it in GitHub Desktop.
Save jkentjnr/d6391238652e96d79323 to your computer and use it in GitHub Desktop.
Salesforce - DTOs to SObjects
// --------------
// DTO Base Class
global abstract class DtoBase {
global abstract String getSObjectName();
global abstract Map<String, Object> getSObjectMap();
global virtual SObject ToSObject() {
SObject obj = Schema.getGlobalDescribe().get(getSObjectName()).newSObject();
Map<String, Object> objectMap = getSObjectMap();
for (String sObjectField : objectMap.keySet()) {
obj.put(sObjectField, objectMap.get(sObjectField));
}
return obj;
}
}
global class ShiftDto extends DtoBase {
webservice Id RecordId;
webservice String Version;
webservice DateTime StartDateTime;
webservice DateTime EndDateTime;
webservice Id UserId;
webservice Id FacilityId;
webservice Id PeriodId;
webservice Id ShiftActivityId;
global override String getSObjectName() {
return 'Shift__c';
}
global override Map<String, Object> getSObjectMap() {
return new Map<String, Object> {
'Id' => this.RecordId,
'Version__c' => this.Version,
'Start_Date_Time__c' => this.StartDateTime,
'End_Date_Time__c' => this.EndDateTime,
'User__c' => this.UserId,
'Facility__c' => this.FacilityId,
'Period__c' => this.PeriodId,
'Shift_Activity__c' => this.ShiftActivityId
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment