+ Start a Discussion

Update Attachment without 'Re-Attaching' it once done.


Is there a way to have them able to update an attachment file and have it update the attached file without having to re-attach a new version and deleting the old one?


Find below the sample code :




                     List<Attachment> lstDoc = Database.query('SELECT d.parentid, d.Name, d.Body FROM Attachment d ); // you can use your own filter criteria..

                   if(lstDoc != null && lstDoc.size() > 0)


                                lstDoc[0].body = filebody;

                              lstDoc[0].name = filename;

                                lstDoc[0].parentid = parentid;

                                update lstDoc[0];




                transient Attachment a = new Attachment();

                a.body = filebody;

                a.name = filename;

                a.parentid = parentid;

                insert a;



           catch(Exception e)


                ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,e.getmessage());




Hope this helps