Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active November 2, 2017 14:50
Show Gist options
  • Save swapnilshrikhande/aff89f65642b80ca1bf8d4f89355fcd1 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/aff89f65642b80ca1bf8d4f89355fcd1 to your computer and use it in GitHub Desktop.
Generic Upsert Data
public static Map<String, String> upsertData(Map<String, String> send) {
// Response payload
//
Map<String, String> resp = new Map<String, String> ();
resp.put('__campaign', send.get('__campaign'));
resp.put('__form', send.get('__form'));
// Build a list of fields in the form
//
String[] formFields = selectFormDataFields(true);
// Extract public token
//
String publicToken = send.get('__data');
//Create a new instance of the batch upload,
//if the BU is found using public token then batchUpload will be populated
//by selectBatchUploadByToken function, else it will create a new BU instance.
Batch_Upload__c batchUpload = new Batch_Upload__c();
// Find the existing batch upload record
//
if (publicToken != null && !String.isBlank(publicToken.trim()) ) {
batchUpload = selectBatchUploadByToken(publicToken, formFields);
}
// Add the token to the response
// This is already done at the bootm of this function, is it required here?
resp.put('__data', batchUpload.Batch_Upload_Public_Token__c);
// Find the existing contact record
//
Contact contact = new Contact();
if (publicToken != null && !String.isBlank(publicToken.trim()) ) {
contact = selectContactByToken(publicToken);
}
// Store the batch upload describe data so we can detect field types
//
Map<String, Schema.SObjectField> BATCH_UPLOAD_FIELD_MAP = Schema.SobjectType.Batch_Upload__c.Fields.getMap();
// Loop over form fields and copy data if present
//
for(String fieldName : send.keySet()) {
if (String.isEmpty(fieldName)) {
continue;
}
// Sanity
if (fieldName.startsWith('__')) {
continue;
}
// Cleanup
fieldName = fieldName.toLowerCase().trim();
// Find the sent data
String fieldData = send.get(fieldName);
// Get the describe data
Schema.SObjectField field = BATCH_UPLOAD_FIELD_MAP.get(fieldName);
Schema.DescribeFieldResult fieldResult = field == null ? null : field.getDescribe();
// Sanity
//
if (Test.isRunningTest()) {
system.assertNotEquals(null, field, 'Internal error: field == null for ' + fieldName);
system.assertNotEquals(null, fieldResult, 'Internal error: fieldResult == null for ' + fieldName);
} else if (field == null) {
continue;
} else if (fieldResult == null) {
continue;
}
// Is this the batch upload token field? Ignore it
//
if (fieldResult.getLocalName().equalsIgnoreCase('Batch_Upload_Public_Token__c')) {
continue;
}
// Soap Type
//
String fieldType = '' + fieldResult.getSoapType();
// No data? Blank it out
//
if (String.isEmpty(fieldData)) {
// If this is a standard field, ignore blanking changes
if (fieldName.endsWith('__c') == false) {
continue;
}
if ('Boolean'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, false);
} else {
batchUpload.put(field, null);
}
continue;
}
// Handle types
//
if ('String'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, fieldData);
continue;
}
if ('Id'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, fieldData);
continue;
}
if ('Double'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, Double.valueof(fieldData));
continue;
}
if ('Integer'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, Integer.valueof(fieldData));
continue;
}
System.debug('fieldType ==> '+fieldType+' field ==> '+field+' fieldData ==> '+fieldData);
if ('Date'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, Date.valueOf(fieldData));
continue;
}
if ('DateTime'.equalsIgnoreCase(fieldType)) {
batchUpload.put(field, DateTime.valueOfGMT(fieldData));
continue;
}
if ('Boolean'.equalsIgnoreCase(fieldType)) {
if ('true'.equalsIgnoreCase(fieldData)) {
batchUpload.put(field, true);
}
if ('false'.equalsIgnoreCase(fieldData)) {
batchUpload.put(field, false);
}
continue;
}
}
// DML
upsert batchUpload;
// Done
return resp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment