+ Start a Discussion
ForceLoverForceLover 

System.DmlException:CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []

Hi Everyone,

 

I have a custom field REFID__c on lead,leadbackup__c objects. i need to search the records according to this field.Whenever REFID exists in lead object and not in leadbackup then it should create a record with same refid,fields on lead into leadbackup and display the record in edit mode.

If it exits on both objects it simply display the record in edit mode. The lead object should use only for searching and to create record on leadbackup__c.

 

I'm getting this error

 

Visualforce Error


System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, cannot reference converted lead: []

Error is in expression '{!search}' in component <apex:page> in page siteupdatinglead


Class.SiteLead.search: line 33, column 1

 

My VF CODE

 

<apex:page standardController="LeadBackup__c" sidebar="false" extensions="SiteLead">
 <head>
    <style>
        .startStyle{
            font-size:12px;
            font-weight:bold;
            color:red;
        }     
    </style>
 </head>
  <apex:sectionHeader title="Lead Form" subtitle="New Lead" />
   <apex:form id="frm">
    <div style="width:1400px; padding-left:5%; ">
     <apex:pageBlock id="Block" mode="edit">
      <apex:pageBlockButtons location="both">
        <apex:commandButton action="{!save}" value="Save Records" rerender="frm"/>
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
       <apex:pageMessages escape="false"/>
        <apex:pageBlockSection >
         <apex:pageblockSectionItem >
          <apex:outputLabel for="searchText" style="font-size:14px">RefID/FileNumber</apex:outputLabel>
          <apex:panelGroup >
          <apex:inputText id="searchText" value="{!searchText}"/>
          <apex:commandButton value="Search" action="{!search}" rerender="resultsBlock" status="status"/>
          </apex:panelGroup>
         </apex:pageblockSectionItem>
        </apex:pageBlockSection>
        
        <apex:actionStatus id="status" startText="Searching... please wait..."/>
      <apex:pageBlockSection title="Lead Results" id="resultsBlock" columns="1">
        <apex:pageBlockTable value="{!searchResults}" var="LeadBackup__c" rendered="{!NOT(ISNULL(searchResults))}">
          <apex:column headerValue="LeadName" width="100">
           <apex:inputField value="{!LeadBackup__c.Name}"/>
          </apex:column>
          <apex:column value="{!LeadBackup__c.Street__c}" headerValue="Street" width="100"/>
          <apex:column value="{!LeadBackup__c.City__c}" headerValue="City" width="100"/>
          <apex:column value="{!LeadBackup__c.State__c}" headerValue="State" width="100"/>
        
        <!--  <apex:column headerValue="First Name" width="100">
            <apex:inputField value="{!Lead.FirstName}"/>
          </apex:column>
          <apex:column headerValue="Last Name" width="100">
            <apex:inputField value="{!Lead.LastName}"/>
          </apex:column> -->
          
          <apex:column headerValue="Email" width="100" style="font-size:12px">
            <apex:inputField value="{!LeadBackup__c.Email__c}"/>
          </apex:column>
          <apex:column headerValue="Phone" width="100">
            <apex:inputField value="{!LeadBackup__c.Phone__c}"/>
          </apex:column>
          <apex:column headerValue="Evening Phone" width="100">
            <apex:inputField value="{!LeadBackup__c.Evening_Phone__c}"/>
          </apex:column>
          <apex:column headerValue="Preferred Call Time" width="100">
            <apex:inputField value="{!LeadBackup__c.Preferred_Call_Time__c}"/>
          </apex:column>
          <apex:column headerValue="Notes" width="100">
            <apex:inputField value="{!LeadBackup__c.Notes__c}"/>
          </apex:column>
          <apex:column headerValue="Do Not Call" width="100">
            <apex:inputField value="{!LeadBackup__c.Do_Not_Call__c}"/>
          </apex:column>
          
        </apex:pageBlockTable>
      </apex:pageBlockSection>
     </apex:pageBlock>
    </div> 
   </apex:form>
</apex:page>

 Controller:

 

public  class SiteLead {
    
    private ApexPages.StandardController controller {get; set;}
    public List<LeadBackup__c> searchResults {get;set;}
    public string searchText {get;set;}
    //public Lead Leads;
   
    
    public SiteLead(ApexPages.StandardController controller) {
    


    }
    
    List<Lead> Leads=[select id, name, FirstName, LastName, Status, Ref_ID_File_Number__c, Email, Phone, Street, City, State, Country, PostalCode, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall from Lead where Ref_ID_File_Number__c =:searchText ];
 
    
    public PageReference search() {
   // String qry = 'select id,Ref_ID_File_Number__c, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall__c from LeadBackup__c' + where Ref_ID_File_Number__c = searchText  order by name';
    searchResults = [select id,RefID_FileNumber__c,Name, Email__c, Phone__c, Street__c, City__c, State__c, TAB_Message__c, TAB_Time_stamp__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,Do_Not_Call__c from LeadBackup__c where RefID_FileNumber__c =:searchText  order by name];
    //searchResults = Database.query(qry);
   
     
     //searchResults = [select id, name, FirstName, LastName, Status, Ref_ID_File_Number__c, Email, Phone, Street, City, State, Country, PostalCode, TAB_Message__c, Evening_Phone__c, Preferred_Call_Time__c, Notes__c,DoNotCall from Lead where Ref_ID_File_Number__c =:searchText  order by name];

   
    if(searchResults.size()==0){

          LeadBackup__c lb = new  LeadBackup__c(RefID_FileNumber__c = searchText,Name=Leads[0].Name, Lead__c = Leads[0].id, Email__c = Leads[0].Email, Phone__c = Leads[0].Phone,
                                                Evening_Phone__c = Leads[0].Evening_Phone__c, Preferred_Call_Time__c = Leads[0].Preferred_Call_Time__c, Notes__c = Leads[0].Notes__c,
                                                Do_Not_Call__c = Leads[0].DoNotCall);
          searchResults.add(lb);
          insert searchResults ;////////Error line
          
    }else{
    
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Info,'<b>Please confirm name and address below. Address will be where the letter was received/addressed. If these do not match, you will need to re-verify Ref ID, as it may have been entered incorrectly</b>'));
       return null;
    }
    return null;
  }
  
  
  public PageReference save() {
   
   if(!(String.isEmpty(searchText))){
          
       try {
            
            list<LeadBackup__c> lstupdate = new list<LeadBackup__c>();
            
                 
             For(LeadBackup__c le:searchResults){
             
             //le.Id = lead.Id;
             le.RefID_FileNumber__c = searchResults[0].RefID_FileNumber__c ;
             le.Lead__c = Leads[0].id;
             le.Name = Leads[0].Name;
             le.Email__c = Leads[0].Email;
             le.Phone__c = Leads[0].Phone;
             le.Evening_Phone__c = Leads[0].Evening_Phone__c ;
             le.Preferred_Call_Time__c = Leads[0].Preferred_Call_Time__c;
             le.Notes__c = Leads[0].Notes__c;
             le.Do_Not_Call__c = Leads[0].DoNotCall;
             le.TAB_Message__c = True;
             le.TAB_Time_stamp__c = System.now();
             lstupdate.add(le);
             }
             
             update lstupdate;
      
      ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'Record <b>'+searchResults[0].Name+'</b> has been Updated'));
            
    } Catch (DMLException e) {
      ApexPages.addMessages(e);
      return null;
    }
   
    searchText='';
    searchResults.clear(); 
    return null;
       
  }
  else
  {
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info,'Please enter REFID/FileNumber'));
  }
  
  return null;
 
}
  public PageReference cancel() {
       
       pageReference pg1 = new pageReference('/apex/siteupdatinglead');
       return(pg1.setredirect(true));
       
  }

}

 Please help me out in this regard, any help greatly appreciated..

Bhawani SharmaBhawani Sharma
You cannot update already converted lead in Salesforce. So you will have to user Isconverted = false in your where clause, when you are querying lead data.\
where Ref_ID_File_Number__c =:searchText AND IsConverted = false