1

I have written an Email Service which goes like this :

global class myHandler implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

          EmailMessage emailMsg = new EmailMessage();
          emailMsg.RelatedToId = *CUSTOM OBJECT RECORD ID*;
          emailMsg.Subject = 'Test Subject';
          emailMsg.fromAddress = *MY EMAIL ADDRESS*;
          emailMsg.htmlBody = 'HTML';
          emailMsg.fromName = 'From Name';
          emailMsg.Incoming =  true;
          INSERT emailMsg;

          system.debug('emailid####' + emailMsg.Id);

          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          return result;

      }
  }

The Inbound email is created successfully and gets related to the Custom object Record, but I am unable to open the Email Message. It gives me error message when I am trying to open it using its record id:

Insufficient Privileges You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary. For more information, see Insufficient Privileges Errors.

However when I run the Email Message creation logic in the above code using Developer Console, the Inbound Email Message is created and am able to view it successfully!

MODIFICATION TO THE ABOVE QUESTION

Just found that only the User who has created the Email Message has permission to view, not even other System Admins can view it.

Is there any way we can by pass this scenario?

1 Answer 1

1

There are no sharing rules on EmailMessage, thus OWD is out of the question. It has do something with the apex code.

On inspection I found out you have not populated an important field in EmailMesage. Its the Status field. I dont exactly know how it internally related to sharing but once you make the value of that field as Read(1) then that email message gets publicly available.

So your code will be:

global class myHandler implements Messaging.InboundEmailHandler {
      global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {

          EmailMessage emailMsg = new EmailMessage();
          emailMsg.status='1';//Most Importantfor sharing.
          emailMsg.RelatedToId = *CUSTOM OBJECT RECORD ID*;
          emailMsg.Subject = 'Test Subject';
          emailMsg.fromAddress = *MY EMAIL ADDRESS*;
          emailMsg.htmlBody = 'HTML';
          emailMsg.fromName = 'From Name';
          emailMsg.Incoming =  true;
          INSERT emailMsg;

          system.debug('emailid####' + emailMsg.Id);

          Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
          return result;

      }
  }
3
  • Thanks for the support, Pranay! I changed the Context User to my User Id and was able to view the Email. But now only I am able to see the Email Message, other System Admins are not able to view it, other are getting the Insufficient Privileges Errors. Is there anyway for all the System Admins to view it?
    – Aditya M
    Commented May 5, 2018 at 15:32
  • It looks like sharing issue. Lets discuss it in chat. chat.stackexchange.com/rooms/77092/… Commented May 5, 2018 at 18:03
  • Updated answer to reflect the importance of the status field. Commented May 6, 2018 at 10:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .