You need to sign in to do that
Don't have an account?

How to create attachment to a related object from custom button
I have a custom button which instigates Apex to create a PDF and attach it to the record - this works. What I now want it to attach the PDF to a related object (child/parent relationship) but struggling with the ParentID - I underlined where the error is occurring.
Here is the code I have
public with sharing class SaveAndOpenPDF { public String recordId { get { return ApexPages.currentPage().getParameters().get('Id'); } } // this is to make testing a lot easier -- simply append renderAs=html // to the url parameters to test, add displayOnly=1 if you don't want to save public String renderAs { get { if (String.isBlank(ApexPages.currentPage().getParameters().get('renderAs'))) { return 'pdf'; } else { return ApexPages.currentPage().getParameters().get('renderAs'); } } } public PageReference saveAndOpenPDF() { if (String.isBlank(ApexPages.currentPage().getParameters().get('displayOnly'))) { Id attachmentId = savePDF(); return openPDF(attachmentId); } else { return null; } } public Id savePDF() { Attachment attachment = new Attachment(); attachment.ParentId = 'aFA.Id'; attachment.name = 'PDF_'+String.valueof(Datetime.now())+'.pdf'; PageReference pdf = Page.SaveAndOpenPDF; pdf.getParameters().put('Id', recordId); pdf.getParameters().put('displayOnly', '1'); pdf.setRedirect(true); try { attachment.Body = pdf.getContent(); } catch (VisualForceException e) { attachment.Body = Blob.valueof('There was an error.'); } attachment.ContentType = 'application/pdf'; insert attachment; return attachment.Id; } public PageReference openPDF(Id attachmentId) { PageReference ret = new PageReference('/servlet/servlet.FileDownload?file=' + attachmentId); ret.setRedirect(true); return ret; } }
Here is the code I have
public with sharing class SaveAndOpenPDF { public String recordId { get { return ApexPages.currentPage().getParameters().get('Id'); } } // this is to make testing a lot easier -- simply append renderAs=html // to the url parameters to test, add displayOnly=1 if you don't want to save public String renderAs { get { if (String.isBlank(ApexPages.currentPage().getParameters().get('renderAs'))) { return 'pdf'; } else { return ApexPages.currentPage().getParameters().get('renderAs'); } } } public PageReference saveAndOpenPDF() { if (String.isBlank(ApexPages.currentPage().getParameters().get('displayOnly'))) { Id attachmentId = savePDF(); return openPDF(attachmentId); } else { return null; } } public Id savePDF() { Attachment attachment = new Attachment(); attachment.ParentId = 'aFA.Id'; attachment.name = 'PDF_'+String.valueof(Datetime.now())+'.pdf'; PageReference pdf = Page.SaveAndOpenPDF; pdf.getParameters().put('Id', recordId); pdf.getParameters().put('displayOnly', '1'); pdf.setRedirect(true); try { attachment.Body = pdf.getContent(); } catch (VisualForceException e) { attachment.Body = Blob.valueof('There was an error.'); } attachment.ContentType = 'application/pdf'; insert attachment; return attachment.Id; } public PageReference openPDF(Id attachmentId) { PageReference ret = new PageReference('/servlet/servlet.FileDownload?file=' + attachmentId); ret.setRedirect(true); return ret; } }
You have to pass ID to Attachment.parentId but you are passing string('aFA.Id').
That's why you are getting error.
Plase pass related object Id as ParentId for attachment.
Ex:Lets assume that account is related object Now you have to pass account ID to Attachment.parentId.
Let us know if you have any issues.
Mark it as best answer if it works.
Thanks.
,,,,,,but how do I get the related object ID to work?
For example the related object is called related_object__c where or how would I pass that ID into the apex code?