You need to sign in to do that
Don't have an account?
Raju kanoperi
Hello Everyone, Please Modify the code using best practices Thanks.
global class OpportunityAttachmentsEmailHandler implements Messaging.Inboundemailhandler {
global Messaging.Inboundemailresult handleInboundEmail (Messaging.Inboundemail email, Messaging.Inboundenvelope envelope)
{
Messaging.Inboundemailresult result = new Messaging.Inboundemailresult();
List<Opportunity> oppsToUpdate = new List<Opportunity>();
List<Attachment> attachmentsToInser = new List<Attachment>();
try
{
for(Messaging.Inboundemail.Binaryattachment bAttachment: email.binaryAttachments)
{
string fileName = bAttachment.fileName;
System.debug('FileName: '+fileName);
if(fileName.lastindexOf('_')!=-1 && fileName.endswith('.pdf')) {
string recordId = fileName.substring(fileName.lastindexOf('_') + 1, fileName.indexOf('.pdf'));
system.debug('ProposalID: ' + recordId);
Integer temp = integer.ValueOf(recordId);
List<Opportunity> oppList = [select ID from Opportunity where ProposalPDFID__c = :temp];
if (oppList.size() > 0)
{
for(Opportunity theOpp : oppList) {
System.debug('Opportunity Id: '+theOpp.id);
Attachment proposalAtt = new Attachment();
proposalAtt.Name = bAttachment.fileName;
proposalAtt.Body = bAttachment.body;
proposalAtt.ParentId = theOpp.ID;
attachmentsToInser.add(proposalAtt);
theOpp.Have_attachment__c = true;
theOpp.Attachment_id__c = proposalAtt.id;
oppsToUpdate.add(theOpp);
}
try
{
insert attachmentsToInser;
update oppsToUpdate;
}
catch(Exception ex)
{
continue;
}
}else{
try{
Unattached_Proposals__c unattachedProposal = new Unattached_Proposals__c();
unattachedProposal.ProposalPDFID__c = temp;
insert unattachedProposal;
Attachment proposalAtt = new Attachment();
proposalAtt.Name = bAttachment.fileName;
proposalAtt.Body = bAttachment.body;
proposalAtt.ParentId = unattachedProposal.ID;
insert proposalAtt;
}
catch(Exception ex)
{
continue;
}
}
}
}
result.success = true;
}
catch(Exception e)
{
result.success = false;
System.debug('Exception: '+e.getMessage());
result.message = 'Failed to insert an attachment. '+e.getMessage()+' stack trace: '+e.getStackTraceString();
}
return result;
}
global Messaging.Inboundemailresult handleInboundEmail (Messaging.Inboundemail email, Messaging.Inboundenvelope envelope)
{
Messaging.Inboundemailresult result = new Messaging.Inboundemailresult();
List<Opportunity> oppsToUpdate = new List<Opportunity>();
List<Attachment> attachmentsToInser = new List<Attachment>();
try
{
for(Messaging.Inboundemail.Binaryattachment bAttachment: email.binaryAttachments)
{
string fileName = bAttachment.fileName;
System.debug('FileName: '+fileName);
if(fileName.lastindexOf('_')!=-1 && fileName.endswith('.pdf')) {
string recordId = fileName.substring(fileName.lastindexOf('_') + 1, fileName.indexOf('.pdf'));
system.debug('ProposalID: ' + recordId);
Integer temp = integer.ValueOf(recordId);
List<Opportunity> oppList = [select ID from Opportunity where ProposalPDFID__c = :temp];
if (oppList.size() > 0)
{
for(Opportunity theOpp : oppList) {
System.debug('Opportunity Id: '+theOpp.id);
Attachment proposalAtt = new Attachment();
proposalAtt.Name = bAttachment.fileName;
proposalAtt.Body = bAttachment.body;
proposalAtt.ParentId = theOpp.ID;
attachmentsToInser.add(proposalAtt);
theOpp.Have_attachment__c = true;
theOpp.Attachment_id__c = proposalAtt.id;
oppsToUpdate.add(theOpp);
}
try
{
insert attachmentsToInser;
update oppsToUpdate;
}
catch(Exception ex)
{
continue;
}
}else{
try{
Unattached_Proposals__c unattachedProposal = new Unattached_Proposals__c();
unattachedProposal.ProposalPDFID__c = temp;
insert unattachedProposal;
Attachment proposalAtt = new Attachment();
proposalAtt.Name = bAttachment.fileName;
proposalAtt.Body = bAttachment.body;
proposalAtt.ParentId = unattachedProposal.ID;
insert proposalAtt;
}
catch(Exception ex)
{
continue;
}
}
}
}
result.success = true;
}
catch(Exception e)
{
result.success = false;
System.debug('Exception: '+e.getMessage());
result.message = 'Failed to insert an attachment. '+e.getMessage()+' stack trace: '+e.getStackTraceString();
}
return result;
}