I'm working on managed package and I have to insert Attachment record from my apex code.

The code is pretty simple, that's just create and insert Attachment, something like that:

Attachment attachment =
    new Attachment (Body = Blob.valueOf(bodyText),
                    ContentType = 'text/plain',
                    ParentId = parentId,
                    Name = 'Name');
insert attachment;

this code is exposed to customer via global class. These I can run same test code on my developer org and on org where my package is installed which will invoke my global class.

The code works fine on development org. But when I tried to use this code in another org where my managed package is installed I got the following error:

System.SObjectException: Field is not writeable: Attachment.ParentId

On both environments I run test code from System Admin user.

I see that Attachment.ParentId field was not included into Package Dependencies although this field is used in few apex classes of my package.

Can someone tell me what I missed?

  • Did you ever figure this out? I'm starting to go down a similar path, but don't want to get too far if it's not possible. If I come across a solution, I'll be sure create an answer.
    – VictorKilo
    Commented May 24, 2016 at 6:20


You must log in to answer this question.

Browse other questions tagged .