You need to sign in to do that
Don't have an account?
SF_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
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
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
Hi Ramesh,
I found this app which reassigns email attachment to the case Email2Case Attachment Reassign
Following is the code of the app:
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
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;
}
}
}
}