Ask Search:
Noah RouseNoah Rouse 

Creating Button to Delete Record Associated Notes and Attachments

We have an application process using the Leads and PersonAccount objects that require applicants to submit supporting documentation. At the end of the application process, we no longer need the attachments on the Lead or Person Account.

End users are complaining that deleting individual attachments is taking too long/disruptive.

I would like to create a button (2 buttons technically, Lead & Account) that will look for and delete all attachements directly related to the record the button was clicked upon.

I have very little code background, but so far it looks like i need to invoke the apex.js and connection.js scripts, run a SOQL query to pull associated attachment records, and use the sforce.connection.delete function.

Can someone please help me put the pieces together to find a solution? 

Most solutions i have seen around deleting attachments are for much broader deletes and lean towards the Dataloader or Triggers. Since this isn't an ALWAYS delete, i figured the button was the best path forward. 

Thanks for any information!
Best Answer chosen by Noah Rouse
Amit SinghAmit Singh
Use below code for Javascript to delete the Attachments of Account Record
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 

var result = sforce.connection.query("Select Id From Attachment Where ParentID = '{!Account.Id}' ");

var records = result.getArray("records");
  var idsForDeletion = [];

  for (var i=0; i<records.length; i++) {
      idsForDeletion.push(records[i].Id);
  }

  sforce.connection.deleteIds(idsForDeletion);
  window.location.reload();

Below for Lead Object
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 

var result = sforce.connection.query("Select Id From Attachment Where ParentID = '{!Lead.Id}' ");

var records = result.getArray("records");
  var idsForDeletion = [];

  for (var i=0; i<records.length; i++) {
      idsForDeletion.push(records[i].Id);
  }

  sforce.connection.deleteIds(idsForDeletion);
  window.location.reload();

 

All Answers

Amit SinghAmit Singh
Hi Noah, Appreciate your time for making this post here but  I would Suggest you to Please redirect your question into Salesforce Developer Forum and Stack Exchange. As this community is based on Configuration and Customization Part.
https://developer.salesforce.com/forums#!/feedtype=RECENT&criteria=ALLQUESTIONS&
https://salesforce.stackexchange.com/


Thanks,
AMIT SINGH
Amit SinghAmit Singh
Use below code for Javascript to delete the Attachments of Account Record
 
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 

var result = sforce.connection.query("Select Id From Attachment Where ParentID = '{!Account.Id}' ");

var records = result.getArray("records");
  var idsForDeletion = [];

  for (var i=0; i<records.length; i++) {
      idsForDeletion.push(records[i].Id);
  }

  sforce.connection.deleteIds(idsForDeletion);
  window.location.reload();

Below for Lead Object
{!REQUIRESCRIPT("/soap/ajax/25.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/25.0/apex.js")} 

var result = sforce.connection.query("Select Id From Attachment Where ParentID = '{!Lead.Id}' ");

var records = result.getArray("records");
  var idsForDeletion = [];

  for (var i=0; i<records.length; i++) {
      idsForDeletion.push(records[i].Id);
  }

  sforce.connection.deleteIds(idsForDeletion);
  window.location.reload();

 
This was selected as the best answer