Unit Of Work pattern (fflib library) and Attachment object

apex

Is there any way to use the fflib uow pattern to insert attachments and relate it to a parent sObject (in this case to an Event)? I am facing the following error due to the parentId is not an SObjectField.

    Attachment attachment = new Attachment();
    attachment.name = 'myName';
    attachment.ContentType = 'plainText';
    attachment.Body = Blob.valueOf('Text');
    uow.registerNew(attachment,Attachment.ParentId,EventSF);

Error:

Error:(414, 13) Method does not exist or incorrect signature: void registerNew(Attachment, Id, Event) from the type fflib_ISObjectUnitOfWork

Best Answer

You just shadowed the name of the standard object (Attachment) with your instance of it (attachment). Either rename it:

Attachment record = new Attachment();
SObjectField parentIdField = Attachment.ParentId;

Or if you don't want to change the name of your record instance, you need to use the fully qualified name to disambiguate:

Attachment attachment = new Attachment();
SObjectField parentIdField = Schema.Attachment.ParentId;
Related Topic