I am trying to create a custom 'take ownership' for Case list view in Lightning.

I took the classic Javascript button (from @sfdcfox answer to this question - Custom button on list view), and included that in a VisualForce Page as follows:

<apex:page standardController="Case" recordSetVar="Case">
<apex:includeScript value="/soap/ajax/31.0/connection.js"/>
<apex:includeScript value="/soap/ajax/29.0/apex.js"/>
<apex:includeScript value="/support/console/26.0/integration.js"/>
<apex:includeScript value="/support/console/26.0/integration.js"/>
    var caseIds = {!GETRECORDIDS($ObjectType.Case)};
    var cases = [], tempCase, result;
    while(caseIds.length) {
    tempCase = new sforce.SObject('Case');
    tempCase.Id = caseIds.shift();
    tempCase.OwnerId = '{!$User.Id}';
    result = sforce.connection.update(cases);
    window.top.location.href = window.top.location.href;

After selecting few cases from List view, and using the custom button, there's an exception - 'getRecordIds is not defined' :

enter image description here

  • Mentioning a user in a question doesn't notify a user, but I felt my metaphorical ears burning. Go ahead and give it a try.
    – sfdcfox
    Commented Jul 29, 2017 at 0:16
  • Thank you sfdcfox, I knew this will catch you attention and highly professional hands :) I just need to struggle the test class now, to complete this.
    – iDog
    Commented Jul 29, 2017 at 7:47

1 Answer 1


JavaScript buttons don't work the way you expect, and I'm pretty sure that the code you're trying to use won't work. That said, you have an alternative you can use (tested/verified).

First, create a new Apex Class:

public class CaseAcceptOwnership {
    PageReference cancel;
    Case[] records;
    public CaseAcceptOwnership(ApexPages.StandardSetController c) {
        records = (Case[])c.getSelected();
        cancel = c.cancel();
    public PageReference updateCases() {
        for(Case record: records) {
            record.OwnerId = UserInfo.getUserId();
        update records;
        return cancel.setRedirect(true);

Next, create a Visualforce page:

<apex:page standardController="Case" recordSetVar="cases" extensions="CaseAcceptOwnership" action="{!updateCases}">

After that, go to Setup > Customize > Cases > Buttons, Links, and Actions, and create a new List Button (with select checkboxes), choose Visualforce for the Content Source, and choose the new page you created.

Finally, go ahead and add this button to your Search Layout, and you're done. The user can select records, and when they click the button, they will own the selected cases. You may want to add some error handling or something, but this should get you started.

Edit: Example Unit Test

Again, does not cover failure conditions, so make sure you add additional error handling if there's a possibility of failure.

@isTest class CaseAcceptOwnershipTest {
    @isTest public static void test() {
        // Get All Fields
        String[] fields = new List<String>(sObjectType.User.fields.getMap().keySet());
        // Get Test User Data
        Id userId = UserInfo.getUserId();
        User tempUser = Database.query('select '+String.join(fields,',')+' from user where Id = :userId');
        // Clone and clear out problematic fields
        tempUser = tempUser.clone(false, false, false);
        tempUser.FederationIdentifier = null;
        tempUser.Alias = 'testUser';
        tempUser.CommunityNickname = 'testUser';
        tempUser.UserName = 'testUser'+Math.random()+'@not-a-real-domain.or-tld';
        // This automatically does a DML, saves us one. Also avoids MIXED_DML errors.
        System.runAs(tempUser) {
            Account accountRecord = new Account(Name='Test');
            Contact contactRecord = new Contact(LastName='Test');
            Case caseRecord = new Case(Subject='Test',Status='Open', OwnerId=tempUser.Id);
            // Multi-Object insert for performance
            insert new sObject[] { accountRecord, contactRecord, caseRecord };
            contactRecord.AccountId = accountRecord.Id;
            caseRecord.ContactId = contactRecord.Id;
            caseRecord.AccountId = accountRecord.Id;
            // Assocate records together
            update new sObject[] { contactRecord, caseRecord };
        // Instantiate controller as it would be from the Visualforce page
        ApexPages.StandardSetController standardSetController = new ApexPages.StandardSetController([
            select id from case
        // And the extension controller
        CaseAcceptOwnership controller = new CaseAcceptOwnership(standardSetController);
        // Avoid MIXED_DML error, run as yourself
        System.runAs(new User(Id=UserInfo.getUserId())) {
            ApexPages.PageReference result = controller.updateCases();
        // Verify ownership change
        System.assertEquals(1, [SELECT COUNT() FROM Case WHERE OwnerId = :UserInfo.getUserId()]);
  • @ultima67 Yes, I was mobile when I got your comment, but I added an example unit test to this answer.
    – sfdcfox
    Commented Jan 5, 2021 at 1:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .