+ Start a Discussion
StenderStender 

Cloning emailMessage and attachments

Hello,

 

I have some custom code written that is used to 'help' email to case.  Basically, what happens is that if an email is received for an already "Closed" case (via email-to-case) a trigger on the EmailMessage object does some evaluations and then creates a NEW child case and then creates a clone of the original EmailMessage and relates it to the new case.  I would simply just change the case the original emailMessage is related to, but we don't have access to change the ParentID field on the emailmessage record.

 

My issue is in getting the ATTACHMENTS to the orignal email and relating them to the new email.  When I query in my class for the attachments it returns 0 rows when testing and I KNOW there is an attachment on the email (the original emailMessage shows an attachment as well).  Is this because I am triggering on insert of the EmailMessage record, meaning that the attachment records have not yet been inserted?

 

If that IS the case is there any way I can get these attachments and related them to the correct cloned email at their insertion?  I just don't see how I will be able to hold the ID(s) needed in memory between the two separate DML operations that are happening (insertion of the original Email and then insertion of the Attachments)

 

Any help/ideas would be appreciated.

Ashish_SFDCAshish_SFDC

Hi Jeremy, 

 

When the user replies to the email then Salesforce catches the thread ID and associates it with the case notifying the case owner. 

 

Not sure if this works, 

 

We have to see if we can redirect the case to a email to case email id. 

 

I think you can create a trigger which sends the email sent to a closed case to a Specific Email address. 

 

When the email hits the specific email address it can be Re Directed to a EMail to Case which can create a new case. 

 

 

Ashish_SFDCAshish_SFDC

 

Also, 

 

Please vote on the Idea, 

 

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

 

See the discussion for more info, 

 

https://success.salesforce.com/answers?id=90630000000gj1MAAQ

 

Regards,

Ashish

StenderStender

Thanks Ashish, but I think I might not have gotten my point across well.  The issue is not with creating the new case it is with getting the ATTACHMENTS from th original email and either re-attaching them to the new case or the 'cloned' email.

 

I think by indicating the order of things that happen will highlight this:

 

1.  Email comes in via email-to-case for an existing email.

2.  EmailMessage record created by system and attached to case.

     a.  Custom trigger/code evaluates and triggers auto-matic creation of a new case.

     b.  Custom code then 'clones' the elements of the original email and creates a NEW EmailMessage record related to the new case.

3.  THEN the ATTACHMENT records for the original email are added to that case.  I need to be able to capture these and relate the (or clone them) and relate them to the NEW EMAIL.  However, how am I going to be able to identify these Attachments and whether they need to be moved as this is coming from a completely separate DML operation/save event?

 

If we could re-parent emailmessage records it would make this so much easier, I agree so I will vote on that idea.

 

Thanks,
Jeremy Stender

StenderStender
Had another thought after re-reading your response Ashihs. Is there some sort of 'redirecting' or 'forwarding' functionality off of the EmailMessage object that might cause the attachments to be included in the email that is forwarded? If that is the case, that might work. I could just create the new case manually (as I already am doing) and then just replace the thread ID in the original email and forward it back to email to case address where it will be attached to the new case by that functionality.

However
Ashish_SFDCAshish_SFDC

Hi Jeremy, 

 

Thread id a combination of Org Id and Case Id. See if that helps. 

 

Generating an Email-to-Case thread id using Apex

Here’s how to generate an Email-To-Case thread ID for insertion into an email body or subject line.

You’ll need to pass in the id of the Case you want replies threaded to.

private String getThreadId(String caseId){ return '[ ref:_'  + UserInfo.getOrganizationId().left(4)  + '0'  + UserInfo.getOrganizationId().mid(11,4) + '._'  + caseId.left(4) + '0'  + caseId.mid(10,5) + ':ref ]'; }

 

http://limitexception.herod.net/2013/06/22/code-snippet-generating-an-email-to-case-thread-id-using-apex/

 

Regards,

Ashish

Ashish_SFDCAshish_SFDC

Also, 

 

See try this, 

 

"ref:00D"&MID(Id,4,1)&RIGHT($Organization.Id, 4) &"."& LEFT(Id,4)&RIGHT(Id,5) &":ref"

 

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

 

 

Regards,

Ashish

StenderStender
Hi Ashish,

Again I do not think you are understanding my issue. Please re-read and realize I am talking about the ATTACHMENTS, not the email itself.

Thanks,
Jeremy Stender
Ashish_SFDCAshish_SFDC

Hi Jeremy, 

 

Currently, It is not possible to modify/change the "Related To" or ParentID field. 

 

Also, vote for this idea. 

 

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

 

Regards,

Ashish

Ashish_SFDCAshish_SFDC

HI Jeremy, 

 

Came across this post, might help, This is talking about Object and you might have to add the record level commands in the code. 

 

You can start of with the below code.

 

The below code is not bulkified, so be careful when using dataloader to insert records in Custom Object. 

 

trigger CopyAttachments on CustomObject(after insert)
{
 Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Opportunity__c];
 Attachment[] insertAttList = new Attachment[]{};
 
         for(Attachment a: attList)
         {
               Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
               insertAttList.add(att);
         }
       if(insertAttList.size() > 0)
       {
            insert insertAttList;
       }
 
}

 

http://boards.developerforce.com/t5/Apex-Code-Development/Cloning-Attachments-from-one-object-to-another/td-p/279009

 

Regards,

Ashish

 

Greer BanksGreer Banks
Stender, did you ever find an answer to this? I am trying to do the same type of thing- copy attachments from a case's email message to the account, but when I query the attachments from the new email, it returns 0 rows.
Niels PetietNiels Petiet
Hi Stender - I don't have an answer for the attachments and hope you were able to resolve but curious if you were indeed successful in cloning the email and associating the cloned email to a new case.  With the Related To on EmailMessage not being modifiable I am curious how you did that.  We have similar use case so would be good to know!  Best and thanks!
Greer BanksGreer Banks
You can use @Future to run the apex after the attachment has been inserted with the EmailMessage record
Niels PetietNiels Petiet
Hi Greer I am actually not trying to move or copy attachments but really just curious how you (and Stender) were able to bypass the apparent limitation to update the Related To on a cloned EmailMessage. Or perhaps that's not a limitation when cloning and creating a new emailmessage record?
Ian Dexter 3Ian Dexter 3

Hi, anyone able to get the solution on this?

I think Salesforce has a lot of updates from 2013 up to now regarding attachments, files, and document.

I'm able to verify that the files attachment are store in  ContentDocument object, I'm facing now a problem on cloning the ContentDocumentLink to relate it on the new Case / Email Message.

 

Here's my query, when I use a variable or a collection ( IN or =: ) in my query it does not return any record even though it has the correct Email Message Id, but when I try to hardcode the id on the query ( = '02s213213219213') it returns the record.

List<ContentDocumentLink> cntntDocLinkList = [SELECT Id, LinkedEntityId, ShareType, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :emailId];

Ian Dexter 3Ian Dexter 3

Hi, was able to clone the attachments but only linked to the new case.

Btw, the solution is I create a future method with EmailMessage Ids to query the ContentDocumentLink using LinkedEntityId field
User-added image