function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nwingnwing 

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?

Pradeep_NavatarPradeep_Navatar

Find below the sample code :

 

            try

                {

                     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];

                }

                else

                {

                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());

             ApexPages.addMessage(myMsg);

           }

 

Hope this helps