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
Adam DanielAdam Daniel 

Lead conversion error - custom button

Hi, Salesforce support directed me here... I have a custom Convert button built by a firm I'm no longer in touch with. It's designed to take users to a custom de-duplicating page, then to create a few different records at once in the system: an opportunity, a client, and a property (that's a custom object the firm also built).

The button is suddenly giving users a couple different errors.

Here's one:

List has no rows for assignment to SObject
Error is in expression '{!Init_Save_Lead_Convert}' in component <apex:commandButton> in page leadconvert: Class.LeadController.Init_Save_Lead_Convert: line 118, column 1

And here's another:

List index out of bounds: 0 Error is in expression '{!Init_Save_Lead_Convert}' in component in page leadconvert: Class.LeadController.Init_Save_Lead_Convert: line 139, column 1 An unexpected error has occurred. Your development organization has been notified.

Hoping someone can help here?

Thanks,
AD
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Can you please post the Lead Controller class?
Adam DanielAdam Daniel
Thanks for your reply. I don't know what the Lead Controller class is, can you help? Sorry!
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Go to Setup>App Setup>Develop>Apex Classes
There you can find this class
Adam DanielAdam Daniel
I see it, but when I click in there's a lot of info. What exactly do you need? Adam Daniel Meridith Baer Home c 310.985.4725 o 310.204.5353 f 310.204.5453 adam@meridithbaer.com meridithbaer.com This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient or the person responsible for delivering the email to the intended recipient, be advised that you have received this email in error, and that any use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you have received this message in error, please advise the sender immediately by reply email and delete this message, and all copies of it, from your computer.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
I need to see this method
Init_Save_Lead_Convert
Adam DanielAdam Daniel
Tell me if this is enough of the code for you: public PageReference Init_Save_Lead_Convert (){ System.debug('Save Hit' + leadId); PageReference page = null; Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(leadId); lc.setConvertedStatus(convertStatus.MasterLabel); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); Id oppId = lcr.getOpportunityId(); Id accId = lcr.getAccountId(); Id conId = lcr.getContactId(); Opportunity opps = [SELECT Id FROM Opportunity WHERE Id = :oppId Limit 1]; System.debug('Debug-Oppslist1: ' + JSON.serialize(opps)); if(opps != null){ //Get leads account and contact List oobAcc = [SELECT Id FROM Account WHERE Id = :accId]; List oobCon = [SELECT Id FROM Contact WHERE Id = :conId]; Account brokerToAdd = null; if(rend1){ List clonedContacts = (List)UtilityClass.cloneObjects(oobCon, oobCon.get(0).getSObjectType()); if(String.isNotBlank(brokerAcc)){ RecordType rType = [SELECT Id FROM RecordType WHERE Name = :type AND sObjectType = 'Account' Limit 1]; if(rType != null){ Account[] brokerList = [SELECT Id, Primary_Contact__c, Name FROM Account WHERE Id = :brokerAcc Limit 1]; if(brokerList.size() > 0){ brokerToAdd = brokerList[0]; if(brokerList[0].Primary_Contact__c == null){ Contact newContact = clonedContacts.get(0); newContact.FirstName = leadFirstName; newContact.LastName = leadLastName; newContact.AccountId = brokerList[0].Id; Insert newContact; brokerToAdd.Primary_Contact__c = newContact.Id; update brokerToAdd; Adam Daniel Meridith Baer Home c 310.985.4725 o 310.204.5353 f 310.204.5453 adam@meridithbaer.com meridithbaer.com This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient or the person responsible for delivering the email to the intended recipient, be advised that you have received this email in error, and that any use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you have received this message in error, please advise the sender immediately by reply email and delete this message, and all copies of it, from your computer.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
It is so hard to read. Please format it properly and then post it.
Adam DanielAdam Daniel
Sorry, is this better? public PageReference Init_Save_Lead_Convert (){ System.debug('Save Hit' + leadId); PageReference page = null; Database.LeadConvert lc = new Database.LeadConvert(); lc.setLeadId(leadId); lc.setConvertedStatus(convertStatus.MasterLabel); Database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); Id oppId = lcr.getOpportunityId(); Id accId = lcr.getAccountId(); Id conId = lcr.getContactId(); Opportunity opps = [SELECT Id FROM Opportunity WHERE Id = :oppId Limit 1]; System.debug('Debug-Oppslist1: ' + JSON.serialize(opps)); if(opps != null){ //Get leads account and contact List oobAcc = [SELECT Id FROM Account WHERE Id = :accId]; List oobCon = [SELECT Id FROM Contact WHERE Id = :conId]; Account brokerToAdd = null; if(rend1){ List clonedContacts = (List)UtilityClass.cloneObjects(oobCon, oobCon.get(0).getSObjectType()); if(String.isNotBlank(brokerAcc)){ RecordType rType = [SELECT Id FROM RecordType WHERE Name = :type AND sObjectType = 'Account' Limit 1]; if(rType != null){ Account[] brokerList = [SELECT Id, Primary_Contact__c, Name FROM Account WHERE Id = :brokerAcc Limit 1]; if(brokerList.size() > 0){ brokerToAdd = brokerList[0]; if(brokerList[0].Primary_Contact__c == null){ Contact newContact = clonedContacts.get(0); newContact.FirstName = leadFirstName; newContact.LastName = leadLastName; newContact.AccountId = brokerList[0].Id; Insert newContact; brokerToAdd.Primary_Contact__c = newContact.Id; update brokerToAdd; Adam Daniel Meridith Baer Home c 310.985.4725 o 310.204.5353 f 310.204.5453 adam@meridithbaer.com meridithbaer.com This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient or the person responsible for delivering the email to the intended recipient, be advised that you have received this email in error, and that any use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you have received this message in error, please advise the sender immediately by reply email and delete this message, and all copies of it, from your computer.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
It would be better if you paste the full class. But don't paste it directly, first click on the <> icon and then paste the code.
Adam DanielAdam Daniel
public with sharing class LeadController {
    public Boolean LeadAddressIsValid {get;set;}
    public List<String> InvalidLeadAddressMessage {get;set;}
    public Opportunity opp{get;set;}
    public Property__c prop {get;set;}
    public Lead lObj;
    public Id leadId {get; set;}
    public String brokerText {get;set;}
    public String homeownerText {get;set;}
    public String propertyText {get;set;}
    public String leadFirstName {get;set;}
    public String leadLastName {get;set;}
    public LeadStatus convertStatus {get;set;}
    public Boolean rend1 {get;set;}
    public Boolean rend2 {get;set;}
    public Boolean render1 {get;set;}
    public Boolean render2 {get;set;}
    public String type {get;set;}
    public List<Property__c> matchedProperties {get; set;}

    public LeadController(ApexPages.StandardController stdController) {
        LeadAddressIsValid = false;
        InvalidLeadAddressMessage = new List<String>();
        leadId = stdController.getId(); 
        lObj = (leadid == null) ? new Lead():[SELECT Id, Property_Street__c, Billing_Street__c, Property_Unit__c,
        Property_State__c, Billing_State__c, Property_City__c, Billing_City__c, Property_Zip_Code__c,
        Billing_Zip_Code__c, New_Construction__c, Design_Style_Requested__c, House_Sq_Ft__c, Lock_Box_1__c, Lock_Box_2__c,
        Gate_Code__c, Lead_Type__c, Broker_Email__c, Broker_Fax__c, Broker_Phone_1__c, Broker_Phone_2__c,
        Homeowner_Email__c, Homeowner_Fax__c, Homeowner_Phone_1__c, Homeowner_Phone_2__c , Client_Type__c,
        FirstName, LastName, Broker_Name__c, Homeowner_Name__c, Email, Fax, Phone, MobilePhone, Access_Restrictions_List__c,
        Walkthrough_Scheduled_Date__c FROM Lead WHERE Id = :leadId];
        
        LeadAddressIsValid = true;
        
        InvalidLeadAddressMessage = new List<String>(); 
        
        if(String.isBlank(lObj.Property_Street__c)){
            InvalidLeadAddressMessage.add('Property Address - Street is blank');
            LeadAddressIsValid = false;
        }
        if(String.isBlank(lObj.Property_City__c)){
            InvalidLeadAddressMessage.add('Property Address - City is blank');
            LeadAddressIsValid = false;
        }
        if(String.isBlank(lObj.Property_State__c)){
            InvalidLeadAddressMessage.add('Property Address - State is blank');
            LeadAddressIsValid = false;
        }
        if(String.isBlank(lObj.Property_Zip_Code__c)){
            InvalidLeadAddressMessage.add('Property Address - Zip Code is blank');
            LeadAddressIsValid = false;
        }  
        
        if(lObj.Walkthrough_Scheduled_Date__c == null){
            InvalidLeadAddressMessage.add('Walkthrough - No Walkthrough Scheduled Date');
            LeadAddressIsValid = false;
        }
         
        if(!LeadAddressIsValid) return;
        
        prop = new Property__c();
        leadFirstName = lObj.FirstName;
        leadLastName = lObj.LastName;
        propertyText = lObj.Property_Street__c + (lObj.Property_Unit__c != null ? ' ' + lObj.Property_Unit__c : '');
        brokerText = lObj.Client_Type__c != 'Homeowner' ? lObj.FirstName+ ' '+lObj.LastName : lObj.Broker_Name__c;
        homeownerText = lObj.Client_Type__c != 'Homeowner' ? Lobj.Homeowner_Name__c : lObj.FirstName+ ' '+lObj.LastName;
        convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
        type = lObj.Client_Type__c;
        
        render1 = lObj.Client_Type__c == 'Realtor' ? true : false; // type is Realtor
        render2 = lObj.Client_Type__c == 'Homeowner' ? true : false; // type is Homeonwer
        
        if(render1 && string.isNotBlank(lObj.Homeowner_Name__c)){
            rend1 = true;
            rend2 = true;
        }
        else if(render1 && !string.isNotBlank(lObj.Homeowner_Name__c)){
            rend1 = true;
            rend2 = false;
        }        
        else if(render2 && string.isNotBlank(lObj.Broker_Name__c)){
            rend1 = true;
            rend2 = true;
            type = 'Realtor';
        }
        else if(render2 && !string.isNotBlank(lObj.Broker_Name__c)){
            rend1 = false;
            rend2 = true;
        }
        else {
            rend1 = true;
            rend2 = false;
        }
        
        matchedProperties = [Select Id,Name From Property__c where Name = :propertyText];
    } 
    public PageReference Init_Save_Lead_Convert (){
        System.debug('Save Hit' + leadId);
        PageReference page = null;
        Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(leadId);
        lc.setConvertedStatus(convertStatus.MasterLabel);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        Id oppId = lcr.getOpportunityId();
        Id accId = lcr.getAccountId();
        Id conId = lcr.getContactId();
        Opportunity opps = [SELECT Id FROM Opportunity WHERE Id = :oppId Limit 1];
        System.debug('Debug-Oppslist1: ' + JSON.serialize(opps));
        if(opps != null){
            //Get leads account and contact
            List<Account> oobAcc = [SELECT Id FROM Account WHERE Id = :accId];
            List<Contact> oobCon = [SELECT Id FROM Contact WHERE Id = :conId]; 
            Account brokerToAdd = null;
            if(rend1){
                List<Contact> clonedContacts = (List<Contact>)UtilityClass.cloneObjects(oobCon, oobCon.get(0).getSObjectType());
                if(String.isNotBlank(brokerAcc)){
                    RecordType rType = [SELECT Id FROM RecordType WHERE Name = :type AND sObjectType = 'Account' Limit 1];
                    if(rType != null){
                        Account[] brokerList = [SELECT Id, Primary_Contact__c, Name FROM Account WHERE Id = :brokerAcc Limit 1];
                        if(brokerList.size() > 0){
                            brokerToAdd = brokerList[0];
                            
                            if(brokerList[0].Primary_Contact__c == null){ 
                                Contact newContact = clonedContacts.get(0);
                                newContact.FirstName = leadFirstName;
                                newContact.LastName = leadLastName;
                                newContact.AccountId = brokerList[0].Id;
                                Insert newContact;
                                brokerToAdd.Primary_Contact__c = newContact.Id;
                                update brokerToAdd;
                            }
                        }
                    }
                }
                else{
                    if(String.isNotBlank(brokerText)){
                        RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = :type AND sObjectType = 'Account' Limit 1];
                        brokerToAdd = new Account(Name = brokerText, RecordTypeId = rType[0].Id);
                        insert brokerToAdd;
                        Contact newContact = clonedContacts.get(0);
                        newContact.FirstName = leadFirstName;
                        newContact.LastName = leadLastName;
                        newContact.AccountId = brokerToAdd.Id;
                        Insert newContact;
                        brokerToAdd.Primary_Contact__c = newContact.Id;
                        brokerToAdd.Email__c = lObj.Email;
                        brokerToAdd.Fax = lObj.Fax;
                        brokerToAdd.Phone = lObj.Phone;
                        brokerToAdd.Phone_2__c = lObj.MobilePhone;
                        brokerToAdd.BillingStreet = lObj.Billing_Street__c;
                        brokerToAdd.BillingCity = lObj.Billing_City__c;
                        brokerToAdd.BillingState = lObj.Billing_State__c;
                        brokerToAdd.BillingPostalCode = lObj.Billing_Zip_Code__c; 
                        brokerToAdd.Type = lObj.Client_Type__c; 
                        Update brokerToAdd;             
                    }
                }
            }
            Account homeownerToAdd = null;
            if(rend2){
                List<Contact> clonedContacts = (List<Contact>)UtilityClass.cloneObjects(oobCon, oobCon.get(0).getSObjectType());
                if(string.isNotBlank(homeownerAcc)){
                    RecordType rType = [SELECT Id FROM RecordType WHERE Name = 'Homeowner' AND sObjectType = 'Account' Limit 1];
                    Account[] homeownerList = [SELECT Id, Name, Primary_Contact__c FROM Account WHERE Id = :homeownerAcc Limit 1];
                    if(homeownerList.size() > 0){
                        homeownerToAdd = homeownerList[0];

                        if(homeownerList[0].Primary_Contact__c == null){
                            Contact newContact = clonedContacts.get(0);
                            newContact.FirstName = leadFirstName;
                            newContact.LastName = leadLastName;
                            newContact.AccountId = homeownerList[0].Id;
                            Insert newContact;
                            if (brokerToAdd != null){
                                homeownerToAdd.Primary_Contact__c = newContact.Id;
                                Update brokerToAdd;
                            }
                        }
                    }
                }
                else{
                    if(string.isNotBlank(homeownerText)){
                        RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = 'Homeowner' AND sObjectType = 'Account' Limit 1];
                        homeownerToAdd = new Account(Name = homeownerText, RecordTypeId = rType[0].Id);
                        if (brokerToAdd != null){
                            homeownerToAdd.ParentId = brokerToAdd.Id;
                        }
                        insert homeownerToAdd;

                        Contact newContact = clonedContacts.get(0);
                        newContact.FirstName = leadFirstName;
                        newContact.LastName = leadLastName;
                        newContact.AccountId = homeownerToAdd.Id;
                        Insert newContact;
                        homeownerToAdd.Primary_Contact__c = newContact.Id;
                        homeownerToAdd.Email__c = lObj.Homeowner_Email__c;
                        homeownerToAdd.Fax = lObj.Homeowner_Fax__c;
                        homeownerToAdd.Phone = lObj.Homeowner_Phone_1__c;
                        homeownerToAdd.Phone_2__c = lObj.Homeowner_Phone_2__c;
                        Update homeownerToAdd;                  
                    }
                }
            }
            Property__c propertyToAdd;
            if(string.isNotBlank(propGetSet)){
                Property__c[] propertyList = [SELECT Id, Name FROM Property__c WHERE Id = :propGetSet Limit 1];
                if(propertyList.size() > 0){
                    propertyToAdd = propertyList[0];
                }
            }
            else{
                if(string.isNotBlank(propertyText)){
                    propertyToAdd = new Property__c(Name = propertyText
                        , Street__c = lObj.Property_Street__c
                        , Unit__c = lObj.Property_Unit__c
                        , City__c = lObj.Property_City__c
                        , State__c = lObj.Property_State__c
                        , Zip_Code__c = lObj.Property_Zip_Code__c
                        , Lock_Box_1__c = lObj.Lock_Box_1__c
                        , Lock_Box_2__c = lObj.Lock_Box_2__c
                        , Gate_Code__c = lObj.Gate_Code__c
                        , Access_Restrictions_List__c = lObj.Access_Restrictions_List__c
                        , House_Sq_Ft__c = lobj.House_Sq_Ft__c
                        , New_Construction__c = lobj.New_Construction__c
                    );
                    
                    if(matchedProperties != null && !matchedProperties.isEmpty())
                        propertyToAdd.Potential_Duplicate__c = true;
                    
                    insert propertyToAdd;

                    opps.Name = propertyText;
                }
            }
            
            if(brokerToAdd != null){
                opps.AccountId = brokerToAdd.Id;
            }
            else if(homeownerToAdd != null){
                opps.AccountId = homeownerToAdd.Id;
            }
            if(homeownerToAdd != null){
                opps.Homeowner__c = homeownerToAdd.Id;
            }
            if (propertyToAdd != null){
                opps.Property__c = propertyToAdd.Id;
               // Update propertyToAdd;
            }
            if(brokerToAdd != null && propertyToAdd != null){
                propertyToAdd.Broker__c = brokerToAdd.Id;
                //Update propertyToAdd;
            }
            if(homeownerToAdd != null && propertyToAdd != null){
                propertyToAdd.Homeowner__c = homeownerToAdd.Id;
               // Update propertyToAdd;
            }              
            if(propertyToAdd != null){
                update propertyToAdd;
            }
            if(string.isNotBlank(propGetSet)){
                opps.Name = propertyText;
            }
            if(lObj.Lead_Type__c == 'Staging'){
                RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = 'Staging' Limit 1];
                opps.RecordTypeId = rType[0].Id;
            }
            else if(lObj.Lead_Type__c == 'Instant Install'){
                RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = 'Instant Install' Limit 1];
                opps.RecordTypeId = rType[0].Id;
            }
            else if(lObj.Lead_Type__c == 'Luxury Furniture Lease'){
                RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = 'Luxury Furniture Lease' Limit 1];
                opps.RecordTypeId = rType[0].Id;
            }
            else if(lObj.Lead_Type__c == 'Interior Design'){
                RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = 'Interior Design' Limit 1];
                opps.RecordTypeId = rType[0].Id;
            }
            else if(lObj.Lead_Type__c == 'Event'){
                RecordType[] rType = [SELECT Id FROM RecordType WHERE Name = 'Event' Limit 1];
                opps.RecordTypeId = rType[0].Id;
            }            
            //else{ //Type = Exception (anything under $10K)
            //    RecordType rType = [SELECT Id FROM RecordType WHERE Name = 'Exception' Limit 1];
            //    opps.RecordTypeId = rType.Id;
            //}     
            Update opps;
            Delete oobCon;
            //Delete oobAcc;
            System.debug('Debug-opplist2  ' + JSON.serialize(opps));
            page = new ApexPages.StandardController(opps).view();
            page.setRedirect(true);
        }
        return page;
    }


    public string brokerAcc{get;set;}
    public List<SelectOption> getAccountsBroke() {
        List<SelectOption> optionList = new list<SelectOption>();
        List<Account> accList;
        if (String.isNotBlank(brokerText)){
            RecordType rType = [SELECT Id, Name FROM RecordType WHERE Name = 'Realtor' Limit 1];
            accList = [Select Id, Name, RecordType.Name From Account where Name = :brokerText /*and RecordTypeId = :rType.Id*/];
            for(Account xAcc: accList){
                optionList.add(new SelectOption(xAcc.Id,'Use Existing: '+ xAcc.Name + (xAcc.RecordType.Name != rType.Name ? ' [' + xAcc.RecordType.Name + ']' : '')));
            }
        }
        optionList.Add(new SelectOption('','Create New: '+brokerText));
        return optionList;  
    }
    public string homeownerAcc{get;set;}
    public List<SelectOption> getAccountsHome() {
        List<SelectOption> optionList = new list<SelectOption>();
        List<Account> accList;
        if (String.isNotBlank(homeownerText)){
            RecordType rType = [SELECT Id, Name FROM RecordType WHERE Name = 'Homeowner' Limit 1];
            accList = [Select Id, Name, RecordType.Name From Account where Name = :homeownerText /*and RecordTypeId = :rType.Id*/];
            for(Account xAcc: accList){
                optionList.add(new SelectOption(xAcc.Id,'Use Existing: '+xAcc.Name + (xAcc.RecordType.Name != rType.Name ? ' [' + xAcc.RecordType.Name + ']' : '')));
            }
        }
        optionList.Add(new SelectOption('','Create New: '+homeownerText));
        return optionList;  
    }    

    public string propGetSet{get;set;}
    
    public List<SelectOption> getProperty() {
        List<SelectOption> optionList = new list<SelectOption>();
        
        if(String.isNotBlank(propertyText) && matchedProperties != null)
            for(Property__c p: matchedProperties)
                optionList.add(new SelectOption(p.Id,'Use Existing: '+ p.Name));
        
        optionList.Add(new SelectOption('','Create New: ' + propertyText));
        return optionList;  
    } 
    
}

 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Got it! The query on Record Type is not returning any record. Did you change anything on Account record types
RecordType rType = [SELECT Id FROM RecordType WHERE Name = :type AND sObjectType= 'Account' Limit 1];
Adam DanielAdam Daniel
Yes, I think so! We changed some values in our Lead Type and Client Type picklists.
SalesFORCE_enFORCErSalesFORCE_enFORCEr
So, this code takes the value from Client_Type__c and use it to query Account Record Types and then creates new account. 
Adam DanielAdam Daniel
What do I need to do to fix?
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Either update the Account Record Types/Lead Client Type to match each other OR modify the code to set some default record type. 
Adam DanielAdam Daniel
Thank you so much for your help!!