function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KULWANT SINGHKULWANT SINGH 

Hi, I am new to code. Here i am trying to create a VF page to show all duplicate accounts based on lead name on lead standard page...code is throwing an error says "Invalid conversion from runtime type Lead to Account" for the below code.

public with sharing class MtachedAccountV1Controller {


    public lead LD {get;set;}
    public account acc {get;set;}
    public list<account> alist {get;set;}
    public String currentRecordId {get;set;}
    public list<account> dupelist {get;set;}
    public ApexPages.StandardController controller {get;set;}
    
    
    
    public MtachedAccountV1Controller(ApexPages.StandardController controller) {
    
  try
  {
      ld= new lead();
      dupelist = new list<account> ();
      this.controller = controller;
      this.acc = (Account)controller.getRecord();
      currentRecordId = controller.getId();
     
                                                  System.debug('id is..'+currentRecordId );
     alist = [select id,name from account where name =: ld.name];
          
   }
              Catch (exception e)
                     {
                                SYstem.debug(e);
                     }
 }

}
Best Answer chosen by KULWANT SINGH
Raj VakatiRaj Vakati
Working copy 
 
<apex:page standardController="lead" extensions="MtachedAccountV1Controller">
    
    
    <apex:form >
        
        
        <apex:pageBlock title="DuplicateAccounts" >
            <apex:pageBlockSection collapsible="false" >
                <apex:pageBlockTable value="{!alist}" var="al">       
                    <apex:column headerValue="Account Name" value="{!al.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
    
    
</apex:page>
 
public with sharing class MtachedAccountV1Controller {
    
    
    public lead LD {get;set;}
    public account acc {get;set;}
    public list<account> alist {get;set;}
    public String currentRecordId {get;set;}
    public list<account> dupelist {get;set;}
    public ApexPages.StandardController controller {get;set;}
    
    
    
    public MtachedAccountV1Controller(ApexPages.StandardController controller) {
        
        try
        {
            dupelist = new list<account> ();
            // this.LD = (Lead)controller.getRecord();
            currentRecordId = controller.getId();
            LD = [Select Id ,Name from Lead where Id =:currentRecordId];
            System.debug('id is..'+currentRecordId );
            alist = [select id,name from account where name =: LD.Name];
            
        }
        Catch (exception e)
        {
            SYstem.debug(e);
        }
    }
    
}

 

All Answers

Raj VakatiRaj Vakati
Give me you VF page also .. i will rewrite the controller 

 
public with sharing class MtachedAccountV1Controller {


    public lead LD {get;set;}
    public account acc {get;set;}
    public list<account> alist {get;set;}
    public String currentRecordId {get;set;}
    public list<account> dupelist {get;set;}
    public ApexPages.StandardController controller {get;set;}
    
    
    
    public MtachedAccountV1Controller(ApexPages.StandardController controller) {
    
  try
  {
      LD= new lead();
      dupelist = new list<account> ();
      this.controller = controller;
      this.LD = (Lead)controller.getRecord();
      currentRecordId = controller.getId();
     
                                                  System.debug('id is..'+currentRecordId );
     alist = [select id,name from account where name =: LD.name];
          
   }
              Catch (exception e)
                     {
                                SYstem.debug(e);
                     }
 }

}

 
KULWANT SINGHKULWANT SINGH
Here is the VF code :

<apex:page standardController="lead" extensions="MtachedAccountV1Controller">
 
 
  <apex:form >
 
 
    <apex:pageBlock title="DuplicateAccounts" >
        <apex:pageBlockSection collapsible="false" >
        <apex:pageBlockTable value="{!alist}" var="al">       
                  <apex:column headerValue="Account Name" value="{!al.name}"/>
        </apex:pageBlockTable>
        </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
 
 
</apex:page>
Raj VakatiRaj Vakati
This code works
 
public with sharing class MtachedAccountV1Controller {


    public lead LD {get;set;}
    public account acc {get;set;}
    public list<account> alist {get;set;}
    public String currentRecordId {get;set;}
    public list<account> dupelist {get;set;}
    public ApexPages.StandardController controller {get;set;}
    
    
    
    public MtachedAccountV1Controller(ApexPages.StandardController controller) {
    
  try
  {
      LD= new lead();
      dupelist = new list<account> ();
      this.controller = controller;
      this.LD = (Lead)controller.getRecord();
      currentRecordId = controller.getId();
     
                                                  System.debug('id is..'+currentRecordId );
     alist = [select id,name from account where name =: LD.name];
          
   }
              Catch (exception e)
                     {
                                SYstem.debug(e);
                     }
 }

}
 
<apex:page standardController="lead" extensions="MtachedAccountV1Controller">
 
 
  <apex:form >
 
 
    <apex:pageBlock title="DuplicateAccounts" >
        <apex:pageBlockSection collapsible="false" >
        <apex:pageBlockTable value="{!alist}" var="al">       
                  <apex:column headerValue="Account Name" value="{!al.name}"/>
        </apex:pageBlockTable>
        </apex:pageBlockSection>

    </apex:pageBlock>
  </apex:form>
 
 
</apex:page>


 
KULWANT SINGHKULWANT SINGH
It says :  SObject row was retrieved via SOQL without querying the requested field: Lead.Name

Do i need to take ld.name as a string?
 
Raj VakatiRaj Vakati
public with sharing class MtachedAccountV1Controller {


    public lead LD {get;set;}
    public account acc {get;set;}
    public list<account> alist {get;set;}
    public String currentRecordId {get;set;}
    public list<account> dupelist {get;set;}
    public ApexPages.StandardController controller {get;set;}
    
    
    
    public MtachedAccountV1Controller(ApexPages.StandardController controller) {
    
  try
  {
      dupelist = new list<account> ();
     // this.LD = (Lead)controller.getRecord();
      currentRecordId = controller.getId();
     LD = [Select Id ,Name from Lead where Id =:currentRecordId];
                                                  System.debug('id is..'+currentRecordId );
     alist = [select id,name from account where name =: LD.Name];
          
   }
              Catch (exception e)
                     {
                                SYstem.debug(e);
                     }
 }

}

 
Raj VakatiRaj Vakati
Working copy 
 
<apex:page standardController="lead" extensions="MtachedAccountV1Controller">
    
    
    <apex:form >
        
        
        <apex:pageBlock title="DuplicateAccounts" >
            <apex:pageBlockSection collapsible="false" >
                <apex:pageBlockTable value="{!alist}" var="al">       
                    <apex:column headerValue="Account Name" value="{!al.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
    
    
</apex:page>
 
public with sharing class MtachedAccountV1Controller {
    
    
    public lead LD {get;set;}
    public account acc {get;set;}
    public list<account> alist {get;set;}
    public String currentRecordId {get;set;}
    public list<account> dupelist {get;set;}
    public ApexPages.StandardController controller {get;set;}
    
    
    
    public MtachedAccountV1Controller(ApexPages.StandardController controller) {
        
        try
        {
            dupelist = new list<account> ();
            // this.LD = (Lead)controller.getRecord();
            currentRecordId = controller.getId();
            LD = [Select Id ,Name from Lead where Id =:currentRecordId];
            System.debug('id is..'+currentRecordId );
            alist = [select id,name from account where name =: LD.Name];
            
        }
        Catch (exception e)
        {
            SYstem.debug(e);
        }
    }
    
}

 
This was selected as the best answer
KULWANT SINGHKULWANT SINGH
Thanks Raj.