Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RatanPaul/0d25d9f522b5fc76e8e670e05d5474da to your computer and use it in GitHub Desktop.
Save RatanPaul/0d25d9f522b5fc76e8e670e05d5474da to your computer and use it in GitHub Desktop.
global class AccountEmailService implements Messaging.InboundEmailHandler {
public static Schema.DescribeSObjectResult objectDescribe = Account.getSObjectType().getDescribe();
public static Map<String, Schema.SObjectField> fields;
static{
fields = objectDescribe.fields.getMap();
}
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
List<String> recordDataLines = new List<String>();//store to CSV's each data lines
Map<String, String> fieldNameToFieldType = new Map<String, String>();
Map < String, Decimal> fieldNumberMap = new Map < String, Decimal>();//Store the Field API name and column no/position in text attachment
try{
//fetch metadata type
for(Account_FieldMapping__mdt fieldMapping: [SELECT Id, MasterLabel, Field_API_Name__c,Column_Number__c from Account_FieldMapping__mdt]){
fieldNumberMap.put(fieldMapping.Field_API_Name__c, fieldMapping.Column_Number__c);
}
List<Document> lstDocument = new List<Document>();
Folder fd = [SELECT Id from Folder WHERE Name = 'My Folder Files'];
//get the CSV attachment from email and store in recordDataLines
for(Messaging.Inboundemail.TextAttachment att : email.textAttachments){
recordDataLines = att.body.trim().split('\n');
Document d = new Document();
d.Name = att.fileName;
d.body = Blob.valueOf(att.body);
d.folderid = fd.Id;
lstDocument.add(d);
}
insert lstDocument;
List<Account> lstAccount = new List<Account> ();
//get the data from 2nd row
for (Integer i = 0; i < recordDataLines.size(); i++) {
List<String> csvRecordData = recordDataLines[i].split(',');
Account objAcc = new Account();
//iterate over each field and based on their data type store in the Service_Time_Entry__c fields
for (String fieldName: fieldNumberMap.KeySet()) {
Integer fieldNumber = Integer.valueOf(fieldNumberMap.get(fieldName));
fieldMapping( fieldNumber, csvRecordData[fieldNumber], fieldName, fieldNameToFieldType, objAcc );
}
lstAccount.add(objAcc)
}
insert lstAccount;
}
catch(Exception e){
System.debug('========='+e.getMessage());
}
return result;
}
//based on the field data type convert the data and save in Account
public static void fieldMapping(Integer fieldNumber, String fieldValue, String fieldName, Map<String, String> fieldNameToFieldType, Account objAcc){
if(fieldNameToFieldType.get(fieldName) == 'DATE'){
objAcc.put(fieldName.trim(), Date.valueOf(fieldValue));
}else if(fieldNameToFieldType.get(fieldName) == 'CURRENCY'){
objAcc.put(fieldName.trim(), Decimal.valueOf(fieldValue));
}else {
objAcc.put(fieldName.trim(), String.valueOf(fieldValue).trim());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment