function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
George AdamsGeorge Adams 

Copy case email attachments to custom object

Hi all,

I'm writing an apex class that is called by a Detail Page Button to do several things. It creates a new record in a custom object called "Hearing Result". I'd like to copy the current case's Case Email attachments to this new record in the custom object.

How can I do this?

Here is my apex class so far:
 

global class logHearingResult{
    
  WebService static id createHearingResult(Id currentCase) {
   
    //new set to hold current case Ids
    Set<Id> caseIds = new Set<Id>();
    
    //add current case Id to above set (currentCase is passed from javascript button)
    caseIds.add(currentCase);
    
      
    //new list to hold cases with the current case's Id
    List<Case> results = [
        select Id, Priority, ContactId
        from Case
        where Id in :caseIds
    ];	
    
    List<Hearing_Result__c> resultsToInsert = new List<Hearing_Result__c>();  
     
    id newHearingResultLocation;
    
    //work based on current case
    for (Case c : results){
        Hearing_Result__c ins = new Hearing_Result__c (ContactID__c = c.ContactId);
		resultsToInsert.add(ins);
        
        insert ins;
        newHearingResultLocation = ins.id;
    }
    
    update results;
    return newHearingResultLocation;
      
  }
    
}
Thanks!
Best Answer chosen by George Adams
Mahesh DMahesh D
Hi George,

Please find the below code:

Here:

(1) i changed the class to normal class just for me to test in Developer Console.
(2)  We can improve it in multiple ways which you can able to do it.

 
public class logHearingResult{
    
  public static id createHearingResult(Id currentCase) {
    
    //new list to hold cases with the current case's Id
    Case caseRec = [Select Id, Priority, ContactId from Case where Id =: currentCase];  
    
    Hearing_Result__c ins = new Hearing_Result__c (ContactID__c = caseRec.ContactId);
    insert ins;
    
    List<Attachment> insertAttList = new List<Attachment>();
    
    List<Attachment> attList = [SELECT Body,BodyLength,ContentType,Description,Id,IsPartnerShared,IsPrivate,Name,ParentId FROM Attachment where ParentId =: caseRec.Id];
    System.debug('==========================attList:'+attList);
    
    if(!attList.isEmpty()) {
    for (Attachment att: attList) {           
        
        Attachment b = att.clone();
        b.parentid = ins.Id;
        insertAttList.add(b);                 
    }
    
        insert insertAttList;
    }   
    
    return ins.Id;
      
  }
    
}

Tested the program by using the below code in Developer Console:
 
Id currentCase = '50040000004VwCZ';
logHearingResult.createHearingResult(currentCase);

Also please find the screenshots of created records.

New Record created under Hearing Result object:


User-added image



Copied Attachment is here:

User-added image

Please do let me know if it helps you.

Regards,
Mahesh

All Answers

Mahesh DMahesh D
Hi George,

Please find the below code:

Here:

(1) i changed the class to normal class just for me to test in Developer Console.
(2)  We can improve it in multiple ways which you can able to do it.

 
public class logHearingResult{
    
  public static id createHearingResult(Id currentCase) {
    
    //new list to hold cases with the current case's Id
    Case caseRec = [Select Id, Priority, ContactId from Case where Id =: currentCase];  
    
    Hearing_Result__c ins = new Hearing_Result__c (ContactID__c = caseRec.ContactId);
    insert ins;
    
    List<Attachment> insertAttList = new List<Attachment>();
    
    List<Attachment> attList = [SELECT Body,BodyLength,ContentType,Description,Id,IsPartnerShared,IsPrivate,Name,ParentId FROM Attachment where ParentId =: caseRec.Id];
    System.debug('==========================attList:'+attList);
    
    if(!attList.isEmpty()) {
    for (Attachment att: attList) {           
        
        Attachment b = att.clone();
        b.parentid = ins.Id;
        insertAttList.add(b);                 
    }
    
        insert insertAttList;
    }   
    
    return ins.Id;
      
  }
    
}

Tested the program by using the below code in Developer Console:
 
Id currentCase = '50040000004VwCZ';
logHearingResult.createHearingResult(currentCase);

Also please find the screenshots of created records.

New Record created under Hearing Result object:


User-added image



Copied Attachment is here:

User-added image

Please do let me know if it helps you.

Regards,
Mahesh
This was selected as the best answer
George AdamsGeorge Adams
Thanks a bunch Mahesh. Super helpful.

I think the issue I'm having with your solution is that the attachment is attached to the Case Email, and not the case itself. So in debug, attList is empty.

Can your code be modified to grab the attachment from the Case Email?

Here's what the case looks like:

User-added image

And if I go to the email, this is what it looks like:

User-added image
 
Mahesh DMahesh D
Hi George,

I am not sure how you tested the solution,

Just for testing, can you copy my code as is and go to Developer Console and execute the below 2 lines of code by chaning the Case number:

 
Id currentCase = '50040000004VwCZ';
logHearingResult.createHearingResult(currentCase);

Step 1: Check whether the new record created under "Hearing Result".
Step 2: Attached got copied or not.
Step 3: Anything new happened on the Input Case object record.

Regards,
Mahesh
George AdamsGeorge Adams
Hi Mahesh,

The issue is that the attachment is on a sub-object of the actual case, so the code needs to look "one level deeper".

I decided to use a workaround to move the attachment from the Case Email up to the Case itself. I found this trigger online that accomplishes this:
 
trigger moveEmailAttachmentToCase on Attachment (before insert) {
    for( Attachment a : trigger.new ) {  
        // Check the parent ID - if it's 02s, this is for an email message   
       if( a.parentid == null )     
            continue;       
        String s = string.valueof( a.parentid );     
        if( s.substring( 0, 3 ) == '02s' ) {  
            a.parentid = [select parentID from EmailMessage where id = :a.parentid].parentID;   
        }
    }
}


That trigger combined with your code works perfectly, so thank you for the help!