0

I am looking for advice on the best direction to go in.

A little background:

On a custom page a user can generate a report. Every time a report is generated, a unique PDF is saved into document storage. My task: I need to take this PDF and add it to the Attachment object. What is the best way of going about this? Is there a way to do this without using dataloader? My thought was to create a new attachment and update the field values:

Attachment att = new Attachment();
att.Name = BVS__c.CompanyName__c;
att.Body = BVS__c.Report__c;
att.ParentId = Contact.Id;
insert att;

But I am getting rejected from doing so. Is this possible? Or is it better to go with DataLoader?

2
  • What is the data type of BVS__c.Report__c – Ashwani Nov 22 '16 at 18:42
  • @Ashwani BCS__c is a custom object and Report__c is a field on the object with the type of Long Text Area. This field stores a link to view the PDF. An example of the data that would be stored in the field is: file://00.0.0.00/DocumentStorage/prod/B%20V/2016/11/17/B%20ID%20-%20Company%20Name%20Here.PDF – Olivia Nov 22 '16 at 18:43
2

Since the BVS__c.Report__c is long text area, program would throw exception.

The field att.Body only except data type of Blob. I believe you need to get the PDF from document storage and assign to Attachment object's Body field something like that:-

Attachment att = new Attachment();
att.Name = BVS__c.CompanyName__c;
att.Body = documentObjectRecord.Body;
att.ParentId = Contact.Id;
insert att;

Here documentObjectRecord is the object where PDF is stored and Body is a field which has data type of Blob.

As per the comment it looks like an external link. So, first get the document by call-out or just by Pagereference.getContent(); method. Make sure you add the base URL in RemoteSite settings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.