+ Start a Discussion
azar khasimazar khasim 

Need to add a Custom button on Lead List Page?

hello everyone,

Need to add a Custom button on Lead List Page?

 Controller Class :
 
/*
Developer : Azar khasim 
Created Date : 03 MAR 2020
Description : Global Duplicate Search. 
*/

public class GlobalDupSearch1 {
 // PE is for searching for duplicates//   
    public string PE{get;set;}
    public string a;
    
 // These booleans are for capturing the filtered records//
    public boolean cn1{get;set;}
    public boolean ac1{get;set;}
    public boolean ld1{get;set;}
    public boolean ld2{get;set;}
    public boolean opp1{get;set;}
    
 // For Capturing of lead status.   
    public boolean Editlead{get;set;}
    public boolean Createlead{get;set;}
    
 // These lists are to capture the filtered records and stored in the list//
    public list<Account> aclist{get;set;}
    public list<contact> cnlist{get;set;}
    public list<lead> ldlist{get;set;}
    public list<lead> ldlist1{get;set;}
    public list<opportunity> opplist{get;set;}
    
    
    public GlobalDupSearch1(apexpages.StandardController sc){
      a=apexpages.currentPage().getparameters().get('id');  
     //   PE= 'search here...'; 
     
    }
    
    public void search(){
        if(pe != null && pe != ''){
            
            aclist=[select id, name, PersonMobilePhone, PersonEmail, Phone, Owner.name, Owner.UserRoleId, Owner.MobilePhone, Owner.Email, OwnerId from account where PersonMobilePhone like :PE or PersonEmail like :PE]; 
            if(aclist.size() > 0){
                ac1=true;
             }
            else{ 
                ac1=false;
            }
            opplist=[SELECT name, Mobile__c, Email__c, StageName, Owner.name, Owner.UserRoleId, Owner.MobilePhone, Owner.Email, OwnerId from opportunity where Email__c like :PE or Mobile__c like :PE];
            if(opplist.size()>0){
                opp1=true;
             }
            else{
                opp1=false;
            }
            cnlist=[SELECT name, MobilePhone, Email, Owner.name, Owner.UserRoleId, Owner.MobilePhone, Owner.Email, OwnerId from contact where MobilePhone like :pe or Email like :PE]; 
            if(cnlist.size() > 0){
                cn1=true;
             }
            else{
                cn1=false;
            }
            ldlist=[SELECT id,name, MobilePhone, Email, Status, User_Mobile__c, Lead_Owner_Role__c, DUP_Mobile__c, Owner.name, Owner.UserRoleId, Owner.Email, OwnerId, DUP_Update_Owner__c from lead where (Status!= 'Converted' AND Status!= 'Prospect' ) AND (MobilePhone like : pe or Email like :pe or DUP_Mobile__c like : pe)];
            if(ldlist.size() > 0){
                ld1=true;
            }
            else{
                ld1=false;
            }            
            
            ldlist1=[SELECT id,name, MobilePhone, Email, Status, User_Mobile__c, Lead_Owner_Role__c, DUP_Mobile__c, Owner.name, Owner.UserRoleId, Owner.Email, OwnerId, DUP_Update_Owner__c from lead where (Status!= 'New' AND Status!= 'Assigned' AND Status!= 'Followup' AND Status!= 'Junk' ) AND (MobilePhone like : pe or Email like :pe or DUP_Mobile__c like : pe)];
            if(ldlist1.size() > 0){
                ld2=true;
            }
            else{
                ld2=false;
            }          
            
            if(ac1 == false && cn1 == false && ld1 == false && ld2 == false && opp1 == false){
             // To show create lead Button.
                Createlead=true;
                Editlead=false;
                apexpages.Message m=new apexpages.message(ApexPages.Severity.ERROR,'No records found!');
                apexpages.addMessage(m);
            }else{
                Createlead=false;
            }
            
        }else{
            
            Createlead=false;
            Editlead=false;
            apexpages.Message m1=new apexpages.message(ApexPages.Severity.ERROR,'Please enter a Mobile number or Email ID to search duplicates');
            apexpages.addMessage(m1);
            
        }
        
  /// This if action is to hide the Save Button when below creteria meets ///    
  
        if(ld1 == true){
        Editlead=true;
    }  
        
    /*    for(lead lds:ldlist){
             if((lds.Status!= 'converted' || lds.Status!= 'prospect')&&(lds.Lead_Owner_Role__c!= 'Internal Sales Agent' || lds.Lead_Owner_Role__c!= 'Internal Sales Team Lead' || lds.Lead_Owner_Role__c!= 'Internal Sales Team Director')){
                Editlead=true;
                 
             }
        }  */
       
        
    }
    
    public pagereference save(){
        
        //// Need to update OwnerId field of particular record in lead object////
  		list<lead> updateLead = new list<lead>();
        lead ldown = new lead();
        integer NoOfCheckes = 0;
        for(lead ld:ldlist){
            if(ld.DUP_Update_Owner__c == true){
               NoOfCheckes++;
               UpdateLead.add(ld);
            }
        }
        if(UpdateLead.size()==1&& Noofcheckes == 1){
        	    UpdateLead[0].DUP_Update_Owner__c = false;
            	Update UpdateLead[0];
        }else{
            apexpages.Message m2=new apexpages.message(ApexPages.Severity.ERROR,'Please Select one Lead at a time only');
            apexpages.addMessage(m2);
        }
        
        pagereference p1=new pagereference('/'+(UpdateLead.size()>0?UpdateLead[0].id:null));
        p1.setRedirect(true);
        return p1;
        
    }
    
    
    public pagereference createLead(){
       
        pagereference cl1=new pagereference('/lightning/o/Lead/new');
        cl1.setRedirect(true);
        return cl1;
    }
    
}

Visualforce :
 
<!--
Developer : Azar khasim 
Created Date : 03 MAR 2020
Description : Global Duplicate Search. 
-->

<apex:page StandardController="Lead" extensions="GlobalDupSearch1">
    
    <apex:form >
        <apex:pageBlock id="block">
            <apex:pageMessages ></apex:pageMessages>
            <style>
                .custom1 .pbSubheader {
                background-color: #2E9AFE !important;
                }
            </style> 
            
            <center>
                <apex:pageBlockButtons location="bottom" > 
                    <apex:commandButton style="color:black;background-image:none;width:50px;height:30px"  action="{!save}" value="Save" rendered="{!Editlead=true}"/>
                </apex:pageBlockButtons>
            </center> 
            
            
            <!---------- Duplicate Search ---------------------------------------------------------------->            
            <apex:outputPanel layout="block" styleClass="custom1">
                <apex:pageBlockSection title="Find Dulicates" collapsible="false" >               
                    <center>
                        <apex:inputText value="{!pe}" id="search" html-placeholder="Search here"/><br/>
                        <apex:commandButton value="Search" action="{!search}" rerender="block"/>
                    </center>             
                    <center>
                        <apex:commandButton style="width:75px;height:25px"  action="{!createLead}" value="Create Lead" rendered="{!Createlead=true}"/>
                    </center>
                </apex:pageBlockSection>
            </apex:outputPanel>
            
            
            <!------------------------------------------------------------ Lead Data(No Converted and Prospect Data)  to Display ------------------------------------------------------------------------>   
       
            <apex:outputPanel layout="block" styleClass="custom1">
                <apex:pageBlockSection title="Lead" rendered="{!ld1 != false}" >
                    
                    <!-- Regarding the Boolean Value in Lead Object ---------------------------------------->       
                    <apex:pageBlockTable value="{!ldlist}" var="Leaddata" cellspacing="10">
                        
                        <apex:column style="width:100px" value="{!Leaddata.Name}" headerValue="Lead Name"/>
                        <apex:column style="width:100px" value="{!Leaddata.Status}" headerValue="Lead Status"/>
                        <apex:column style="width:100px" value="{!Leaddata.Owner.Name}" headerValue="Owner Name"/>
                        <apex:column style="width:100px" value="{!Leaddata.Lead_Owner_Role__c}" headerValue="Owner Role"/>
                        <apex:column style="width:100px" value="{!Leaddata.User_Mobile__c}" headerValue="Owner Mobile"/>
                        <apex:column style="width:100px" value="{!Leaddata.Owner.Email}" headerValue="Owner Email"/>
                        <!----- OwnerId Field in Lead Object ----------------------------------------------->      
                       
                        <apex:column >
                            <apex:facet name="header">New Lead Owner</apex:facet>
                              <apex:inputField value="{!Leaddata.ownerId}" required="false" rendered="{!IF((Leaddata.Lead_Owner_Role__c ='Internal Sales Agent'||Leaddata.Lead_Owner_Role__c ='Internal Sales Team Lead'||Leaddata.Lead_Owner_Role__c ='Internal Sales Team Director'),false,true)}"/>
                        </apex:column> 
                        <apex:column headerValue="Change Ownership" >
                            <apex:inputCheckbox value="{!Leaddata.DUP_Update_Owner__c}"  label="Change Ownership" rendered="{!If((Leaddata.Lead_Owner_Role__c ='Internal Sales Agent'||Leaddata.Lead_Owner_Role__c ='Internal Sales Team Lead'||Leaddata.Lead_Owner_Role__c ='Internal Sales Team Director'),false,true)}"/> 
                        </apex:column> 
                        
                    </apex:pageBlockTable>
                    
                </apex:pageBlockSection>
            
            </apex:outputPanel> 
          
            
            <!------------------------------------------------------------ Lead 2 Data(No New, Assigned, Followup and Junk Data)  to Display ------------------------------------------------------------------------>   
       
            <apex:outputPanel layout="block" styleClass="custom1">
                <apex:pageBlockSection title="SalesOwner leads" rendered="{!ld2 != false}" >
                    
                    <!-- Regarding the Boolean Value in Lead Object ---------------------------------------->       
                    <apex:pageBlockTable value="{!ldlist1}" var="lds" cellspacing="10">
                        
                        <apex:column style="width:100px" value="{!lds.Name}" headerValue="Lead Name"/>
                        <apex:column style="width:100px" value="{!lds.Status}" headerValue="Lead Status"/>
                        <apex:column style="width:100px" value="{!lds.Owner.Name}" headerValue="Owner Name"/>
                        <apex:column style="width:100px" value="{!lds.Lead_Owner_Role__c}" headerValue="Owner Role"/>
                        <apex:column style="width:100px" value="{!lds.User_Mobile__c}" headerValue="Owner Mobile"/>
                        <apex:column style="width:100px" value="{!lds.Owner.Email}" headerValue="Owner Email"/>
                        <!----- OwnerId Field in Lead Object ----------------------------------------------->      
                       
                    </apex:pageBlockTable>
                    
                </apex:pageBlockSection>
            
            </apex:outputPanel> 
          
            <!------------------------------------------------------- Opportunity Data  to Display ---------------------------------------------------------------------------->             
       
            <apex:outputPanel layout="block" styleClass="custom1">
                <apex:pageBlockSection title="Opportunity" rendered="{!opp1 != false}">
                    
                    <!--- Values to display in Opportunity Object---------------------------------------->
                    <apex:pageBlockTable style="width:150%" value="{!opplist}" var="opp"  cellspacing="10">

                        <apex:column style="width:100px" value="{!opp.name}" />  
                        <apex:column style="width:100px" value="{!opp.StageName}" headerValue="Opportunity Stage"/>
                        <apex:column style="width:100px" value="{!opp.owner.Name}" headerValue="Owner"/>  
                        <apex:column style="width:100px" value="{!opp.owner.UserRoleId}" headerValue="Owner Role"/>
                        <apex:column style="width:100px" value="{!opp.owner.MobilePhone}" headerValue="Owner Mobile"/>
                        <apex:column style="width:100px" value="{!opp.owner.Email}" headerValue="Owner Email"/>   
                        <!----- OwnerId Field in Lead Object ----------------------------------------------->      
                        
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:outputPanel>

            <!---------------------------------------------------------- Account Data  to Display ---------------------------------------------------------------------->             
    <!--     
            <apex:outputPanel layout="block" styleClass="custom1">
                <apex:pageBlockSection title="Account" rendered="{!ac1 != false}">  -->
 
                    <!--- Values to display in Account Object---------------------------------------->
               <!--     <apex:pageBlockTable style="width:150%" value="{!aclist}" var="ac"  cellspacing="10">
 
                        <apex:column style="width:100px" value="{!ac.Name}" />  
                        <apex:column style="width:100px" value="{!ac.owner.Name}" headerValue="Owner"/>
                        <apex:column style="width:100px" value="{!ac.owner.UserRoleId}" headerValue="Owner Role" />
                        <apex:column style="width:100px" value="{!ac.owner.MobilePhone}" headerValue="Owner Mobile"/>
                        <apex:column style="width:100px" value="{!ac.owner.Email}" headerValue="Owner Email"/>   
                        
                         
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:outputPanel>  -->
            
            <!------------------------------------------------------------- Contact Data  to Display --------------------------------------------------------------------->             
         
            <apex:outputPanel layout="block" styleClass="custom1">
                <apex:pageBlockSection title="Account / Contact" rendered="{!cn1 != false}">

                    <!---  Values to display in Contact Object-------------------------------------------->
                    <apex:pageBlockTable style="width:150%" value="{!cnlist}" var="con"  cellspacing="10">

                        
                        <apex:column style="width:100px" value="{!con.Name}" />  
                        <apex:column style="width:100px" value="{!con.owner.Name}" headerValue="Owner"/>  
                        <apex:column style="width:100px" value="{!con.owner.UserRoleId}" headerValue="Owner Role"/>
                        <apex:column style="width:100px" value="{!con.owner.MobilePhone}" headerValue="Owner Mobile"/>
                        <apex:column style="width:100px" value="{!con.owner.Email}" headerValue="Owner Email"/> 
                        
                         
                    </apex:pageBlockTable>
                </apex:pageBlockSection>
            </apex:outputPanel> 
            
            <!-------- End of logics ------------------------------------------------------------------------------->     
        </apex:pageBlock>
    </apex:form>
</apex:page>

Now i need to create a List Button On Lead List Page.
to access this Vf page.

Please provide me a solution.

Thanks and Regards,
Azar Khasim.
Best Answer chosen by azar khasim
Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi Azar,
Inorder to add your vf page as list button,the page should have standard list controllers in your page .You have to add recordSetVar attribute in your <apex:page>

Please refer below links which might help you in this
https://salesforce.stackexchange.com/questions/66732/list-button-doesnt-see-visualforce-page
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_sosc_associate.htm
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_sosc_custom_button.htm

Hope this helps you
Let me know if this helps you. Kindly mark it as solved so that it may help others in future.

Thanks and Regards