+ Start a Discussion
NasirNasir 

System.DmlException: ConvertLead failed.

Hi

 

I am getting this error in the production.

 

System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ConvertLead: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0062000000F3KnAAAV; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Profile Class allowed on this Record Type is 00. Your MPAN should start with 00.: [MPAN_1_Top_Line__c] Trigger.ConvertLead: line 206, column 5

 

This is a validation rule which was implemented in the production.Due to this there is a code breakage.

 

The related class is

 public static testMethod void testConvertLead(){
            Lead newLead = new Lead();
            newLead = setupLead();
            newLead.Status = 'Qualified';
            System.assertNotEquals(newLead, null);
            newLead.Status = 'Qualified';
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(newLead.id);
            lc.setOwnerId(UserInfo.getUserId());

            LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus where IsConverted=true limit 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            Database.LeadConvertResult lcr = Database.convertLead(lc); 
            System.assert(lcr.isSuccess());
    }

 

 

and the related trigger is

 

trigger ConvertLead on Lead (after update) {
    List<Account> Accs = new List<Account>();
    List<Opportunity> oppors = new List<Opportunity>();
    List<Opportunity> updateOprs = new List<Opportunity>();
    Id UserProfileId = [Select Name, Id From Profile where Name = 'Sub Broker'].Id;
    List<Task> tsks = new List<Task>();
    List<Contact> decisionMakerContacts = new List<Contact>();
    for(Lead ConvertedLead : Trigger.new){
          if(ConvertedLead.IsConverted == true){   
             Account Acc = [SELECT Id, Name FROM Account WHERE Id = :ConvertedLead.ConvertedAccountId];
             Contact cnt = [Select Id, Name, Email from Contact WHERE Id = :ConvertedLead.ConvertedContactId];
             tsks = [Select WhatId, Subject From Task where WhatId = : ConvertedLead.ConvertedAccountId or WhatId = : ConvertedLead.ConvertedOpportunityId limit 1];
         /*      Acc.Channel_Source__c = ConvertedLead.Channel_Source__c;
             Acc.Channel_Type__c = ConvertedLead.Channel_Type__c;
             Acc.Channel_Partner__c = ConvertedLead.Channel_Partner__c;
             Date formatDate = date.newInstance(ConvertedLead.CreatedDate.dayOfYear(), ConvertedLead.CreatedDate.month(), ConvertedLead.CreatedDate.day());
             Double Closedays = formatDate.daysBetween(System.Today());
             
             Acc.ShippingStreet = ConvertedLead.Site_Street__c;
             Acc.Site_Building_Number__c = ConvertedLead.Site_Buiilding_Number__c;
             Acc.Site_Building_Name__c = ConvertedLead.Site_Buiilding_Name__c;
             Acc.ShippingState = ConvertedLead.Site_County__c;
             Acc.ShippingPostalCode = ConvertedLead.Site_Postal_Code__c;
             Acc.ShippingCountry = ConvertedLead.Site_Country__c;
             Acc.ShippingCity  = ConvertedLead.Site_City__c; 
             Acc.days_between_the_LEAD_and_Acc_Creat_Date__c = Closedays;
             Acc.RA_Building_Name__c = ConvertedLead.RA_Building_Name__c;
             Acc.RA_Building_Number__c = ConvertedLead.RA_Building_Number__c;
             Acc.RA_Street__c  = ConvertedLead.RA_Street__c;
             Acc.RA_Postal_Code__c  = ConvertedLead.RA_Postal_Code__c; 
             Acc.RA_County__c  = ConvertedLead.RA_County__c; 
             Acc.RA_Country__c  = ConvertedLead.RA_Country__c;
             Acc.RA_City__c  = ConvertedLead.RA_City__c; 
             Acc.Company_Reg_No__c =  ConvertedLead.Company_Reg_Number__c;
             Acc.Date_LOA_Obtained__c =ConvertedLead.Date_LOA_Obtained__c;
             Acc.LOA_Obtained__c =ConvertedLead.LOA_Obtained__c;
             Acc.Current_Electric_Supplier_Meter1__c = ConvertedLead.New_Supplier__c; */
             
                                 
              if(ConvertedLead.Related_Utility__c == 'Gas & Electricity' || ConvertedLead.Related_Utility__c == 'Gas' || ConvertedLead.Related_Utility__c == 'Electricity'){
                Opportunity updateopr = leadOpportunity.setOpportunityRecordType(ConvertedLead, UserProfileId);
                updateopr.Channel_Source__c = ConvertedLead.Channel_Source__c;
                updateopr.Channel_Type__c = ConvertedLead.Channel_Type__c;
                updateopr.Channel_Partner__c = ConvertedLead.Channel_Partner__c;
                updateopr.Description = ConvertedLead.Description;
            /*    updateopr.Site_Building_Number__c = ConvertedLead.Site_Buiilding_Number__c;
                updateopr.Site_Building_Name__c = ConvertedLead.Site_Buiilding_Name__c;
                updateopr.Site_Street__c = ConvertedLead.Site_Street__c;
                updateopr.Site_Postal_Code__c = ConvertedLead.Site_Postal_Code__c;
                updateopr.Site_County__c = ConvertedLead.Site_County__c;
                updateopr.Site_Country__c = ConvertedLead.Site_Country__c;
                updateopr.Site_City__c = ConvertedLead.Site_City__c;
                
                updateopr.New_Supplier_Renewal_Opt_Out__c = ConvertedLead.New_Supplier_Renewal_Opt_Out__c;
                updateopr.Partner_Reference__c = ConvertedLead.Partner_Reference_Number__c;
            
             */
                //if(ConvertedLead.Current_Supplier__c != null) updateopr.Current_Supplier__c = ConvertedLead.Current_Supplier__c;
                if(ConvertedLead.Related_Utility__c == 'Gas'){
              /*        Acc.HasGas__c =  true;
                    if(ConvertedLead.MPR_2__c != null)Acc.MPR_2_Account_2__c =  ConvertedLead.MPR_2__c;
                    Acc.MPR_1_Account_2__c =  ConvertedLead.MPR_1__c;
                    
                    updateopr.Name = 'Gas';
                    updateopr.Related_Utility__c = 'Gas';
                    //updateopr.Current_Supplier__c = ConvertedLead.Current_Supplier_Electricity__c;
                    updateopr.Current_Supplier__c = ConvertedLead.Current_Supplier_Gas__c;
                    //updateopr.Current_Supplier__c = ConvertedLead.Current_Supplier__c;
                    updateopr.Current_Gas_Unit_Price_2__c = ConvertedLead.Current_Gas_Unit_Price_2__c;
                    updateopr.Current_Gas_Unit_Price_1__c = ConvertedLead.Current_Gas_Unit_Price_1__c;
                     
                    updateopr.Annual_Spend__c = ConvertedLead.Annual_Spend_Gas__c;
                    updateopr.Current_Gas_Unit_Price_1__c = ConvertedLead.Current_Gas_Unit_Price_1__c;
                    
                    updateopr.Contract_Status__c = ConvertedLead.Contract_Status_Gas__c;
                    
                    if(ConvertedLead.MPR_2__c != null)updateopr.MPR_2__c = ConvertedLead.MPR_2__c ; 
                    updateopr.MPR_2_AQ__c = ConvertedLead.MPR_2_AQ__c; 
                    updateopr.MPR_1__c = ConvertedLead.MPR_1__c; 
                    updateopr.MPR_1_AQ__c = ConvertedLead.MPR_1_AQ__c;
                    
                         
                */
                    if(ConvertedLead.Actual_Expiry_Date_Gas__c != null){updateopr.Actual_Expiry_Date__c = ConvertedLead.Actual_Expiry_Date_Gas__c;} 
                    
                     if(ConvertedLead.Contract_Status_Gas__c == 'In Contract'){
                        updateopr.StageName = 'Prospecting';
                    } 
                    
                }
                 if(ConvertedLead.Current_Supplier_Gas__c == ConvertedLead.New_Supplier__c){
                //   updateopr.Current_Supplier__c = ConvertedLead.New_Supplier__c;
                 }
                 
                  if(ConvertedLead.Current_Supplier_Electricity__c == ConvertedLead.New_Supplier__c){
                    // updateopr.Current_Supplier__c = ConvertedLead.New_Supplier__c;
                 }
                   
                    //updateopr.Contract_Term_Months__c = ConvertedLead.Contract_Term_Months_FL__c ;
                    
                    if(ConvertedLead.Related_Utility__c == 'Electricity' || ConvertedLead.Related_Utility__c == 'Gas & Electricity'){
                     // Acc.HasElectricity__c = true;
                     
                    /*updateopr.Name = 'Electricity';
                    updateopr.Related_Utility__c = 'Electricity';
                    updateopr.KVA__c = ConvertedLead.KVA__c;
                    /*updateopr.MPAN_1_Top_Line__c = ConvertedLead.MPAN_Profile__c+ConvertedLead.MTC__c+ConvertedLead.LLF__c;
                    updateopr.MPAN_1_Bottom_Line__c = ConvertedLead.MPAN_Dis_Id__c+ConvertedLead.MPANLeft__c+ConvertedLead.MPANMid__c+ConvertedLead.MPANRight__c;
                    if( ConvertedLead.Rel_MPAN_Profile__c != null )updateopr.MPAN_2_Top_Line__c = ConvertedLead.Rel_MPAN_Profile__c;
                    if(ConvertedLead.Rel_MTC__c != null) updateopr.MPAN_2_Top_Line__c += ConvertedLead.Rel_MTC__c+ConvertedLead.Rel_LLF__c;
                    if(ConvertedLead.Rel_LLF__c != null) updateopr.MPAN_2_Top_Line__c += ConvertedLead.Rel_LLF__c;
                    if(ConvertedLead.Rel_MPAN_Dis_Id__c != null) updateopr.MPAN_2_Bottom_Line__c = ConvertedLead.Rel_MPAN_Dis_Id__c+ConvertedLead.Rel_MPANLeft__c+ConvertedLead.Rel_MPANMid__c+ConvertedLead.Rel_MPANRight__c;
                    if(ConvertedLead.Rel_MPANLeft__c != null) updateopr.MPAN_2_Bottom_Line__c += ConvertedLead.Rel_MPANLeft__c;
                    if(ConvertedLead.Rel_MPANMid__c != null) updateopr.MPAN_2_Bottom_Line__c += ConvertedLead.Rel_MPANMid__c;
                    if(ConvertedLead.Rel_MPANRight__c != null) updateopr.MPAN_2_Bottom_Line__c += ConvertedLead.Rel_MPANRight__c;

                    updateopr.LLF__c = ConvertedLead.LLF__c;
                    updateopr.MPAN_Profile__c = ConvertedLead.MPAN_Profile__c;
                    updateopr.MPAN_Dis_Id__c = ConvertedLead.MPAN_Dis_Id__c;
                    updateopr.MPANLeft__c = ConvertedLead.MPANLeft__c; */
                    if(ConvertedLead.Actual_Expiry_Date_Electricity__c != null){updateopr.Actual_Expiry_Date__c = ConvertedLead.Actual_Expiry_Date_Electricity__c;}
                //  updateopr.MPANMid__c = ConvertedLead.MPANMid__c;
                //  updateopr.MPANRight__c = ConvertedLead.MPANRight__c;
                //  updateopr.Related_MPAN__c = ConvertedLead.Related_MPAN__c;
                    
                    //updateopr.Contract_Status__c = ConvertedLead.Contract_Status_Electricity__c;
                    
                    //if(ConvertedLead.Rel_MPAN_Profile__c!= null)updateopr.Related_MPAN_Profile__c = ConvertedLead.Rel_MPAN_Profile__c;
                    
                    //if(ConvertedLead.Rel_MTC__c != null)updateopr.Related_MTC__c = ConvertedLead.Rel_MTC__c;
                    //if(ConvertedLead.Rel_LLF__c != null) updateopr.Rel_LLF__c = ConvertedLead.Rel_LLF__c;
                    //if(ConvertedLead.Rel_MPANLeft__c != null) updateopr.Rel_MPANLeft__c = ConvertedLead.Rel_MPANLeft__c;
                    //if(ConvertedLead.Rel_MPANMid__c != null) updateopr.Rel_MPANMid__c = ConvertedLead.Rel_MPANMid__c;
                    //if(ConvertedLead.Rel_MPANRight__c != null) updateopr.Rel_MPANRight__c = ConvertedLead.Rel_MPANRight__c;
                    //updateopr.MTC__c = ConvertedLead.MTC__c;
                    //updateopr.Voltage__c = ConvertedLead.Voltage__c;
                    //updateopr.Current_Supplier__c = ConvertedLead.Current_Supplier_Electricity__c;
                    //updateopr.Email_Contact_del__c = cnt.Id;
                    
                    //updateopr.Electric_AQ_Day__c = ConvertedLead.Electric_AQ_Day__c;
                    //updateopr.Electric_AQ_Eve_W_end__c = ConvertedLead.Electric_AQ_Eve_W_end__c;
                    //updateopr.Electric_AQ_Night__c = ConvertedLead.Electric_AQ_Night__c;
                    //updateopr.Annual_Spend__c  = ConvertedLead.Annual_Spend_Electricity__c;
                    
                    //updateopr.Site_Building_Number__c = ConvertedLead.Site_Buiilding_Number__c;
                     //updateopr.Site_Building_Name__c = ConvertedLead.Site_Buiilding_Name__c;
                     //updateopr.Site_Street__c = ConvertedLead.Site_Street__c;
                     //updateopr.Site_Postal_Code__c = ConvertedLead.Site_Postal_Code__c;
                     //updateopr.Site_County__c = ConvertedLead.Site_County__c;
                     //updateopr.Site_Country__c = ConvertedLead.Site_Country__c;
                     //updateopr.Site_City__c = ConvertedLead.Site_City__c; 
                     
                }
                                
                if(ConvertedLead.Related_Utility__c == 'Gas & Electricity'){
                    //Opportunity oppr = CreateGasOpportunity.createOpportunity(ConvertedLead, UserProfileId, Acc, cnt); 
                 // oppr.Lead_Created__c = formatDate;
                    //Acc.HasGas__c =  true;
                //  Acc.MPR_2_Account_2__c =  ConvertedLead.MPR_2__c;
                    //Acc.MPR_1_Account_2__c =  ConvertedLead.MPR_1__c;
                    //oppr.Customer_Type__c = 'New Customer';
                    //oppr.Type = 'New Business';
                    //oppors.add(oppr);
                   // ConvertedLead.addError('Lead cannot be converted');
                }
                
                if(updateopr.Name == 'Electricity'){
                   if(ConvertedLead.Related_Utility_Type__c == 'Non Half Hourly'){
                      //    updateopr.KVA__c = ConvertedLead.KVA__c ;
                       //   updateopr.Voltage__c = ConvertedLead.Voltage__c;
                   }    
                }
                 if(ConvertedLead.Contract_Status__c == 'In Contract'){
                    updateopr.Type = 'Future Business';
           }
                if(ConvertedLead.Related_Utility__c == 'Gas'){
                //updateopr.Current_Supplier__c = ConvertedLead.Current_Supplier_Gas__c;
                }
                // Create Opportunity Task
                // Create Task for the Opportunity
                if(ConvertedLead.Name_of_Decision_Maker__c != null){
                   List<String> Str = ConvertedLead.Name_of_Decision_Maker__c.split(' ',-2);
                   String FirstName = '';
                   String LastName = '';
                   if(Str.size() == 1)LastName = Str.get(0);
                   if(Str.size() > 1){
                        FirstName = Str.get(0);
                        LastName = Str.get(1);
                   }
                   Contact decisionMakerContact = new Contact(AccountId = Acc.Id, FirstName = FirstName, LastName = LastName,Phone=ConvertedLead.Phone,Email=ConvertedLead.Email,MailingCity=ConvertedLead.RA_City__c,MailingPostalCode=ConvertedLead.RA_Postal_Code__c,MailingStreet=ConvertedLead.RA_Street__c,Title=ConvertedLead.Title);
                   decisionMakerContacts.add(decisionMakerContact);
                } 
                //updateopr.Customer_Type__c = 'New Customer';
                //updateopr.Type = 'New Business';
                updateopr.Opportunity_Contact_Name__c = cnt.Name;
                updateopr.Email_Contact_del__c = cnt.Id;
                updateopr.Lead_Created__c = ConvertedLead.createdDate.date();
                Acc.Lead_Created_Date__c = ConvertedLead.createdDate.date();
                Accs.add(Acc); 
                updateOprs.add(updateopr);
              }
          } 
    }
    
    update Accs;
    update updateOprs;
//  if(oppors.size() > 0){insert oppors;}
    if(decisionMakerContacts.size() > 0){insert decisionMakerContacts;}
}

 

I am getting error in the 3 last line 

 

 

update updateOprs;

 

Please help

Jeremy_nJeremy_n

You have a validation rule that seems to require a specific value in the field MPAN_1_Top_Line__c, possibly based on the recordtype of the Opportunity. You are not setting this field anywhere in your class, however, so it may or may not have the right value in it.

 

You need to determine what the value for that field should be, and set it that way in your test class.

 

Jeremy