Storing Record IDs from a List View in Salesforce

Storing Record IDs from a List View in Salesforce

Built into Salesforce is the ability to add buttons onto List Views. This is an extremely helpful and powerful solution for updating records without needing to navigate onto the record detail page. In this blog post, I aim to cover how to implement selecting records to update, along with handling multiple record selections.

Part 1: Storing and Passing IDs

Salesforce provides built in syntax to capture the user selected record. In the button logic you will need to provide this piece of code:

var oId= {!GETRECORDIDS($ObjectType.Object)};

This stores the user selected record Id into a variable which then can be manipulated. The user action would look like the following:

multiple user selection

Once stored, the variable is passed to the class and web service, wrapped in a javascript function, as seen below:

function gogo(){

var result = sforce.apex.execute(“ClassName”, “Web Service”, {oId:oId}, {onSuccess: function (resultMessage) {alert(‘Success’);window.close();},onFailure: function (error) {alert(“ERROR: ” + error)}});

}

Part 2: Utilizing Passed IDs

When passing the Object Id, you will need to structure your web service to handle multiple records, or else you will receive a DML error. To do so, you will need to first query your object using the Id you passed from Part 1 into a Salesforce.com List.

List <Object> example = [Select Id from Object where Id IN: oId];

This line of code will store in a list all records that were selected by the user from Part 1.

To process your web service logic, encompass the logic in a for loop.

for(Object example1 : example){

//Logic ………….

}

If you are interested in Salesforce or need help, we would love to talk with you about your needs and challenges, contact us today.

Leave a Reply

Your email address will not be published. Required fields are marked *