+ Start a Discussion
ArunaAruna 

Create a case and attach email attachment to case as a Notes and Attachment -- Urgent

Hello there,

I am having a requirement  when email is sent from out look to  salesforce with some attachments (Like word doc or Image ect) the trigger on EmailMessage should create a case in salesforce and attach the attachments of email to as Notes and Attachments.

I am able to create the case but  unable to attach the attachment to case. :( can any one please help me on this issue .
I need to finish this task as soon as possible.

Thank you :)
 
sfdcsushilsfdcsushil
Are you able to retrieve attachment but not able to related them? What's the error coming? 
ArunaAruna
I am not understand how to get those attachmanets from email and attach to case Notes and attachments.

any sample code is help full.

Thank you :)
 
sfdcsushilsfdcsushil
You can defintely use Email Services to solve this. I am not sure if there is any other way. But its worth exploring. PFB link to email services. https://developer.salesforce.com/page/An_Introduction_To_Email_Services_on_Force.com
ArunaAruna
Here is the story

I am having a trigger on EmailMessage  this trigger(which creates case) will fire when an email is sent from out look to salesforce.
The trigger is creating case but I want i want is if there are any attachments in email i need to attach those attachments to case as Nots and Attachments.

here is the code attachment is not attached to case 

I think some problem is there in this case but I am not sure what is it  ?

trigger CreateNewCase on EmailMessage (after insert) {
    
    map<id,string> caseIdNBodyMap = new map<id,string>();
    map<id,id> emailIdNCaseIdMap= new map<id,id>();
    map<id,id> oldCaseidNnewcaseIDmap = new map<id,id>();
    list<case> toInsertCaseList = new list<case>();
    list<Attachment > toInsertAttachmentList = new list<Attachment >();
    
    for(EmailMessage em : Trigger.new){
        if(((String)em.parentid).startsWith('500') && em.Incoming){
            caseIdNBodyMap.put(em.parentid,em.TextBody);
            emailIdNCaseIdMap.put(em.id,em.parentid);
        }
    }
    system.debug('--caseIdNBodyMap---'+caseIdNBodyMap);
    system.debug('--emailIdNCaseIdMap---'+emailIdNCaseIdMap);
    Map<Id,case> caseMAP = new Map<Id,case>([SELECT id, isClosed, Type, Subject,Description FROM Case WHERE id IN: caseIdNBodyMap.keyset()]);  
   
    for(case c : caseMAP.values()){
        if(c.IsClosed) {
            Case cloneCase = new Case();
            cloneCase.parentid=c.id;  
            cloneCase.subject='NEW CASE NOTIFICATION-'+ c.subject;
            cloneCase.description=caseIdNBodyMap.get(c.id);
            toInsertCaseList.add(cloneCase);      
            
        }
    } 
    if(!toInsertCaseList.isEmpty())
        insert toInsertCaseList;
    
    system.debug('-----toInsertCaseList --'+toInsertCaseList );
        
    set<id> newcaseSet = new set<id>();
    for(case c : toInsertCaseList){
        newcaseSet.add(c.id);
    }
   
    list<case> newCaseList =[select id, parentId from case where id in: newcaseSet];
    for(case c : newCaseList){
        oldCaseidNnewcaseIDmap.put(c.parentid,c.id);
    }
     
    Attachment[] attList = [select id, ParentId, name, body from Attachment where ParentId in :emailIdNCaseIdMap.keySet()];
    system.debug('---attList ---'+attList);
    for(Attachment a :attList ){
        Attachment att = new Attachment();
        att.name = a.name;
        att.body = a.body;
       // att.parentid = oldCaseidNnewcaseIDmap.get(emailIdNCaseIdMap.get(a.ParentId));
       att.parentid = toInsertCaseList[0].id;
        toInsertAttachmentList.add(att);
    }
    if(!toInsertAttachmentList.isEmpty()){
        insert toInsertAttachmentList;
    }
    
    system.debug('-----toInsertAttachmentList--'+toInsertAttachmentList
 
sfdcsushilsfdcsushil
ok. So you need to make sure you have configured email to salesforce using below link
https://help.salesforce.com/HTViewHelpDoc?id=email_my_email_2_sfdc_setup.htm

Attachments that come from outlook are associated with the contact who sent email. So you need to take contact id from email, and then fetch attachments for that contact. Hope this helps. 
ArunaAruna
its not about the contact .
scenario is first time when i send email form out look to salesforce it is creating a new case and it send an email to out look saying that new case as been created
then manually I am changing the case status to close and give reply to email with some attachments.
It should create a new case with the attachments under notes and attachment for new case not to closed case.

the code which i have it is attaching attachments to closed case not to new case.

any sample code is apreciated 



 
Bhuvaneswary RadhakrishnanBhuvaneswary Radhakrishnan
Have you find any solution for this?