[SalesForce] problem adding custom button to list view

I want to add a custom button to the list view of Contacts but it is not working. I go to Object Manager… Contacts… Search Layouts… List View… Edit… Custom Buttons… add my button to the list of Selected Buttons. When I go back to Contacts, it does not appear. If I remove Standard Buttons by unchecking them, nothing changes. Any direction would be appreciated.

I'm using Lightning. enter image description here

enter image description here
enter image description here

public class createNewContactController_ext {
//variables for the student
private final Contact con;
private ApexPages.StandardController stdController;
String StudentRecordType = '0121H000001If0DQAS';


//variables for dependent spouse
public List<Contact> spsList {get;set;} //using list so that I don't have to declare each variable
public Integer rowNumsps{get;set;}
String DepedendentRecordType = '0121H000001If0IQAS';


//variables for dependent children
public List<Contact> depList {get;set;}
public Integer rowNum{get;set;}

//initialize 
public createNewContactController_ext(ApexPages.StandardController stdController) {
    this.con = (Contact)stdController.getRecord();
    depList = new List<Contact>();  
    spsList = new List<Contact>();
}
    //for inserting additional rows in dependent spouse table
public void insertRowsps(){
    spsList.add(new Contact(recordTypeID=DepedendentRecordType)); 
}

    //for deleting rows from dependent spouse table
public void delRowsps(){
    rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('indexsps'));
    depList.remove(rowNumsps);   
}

//for inserting additional rows in dependent children table
public void insertRow(){
    depList.add(new Contact(recordTypeID=DepedendentRecordType)); 
}


//for deleting rows from dependent children table
public void delRow(){
    rowNum = Integer.valueOf(apexpages.currentpage().getparameters().get('index'));
    depList.remove(rowNum);   
}

//save the records
public PageReference save(){

    //save the student
    insert con;

    //save the spouse
    insert spsList;
    for (Contact sps : spsList){
        hed__Relationship__c spsrel = new   hed__Relationship__c(hed__Contact__c=con.Id, hed__RelatedContact__c=sps.Id, hed__Type__c='Spouse');
      insert spsrel;
    }

    //save the child dependents
    insert depList;
    for (Contact dep : depList){
      hed__Relationship__c rel = new    hed__Relationship__c(hed__Contact__c=con.Id, hed__RelatedContact__c=dep.Id, hed__Type__c='Child');
      insert rel;
    }    

    //redirect to the detail page of the student
    PageReference redirectPage = new ApexPages.StandardController(con).view();
    redirectPage.setRedirect(true);
    redirectPage.getParameters().put('id',con.Id);
    return redirectPage;
    }

}

the vf page:

    <apex:page standardController="Contact" extensions="createNewContactController_ext" docType="html-5.0" >
<apex:pageBlock title="Student">
    <apex:form >
        <apex:pageBlockSection >       
            <apex:inputField value="{!Contact.FirstName}" />
            <apex:inputField value="{!Contact.LastName}" />
            <apex:inputField value="{!Contact.Email}" required="true" />
            <apex:inputField value="{!Contact.Birthdate}" showDatePicker="true"/>
            <apex:inputField value="{!Contact.Affiliated_Community__c}" />
            <apex:inputField value="{!Contact.Beneficiary_Number__c}" />
            <apex:inputField value="{!Contact.Birth_Place__c}" />
            <apex:inputField value="{!Contact.Social_Insurance_Number__c}" />
            <apex:inputField value="{!Contact.hed__Gender__c}" />
            <apex:inputField value="{!Contact.Counselor__c}" />
            <apex:inputField value="{!Contact.Permanent_Code__c}" />
            <apex:inputField value="{!Contact.Facebook_Profile__c}" />
            <apex:inputField value="{!Contact.Language_Preference__c}" />
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Student Permanent Address">
            <apex:inputField value="{!Contact.MailingStreet}" />
            <apex:inputField value="{!Contact.MailingCity}" />
            <apex:inputField value="{!Contact.MailingState}" /> 
            <apex:inputField value="{!Contact.MailingPostalCode}" label="Province" />
            <apex:inputField value="{!Contact.MailingCity}" />
            <apex:inputField value="{!Contact.MailingState}" />        
        </apex:pageBlockSection> 
        <apex:pageBlock title="Dependent Spouse" >
            <apex:variable var="rowNumsps" value="{!0}"  />  
            <apex:pageBlockTable value="{!spsList}" var="spstable">
                <apex:facet name="footer">
                    <apex:commandLink value="Add" action="{!insertRowsps}"/>
                </apex:facet>
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!spstable.FirstName}"/>
                </apex:column>
                <apex:column headerValue="Last Name">
                    <apex:inputText value="{!spstable.LastName}"/>
                </apex:column>   
                <apex:column headerValue="Birthdate">
                    <apex:inputField value="{!spstable.Birthdate}"/>
                </apex:column>   
                <apex:column headerValue="Beneficiary Number">
                    <apex:inputField value="{!spstable.Beneficiary_Number__c}"/>
                </apex:column>   
                <apex:column headerValue="Social Insurance Number">
                    <apex:inputField value="{!spstable.Social_Insurance_Number__c}"/>
                </apex:column>
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}">
                        <apex:param value="{!rowNumsps}" name="indexsps" />
                    </apex:commandLink>
                    <apex:variable var="rowNumsps" value="{!rowNumsps+1}"/>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:variable var="rowNum" value="{!0}"  />
        <apex:pageBlock title="Dependent Children" >
            <apex:variable var="rowNum" value="{!0}"  />  
            <apex:pageBlockTable value="{!depList}" var="deptable">
                <apex:facet name="footer">
                    <apex:commandLink value="Add" action="{!insertRow}"/>
                </apex:facet>
                <apex:column headerValue="First Name">
                    <apex:inputField value="{!deptable.FirstName}"/>
                </apex:column>
                <apex:column headerValue="Last Name">
                    <apex:inputText value="{!deptable.LastName}"/>
                </apex:column>   
                <apex:column headerValue="Birthdate">
                    <apex:inputField value="{!deptable.Birthdate}"/>
                </apex:column>   
                <apex:column headerValue="Beneficiary Number">
                    <apex:inputField value="{!deptable.Beneficiary_Number__c}"/>
                </apex:column>   
                <apex:column headerValue="Social Insurance Number">
                    <apex:inputField value="{!deptable.Social_Insurance_Number__c}"/>
                </apex:column>         
                <apex:column headerValue="Delete" >
                    <apex:commandLink style="font-size:15px; font-weight:bold; text-align:center;color:red;" value="X" action="{!delRow}">
                        <apex:param value="{!rowNum}" name="index" />
                    </apex:commandLink>
                    <apex:variable var="rowNum" value="{!rowNum+1}"/>
                </apex:column>          
            </apex:pageBlockTable>
        </apex:pageBlock>
        <apex:commandButton value="save" action="{!save}" />  
    </apex:form>    
</apex:pageBlock>
</apex:page>

Best Answer

You have to use recordSetVar to add a button to List view. Adding recordSetVar makes it a StandardSetController and thus makes it available in that picklist to select.

<apex:page standardController="Contact" extensions="createNewContactController_ext" docType="html-5.0" recordSetVar="contacts">

Also, The list view VF page button won't be visible in "Recently Viewed" List view, you have to select any other listview to see that button.

EDIT:

Your apex Extension should be StandardSetController extension as well. StandardSetControllerExtension works on Set of records.

public class createNewContactController_ext {

    public createNewContactController_ext(ApexPages.StandardSetController controller){
    // constructor logic
    }
}

NOTE: StandardSetController allows you to use the page in a List Button, whereas using a StandardController allows you to use it in a Detail Button.

Once you save your VF Page and Apex Class, It will be visible for you to add to Listview.

Src : https://salesforce.stackexchange.com/a/144831