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
José Carlos LorenzaleJosé Carlos Lorenzale 

Change RelatedToId from Email Message

Hello!
There is a process in my client's daily routine which requires the possibilty to modify the RelatedToId from an Email Message to change the related Case. ¿Is there a way of achieving this? ¿How would a possible trigger be like?

Thank in advanced.
José Carlos LorenzaleJosé Carlos Lorenzale
I created a lookup field to Cases in the Email Message object named "mc_Input_a_relacionar" and wrote this simple Trigger:

trigger CambiarRelatedToId on EmailMessage (after update) {

    for(EmailMessage emsg: trigger.new){
         
        if (emsg.mc_Input_a_relacionar__c != null) {     
           
           emsg.RelatedToId = emsg.mc_Input_a_relacionar__c;
        }        
      }
}

But I get the following error: 
CambiarRelatedToId: execution of AfterUpdate caused by: System.FinalException: Record is read-only Trigger.CambiarRelatedToId: line 7, column 1
 
AmarpreetAmarpreet
You can change the "after update" to "before update" and try.
José Carlos LorenzaleJosé Carlos Lorenzale
Hello Amarpreet! First of all, thanks for responding. I changed the "after update" to "before update" but I get this error now: 

Review the following errors
You cannot edit this field
Review the following fields
Related To ID

Maybe using the api of the new feature Merge Cases?
Khan AnasKhan Anas (Salesforce Developers) 
Hi José,

Greetings to you!

Email fields, including RelatedToId, can only be changed when the email is in draft status. There's an idea which is active on the success community with a similar discussion for which you can upvote so that it gets available in the future.

https://success.salesforce.com/ideaView?id=0873A0000003aloQAA

https://success.salesforce.com/ideaView?id=0873A000000lKp6QAE

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas