Blog

You may be faced with the situation where you need to be able to delete multiple records from a related list in one click. Standard related lists do not have a feature to select all and delete all selected records. The standard way to delete from a related list is to use the del link and delete each record individually. This can be very time consuming and tiresome if there are many records to delete.

Salesforce Multiple Products

Salesforce Multiple Products

Imagine you have the list above and need to remove all products. Rather than clicking delete for each product, there must be an easier way! Actually there is, and it is in the form of list buttons.

When you define a custom button, you can define it as a Detail page button or List button. As you can see below, defining it as a list button allows you to select the ‘Display Checkboxes’ option for multirecord selection. This is all standard Salesforce so just defining a list button with this option will get you a long way to a solution for selecting multiple records and deleting them.

Salesforce List Button

Salesforce List Button

After defining a list button, we must define the action for the button. This is where we need to use some JavaScript. You don’t need to be a JavaScript expert and it is quite simple. In fact to get it working only requires 4 lines!

Salesforce JavaScript for Multiple Record Delete

Going through the lines, one by one:

{!REQUIRESCRIPT(‘/soap/ajax/29.0/connection.js’)}
-> This makes an AJAX toolkit library available and is required to be able to make API and AJAX toolkit methods through JavaScript

var selectedRecords = {!GETRECORDIDS( $ObjectType.OpportunityLineItem )};
-> This defines a variable ‘selectedRecords’ that will contain the record ids of all the selected records. If you want to delete another type of record instead of Opportunity Products, you would replace the merge field $ObjectType.OpportunityLineItem with the reference to the other type of record, e.g. $ObjectType.QuoteLineItem

sforce.connection.deleteIds(selectedRecords);
-> This line actually deletes the selected records

navigateToUrl(window.location.href);
-> This line refreshes the page. Without this, the records would be deleted, but would still appear.

This is the basic solution to give you an understanding of how it works. From here we could enhance it to add:
– Display a message if no records have been selected
– Check how many records were successfully deleted and how many failed
– Display the errors in a message

To check on the results of the operation, we would need to change the line:
sforce.connection.deleteIds(selectedRecords); to
deletionResult = sforce.connection.deleteIds(selectedRecords);

We could then access the success or failure of each record, using the deletionResult array:
e.g.
– deletionResult[RecordNumber].getBoolean(‘success’) would indicate a successful deletion.
– deletionResult[RecordNumber].errors.message would give us an error message for the failed records.

With our button added to the Opportunity Page Layout, we would be able to select multiple records and delete in one click!

Salesforce Multiple Delete Checkbox

Salesforce Multiple Delete Checkbox

What Certification are you studying for now?

Focus on Force currently provides practice exams and study guides for sixteen certifications

How to Delete Multiple Records from a Salesforce Related List

You may be faced with the situation where you need to be able to delete multiple records from a related list in one click. Standard related lists do not have a feature to select all and delete all selected records. The standard way to delete from a related list is to use the del link and delete each record individually. This can be very time consuming and tiresome if there… Read More

How to Delete Multiple Records from a Salesforce Related List

Salesforce Cross Filter Report Examples

You may have come across cross filters when learning about Salesforce reports but having no immediate need for them, forget about them. It is only sometime later when faced with a reporting challenge that you may delve back and find that they are very useful and may even solve a problem that you thought you could not do with Salesforce reports. Lets review what reports with cross filters can do:… Read More

Salesforce Cross Filter Report Examples

Salesforce Email Templates

When you send an email from Salesforce, such as when you use the Send Email button or triggered from a workflow, you can select an email template. Email Templates in Salesforce come in four different types: Plain Text HTML using Letterhead HTML Custom Visualforce Plain Text Email Template This is the simplest email template to create. You can create it very quickly by entering the email text and including the… Read More

Salesforce Email Templates

Salesforce Lead Conversion

I was looking for a process flow diagram, that describes what happens when a lead is converted in Salesforce. I couldn’t find one, so I made my own. Once a lead is qualified, the next step is to convert the lead. In Salesforce, when you convert a lead, there are a number of options that are part of the process flow. The first step is to press the convert button…. Read More

Salesforce Lead Conversion

How to Display a Traffic Light Indicator in Salesforce

Visual indicators are a great addition to a Salesforce page to highlight important information or values. Instead of just displaying a value, showing an image will make a much larger impact. Depending on the situation, it may be more suitable to just display a single indicator such as a caution or stop image, or if a variable can have a range of values, display a variable image according to which… Read More

How to Display a Traffic Light Indicator in Salesforce

Salesforce Platform Overview

There are a number of features and functionality that are part of the Salesforce platform and are available to all Salesforce solutions. They offer the technology or functionality that can be used either standalone (e.g. content management) or used to build upon (e.g. force.com code). As they are common across the different Salesforce applications (e.g. Sales Cloud, Service Cloud and custom applications) they are part of the Salesforce platform. The… Read More

Salesforce Platform Overview

Salesforce Activities Tab and List View

  You may wonder where the Activities tab is in Salesforce, so you can see a list view of activities. You can see the task list and calendar on the home page but what if you would like to see all of your tasks and events in a list. Well there is a way to get to a list view, but it is easy to overlook. In the calendar section… Read More

Salesforce Activities Tab and List View

Salesforce and Email – What are the options?

When it comes to Salesforce and email, it can get confusing to understand what the options are. Read on to discover 10 ways you can work with email and Salesforce together. Sending Email from Salesforce Some people may wonder whether integration to an email client such as Outlook is even necessary. If Salesforce could send and automatically process incoming emails, then couldn’t users just live in Salesforce and use it… Read More

Salesforce and Email – What are the options?

Salesforce URL Hacking

You may find yourself with the requirement to create a record from a related list and populate a number of fields. By default, Salesforce will populate the field on the related record that links the two objects, but no other fields. For example, if you create a record for a custom object related to an opportunity, the custom record will have the opportunity populated as it relates the two records,… Read More

Salesforce URL Hacking

Salesforce Joined Reports

It can be tricky to understand how Joined reports work in Salesforce. For those who have experience with database queries or other report writing tools, the name Joined report can raise expectations that it will be possible to report on data joined from different objects in the one report. A way to think of joined reports is of separate block reports where the data can be optionally grouped to ‘join’… Read More

Salesforce Joined Reports