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
SF_ChimpSF_Chimp 

Trigger to change parent of an email-to-case attachment to a custom object

Hi,

 

I have a requirement to change the parent of an email-to-case attachment to a custom object when the case is created.

 

The flow is as follows.

 

1. An email-to-case case with record type Master__c record is created

2. Check if it has an attachment.

3. If attachment is present, created a custom object, Attach_Secure__c, record is created.

4. Make Attach_Secure__c parent of the attachment

 

Any idea of how to implement this trigger?

 

Thanks for help 

Best Answer chosen by Admin (Salesforce Developers) 
SF_ChimpSF_Chimp

Following code works fine

 

trigger SecureAttachmentParent on Attachment (before insert) {
    
    Set<ID> caseId = new Set<ID>();
    
    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;   
            caseId.add(a.ParentId);
        }
    }
    
    Map<id,Case> caseMap = new Map<id,Case>([select id,CaseNumber,RecordTypeId from Case where id in: caseId]);
    List<Secured_Attachment__c> secureAttach = new List<Secured_Attachment__c>();
    

ID rectypeid = Schema.SObjectType.customobject__c.getRecordTypeInfosByName().get('secured record').getRecordTypeId();

  


    for(Case c: caseMap.values()){
        if(c.RecordTypeId == rectypeid){
            Secured_Attachment__c secAtt = new Secured_Attachment__c(Name = 'Case '+c.CaseNumber, Case__c = c.Id);
            secureAttach.add(secAtt);
        }
    }
    
    system.debug('Line 26 working fine');
    
    if(!secureAttach.isEmpty()){
        insert secureAttach;
    }
    
    List<Secured_Attachment__c> secureAttachUpdate = new List<Secured_Attachment__c>();
    for(Attachment a: trigger.new){
        for(Secured_Attachment__c sa:secureAttach ){
            if(a.ParentId == sa.Case__c){
                a.ParentId = sa.Id;
            }
        }
    }

}

All Answers

Shiva Ramesh @ xcdhrShiva Ramesh @ xcdhr

Hi 

 

I think after created Attach_Secure__c record through trigger you need to make another copy of attachment and insert with Attach_Secure__c id as parent(Master). we can't update parentId of attachment. Following link will help you.

 

https://success.salesforce.com/ideaView?id=08730000000hULlAAM

SF_ChimpSF_Chimp

Hi Ramesh,

 

I found this app which reassigns email attachment to the case Email2Case Attachment Reassign

 

Following is the code of the app:

// Jonathan Hersh - jhersh@salesforce.com
// November 13, 2008
// original trigger name: emailAttachmentReassigner
trigger emailAttachmentVisibleOnParent 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;   
        }
    }
}
The original unit test:
public class emailAttachmentReassignerTest {
    static testmethod void attachmentTester() {
      Attachment a = new Attachment();
      a.name = 'test attachment';
      a.body = blob.valueof('attachment body');
      a.parentid = [select id from EmailMessage limit 1].id;

      insert a;  

      delete a;
    }  
  }

 

Do you think it could work in my case?

I need to create a custom object Attach_Secure__c record when Attachment is inserted and make Attach_Secure__c parent of that attachment.

 

Thanks

 

 

 

SF_ChimpSF_Chimp

Following code works fine

 

trigger SecureAttachmentParent on Attachment (before insert) {
    
    Set<ID> caseId = new Set<ID>();
    
    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;   
            caseId.add(a.ParentId);
        }
    }
    
    Map<id,Case> caseMap = new Map<id,Case>([select id,CaseNumber,RecordTypeId from Case where id in: caseId]);
    List<Secured_Attachment__c> secureAttach = new List<Secured_Attachment__c>();
    

ID rectypeid = Schema.SObjectType.customobject__c.getRecordTypeInfosByName().get('secured record').getRecordTypeId();

  


    for(Case c: caseMap.values()){
        if(c.RecordTypeId == rectypeid){
            Secured_Attachment__c secAtt = new Secured_Attachment__c(Name = 'Case '+c.CaseNumber, Case__c = c.Id);
            secureAttach.add(secAtt);
        }
    }
    
    system.debug('Line 26 working fine');
    
    if(!secureAttach.isEmpty()){
        insert secureAttach;
    }
    
    List<Secured_Attachment__c> secureAttachUpdate = new List<Secured_Attachment__c>();
    for(Attachment a: trigger.new){
        for(Secured_Attachment__c sa:secureAttach ){
            if(a.ParentId == sa.Case__c){
                a.ParentId = sa.Id;
            }
        }
    }

}

This was selected as the best answer
T ReyT Rey
Hi. I've used the trigger emailAttachmentReassigner. It works only for Inbound emails. Tried AFTER INSERT and AFTER UPDATE operations, it does not work for attachments in Outbound emails.