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
Sowmya YakkalaSowmya Yakkala 

How to delete data from EmailMessage

When i am trying to delete ToAddress Field data From EmailMessage Object I am Getting Error like.
Delete failed.  INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

I tried with Query 
List<EmailMessage> lst = [Select ToAddress From EmailMessage];
Delete lst;

can any one Plz respond to above  Problem .
Thanks in advance.
Deepali KulshresthaDeepali Kulshrestha
Hi Sowmya,

I have gone through your question. 

If you delete the list of the Email Message whole record of an email message is deleted.

Through your Query, you are getting outbound Email Messages which cannot be updated.

First Query the Inbound Email Message by using the status field of Email Message.
Ex:-  Select id,Status, ToAddress from EmailMessage where Status = 5;

To Update, the Email Message follow below step:-


 1:- Update() is supported only on records whose Status is Draft, and IsPrivateDraft is false.
 
 2:- If Status and IsPrivateDraft is true and CreatedBy is associated with the current user.
 
 3:- When a record is not Draft, update() is supported only for the IsExternallyVisible field
    and custom fields.
    
 4:- When a record’s Status is not Draft, update() is supported only for the IsExternallyVisible field and custom fields.
 
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com 
Ajay K DubediAjay K Dubedi
Hi Sowmya,

I have gone through your problem, It's possible that your you query of ToAddress field from EmailMessage , are related any other object's required fields.Thats why this error has been coming.

As the other commenter said, I would start by going to Setup -> Manage Users -> Profiles, and looking at the user's profile. In the "Standard Object Permissions" area, you should double-check the permissions related to EmailMessage.

Also check the all active Triggers and workflow too. It may be email fields would be used in any of object or process.

I have tested the same code , you shared and found its deleting the ToAddress in my Org successfully.

public class Emaildelete {
    public static void test()
    {
        List<EmailMessage> lst = [Select ToAddress From EmailMessage];
        system.debug(lst);
          Delete lst;
    }

}

Your EmailMessege object must be used at somewhere that are required field.You should check them First. After that You can delete the ToAddress from EmailMessage.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com