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
ctewellctewell 

Database.ConvertLead failing from trigger in Winter '13

I have a trigger that calls Database.ConvertLead when certain conditions are met.

This call succeeds in production (currently at Summer '12) but is failing in sandbox (currently at Winter '13).

 

We only have basic support so the help desk was not helpful.  This is in spite of the fact that this is clearly a Winter '13 release issue.

 

Here is the error message.

 

System.DmlException: Update failed. First exception on row 0 with id a00V0000002mtbUIAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, FinancialAccountSummaryAutoConvert: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, An unexpected error occurred. Please include this ErrorId if you contact support: 297185815-14206 (-1560672462): [] Trigger.FinancialAccountSummaryAutoConvert: line 89,

 

This is on a Database.ConvertLead call from a trigger.  I have other apex code that runs ConvertLead without issue in Winter '13.  The error only happens when called from a trigger.

Best Answer chosen by Admin (Salesforce Developers) 
ctewellctewell

As of 9/27/2012 this error is no longer occurring.  I assume an update to Winter '13 code took place in the last 24 hours that fixed this.

 

 

All Answers

_Prasu__Prasu_

Does trigger sends any emails, or uses any formula fields in the code? Did you tried running the code by setting debug log on? 

ctewellctewell

The trigger does not use any formula fields.

Yes, I have run with debug monitoring on.  Your comment motivated me to take another look at the log. 

 

The call to Database.ConvertLead causes Lead triggers to fire.  One of these triggers queries the Account record for the Account Id the lead converted to.  The first internal error occurs here.  See below.

 

13:08:09.730 (3730795000)|CODE_UNIT_FINISHED|LeadSetConversionKey on Lead trigger event BeforeUpdate for [00QV00000044M4D]
13:08:09.730 (3730898000)|CODE_UNIT_STARTED|[EXTERNAL]|01q70000000Kyup|LeadConversion on Lead trigger event BeforeUpdate for [00QV00000044M4D]
13:08:09.730 (3730992000)|USER_DEBUG|[15]|DEBUG|Lead Trigger fired
13:08:09.731 (3731009000)|SYSTEM_CONSTRUCTOR_ENTRY|[16]|<init>()
13:08:09.731 (3731016000)|SYSTEM_CONSTRUCTOR_EXIT|[16]|<init>()
13:08:09.731 (3731028000)|SYSTEM_CONSTRUCTOR_ENTRY|[17]|<init>()
13:08:09.731 (3731032000)|SYSTEM_CONSTRUCTOR_EXIT|[17]|<init>()
13:08:09.731 (3731182000)|USER_DEBUG|[20]|DEBUG|Lead Id: 00QV00000044M4DMAU
ConvertedAcctId 001V000000C4rDhIAJ  Old Converted: null
13:08:09.731 (3731312000)|USER_DEBUG|[25]|DEBUG|Record Type 01270000000M9gWAAS  Account Type null
13:08:09.731 (3731509000)|USER_DEBUG|[42]|DEBUG|Number of accounts is 1
13:08:09.731 (3731712000)|SOQL_EXECUTE_BEGIN|[44]|Aggregations:0|select Id, SIC, Description, BOK_Referral_Id__c, CreatedDate from Account where Id IN :tmpVar1
13:08:09.732 (3732676000)|FATAL_ERROR|Internal Salesforce.com Error

 

ctewellctewell

 

Here is the TestLeadAutoConvert code.

/**
    TestLeadAutoConvert.cls  
    
    Description
    Test the Auto Conversion of Leads.
    
    Developer:  Carl Tewell
    
    Change History (most recent first)
    ----------------------------------------------------------
    05/26/2011  cdt Initial write
    12/05/2011  bld added start/stop test to allow for SOQL query limits, 
                    added phone number to allow for validation rules requiring contact information
*/
@isTest
private class TestLeadAutoConvert {
    static testMethod void TestLeadAutoConvert(){
        // Testing this hardcoded testKey ensures that we catch if SalesForce changes their digest 
        // routine. 
        String testKey = '61f6d8e85d77e694e8ab032241f4151bd8cbb2de3bf8450cb2d9428827cb1dcb67338d19fc76ac11b5b7b4cb6b21e3b17b3b95326c5f747f692f6a6b7ffe80ac';
        String myTaxId = '987654321-test';      
        // Get an active BOSC user
        User u = [Select id from user where Profile.Name like '%BOSC User' and IsActive = true][0];
        // Get record type id for 'BOSC Lead'
        Id BoscRecordTypeId = [select id from RecordType where Name = 'BOSC Lead'][0].Id;
        // Get record type id for 'Business Account'
        Id BusinessAcctRecordTypeId = [select id from RecordType where Name = 'Business Account'][0].Id;
        test.startTest();
        testLeadThenFas(testKey, myTaxId, u, BoscRecordTypeID, BusinessAcctRecordTypeId);
        testFasThenLead(testKey, myTaxId, u, BoscRecordTypeId, BusinessAcctRecordTypeId);
        test.stopTest();        

    }
    static void testLeadThenFas(String testKey, String myTaxId, User u, Id BoscRecordTypeId, Id BusinessAcctRecordTypeId){
        // Test updating the Fas with the TaxId and the Lead after that.
        // This will convert code using FinancialAccountSummaryAutoConvert.trigger
        String methodName = 'Method testLeadThenFas.  ';
        // Runas the Bosc User
        System.RunAs(u){
            // Create a Lead with a TaxId
            Lead myLead = new Lead();
            myLead.Company = 'Test Lead 1';
            myLead.FirstName = 'Test';
            myLead.LastName = 'Test';
            myLead.RecordTypeId = BoscRecordTypeId;
            myLead.Phone ='1234567890';
            Insert myLead;

            // Update the TaxId on the Lead record
            myLead.TaxId__c = myTaxId;
            update myLead;
                            
            // Create an Account 
            Account myAccount = new Account();
            myAccount.Name = 'Test Account 1';
            myAccount.RecordTypeId = BusinessAcctRecordTypeId;
            insert myAccount;
            // Create a Financial Account Summary with same TaxId 
            Financial_Account_Summary__c myFas = new Financial_Account_Summary__c();
            myFas.Account__c = myAccount.Id;
            insert myFas;
            // Update the TaxId on the Financial Account Summary record
            myFas.TaxId__c = myTaxId;
            update myFas;           
                        
            // Pull in the Lead with the updated LeadConversionKey
            myLead = [select Id, Company, TaxId__c, LeadConversionKey__c
                        from Lead
                        where Id = :myLead.Id][0];  
            // Assert that the LeadConversionKey value = expected value.  If this fails it means
            // SalesForce changed their digest routine and we may need to update LeadConversionKey
            // values based on new routine.
            System.debug('Asserting that LeadConversionKey = expected value');
            System.assertEquals(testKey, myLead.LeadConversionKey__c);
            // Get the fas and assert that the LeadConversKey values are the same for both
            myFas = [select Id, TaxId__c, LeadConversionKey__c
                        from Financial_Account_Summary__c
                        where Id = :myFas.Id][0];
            System.Debug(methodName + 'Asserting that Lead.LeadConversionKey = fas.LeadConversionKey');                                 
            System.AssertEquals(myLead.LeadConversionKey__c, myFas.LeadConversionKey__c);
            // Assert that the Lead is now converted
            myLead = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId, 
                            ConvertedOpportunityId, FirstName, LastName
                            from Lead 
                            where Id = :myLead.Id][0];
            System.Debug(methodName + 'Asserting that Lead is converted');                          
            System.Assert(myLead.IsConverted);
            // Assert that the ConvertedAccountId is the same as the AccountId
            System.debug(methodName + 'Asserting that myLead.ConveredAccountId = myAccount.Id');
            System.AssertEquals(myLead.ConvertedAccountId, myAccount.Id);
            // Assert that the Opportunity is 'Won'
            Opportunity opp = [Select Id, StageName FROM Opportunity
                                    where Id = :myLead.ConvertedOpportunityId];
            System.debug(methodName + 'Asserting that Won = opp.StageName');                            
            System.AssertEquals('Won', opp.StageName);  
            // Assert that the contact was created with same firstname and last name as Lead
            Contact myContact = [Select Id, FirstName, LastName from Contact
                                    Where Id = :myLead.ConvertedContactId][0];
            System.debug(methodName + 'Asserting that Contact.FirstName + Contact.LastName = Lead.FirstName + Lead.LastName');                          
            System.AssertEquals(myContact.FirstName + myContact.LastName, myLead.FirstName + myLead.LastName);  
        }
    }   
    static void testFasThenLead(String testKey, String myTaxId, User u, Id BoscRecordTypeId, Id BusinessAcctRecordTypeId){
        // Test updating the Fas with the TaxId and the Lead after that.
        // This will convert code using LeadAutoConvert.trigger
        String methodName = 'Method testFasThenLead.  ';
        // Runas the Bosc User
        System.RunAs(u){    
            // Create an Account 
            Account myAccount = new Account();
            myAccount.Name = 'Test Account 2';
            myAccount.RecordTypeId = BusinessAcctRecordTypeId;
            insert myAccount;
            // Create a Financial Account Summary with same TaxId 
            Financial_Account_Summary__c myFas = new Financial_Account_Summary__c();
            myFas.Account__c = myAccount.Id;
            insert myFas;
            // Doing this first for NOW !!!!!!!!!!!!!!!!!!!!!!!!!!!
            // Update the TaxId on the Financial Account Summary record
            myFas.TaxId__c = myTaxId;
            update myFas;           
                        
            // Create a Lead with a TaxId
            Lead myLead = new Lead();
            myLead.Company = 'Test Lead 2';
            myLead.FirstName = 'Test';
            myLead.LastName = 'Test';
            myLead.RecordTypeId = BoscRecordTypeId;
            myLead.Phone ='9876543210';
            Insert myLead;

            // Update the TaxId on the Lead record
            myLead.TaxId__c = myTaxId;
            update myLead;
            // Pull in the Lead with the updated LeadConversionKey
            myLead = [select Id, Company, TaxId__c, LeadConversionKey__c
                        from Lead
                        where Id = :myLead.Id][0];  
            // Assert that the LeadConversionKey value = expected value.  If this fails it means
            // SalesForce changed their digest routine and we may need to update LeadConversionKey
            // values based on new routine.
            System.debug('Asserting that LeadConversionKey = expected value');
            System.assertEquals(testKey, myLead.LeadConversionKey__c);
            // Get the fas and assert that the LeadConversKey values are the same for both
            myFas = [select Id, TaxId__c, LeadConversionKey__c
                        from Financial_Account_Summary__c
                        where Id = :myFas.Id][0];
            System.Debug(methodName + 'Asserting that Lead.LeadConversionKey = fas.LeadConversionKey');                                 
            System.AssertEquals(myLead.LeadConversionKey__c, myFas.LeadConversionKey__c);
            // Assert that the Lead is now converted
            myLead = [select Id, IsConverted, ConvertedAccountId, ConvertedContactId, 
                            ConvertedOpportunityId, FirstName, LastName
                            from Lead 
                            where Id = :myLead.Id][0];
            System.Debug(methodName + 'Asserting that Lead is converted');                          
            System.Assert(myLead.IsConverted);
            // Assert that the ConvertedAccountId is the same as the AccountId
            System.debug(methodName + 'Asserting that myLead.ConveredAccountId = myAccount.Id');
            System.AssertEquals(myLead.ConvertedAccountId, myAccount.Id);
            // Assert that the Opportunity is 'Won'
            Opportunity opp = [Select Id, StageName FROM Opportunity
                                    where Id = :myLead.ConvertedOpportunityId];
            System.debug(methodName + 'Asserting that Won = opp.StageName');                            
            System.AssertEquals('Won', opp.StageName);  
            // Assert that the contact was created with same firstname and last name as Lead
            Contact myContact = [Select Id, FirstName, LastName from Contact
                                    Where Id = :myLead.ConvertedContactId][0];
            System.debug(methodName + 'Asserting that Contact.FirstName + Contact.LastName = Lead.FirstName + Lead.LastName');                          
            System.AssertEquals(myContact.FirstName + myContact.LastName, myLead.FirstName + myLead.LastName);  

        }
    }
}

 

 

Here is the LeadConversion trigger code.

trigger LeadConversion on Lead (before update) {
/** 
   Trigger LeadConversion
   Description    This trigger copies the Standared SIC field from the Lead
                  object to the Account object.  The trigger is necessary because
                  SF does not provide ability to map custom object fields to 
                  standard object fields on lead conversion.
   Change History (newest first)
   20090826  ctewell    initial write
   20091209  ctewell    Set Global Referral Id on Account record if on Lead record and is null on Account
                        Also copy Description to Opportunity Description for Referrals
   
*/
    // Find the All account Ids converted to
    System.Debug('Lead Trigger fired');
    List<Id> AccountIds = new List<Id>();
    List<Id> OppIds = new List<Id>();
    for(Lead l : Trigger.new) {
       // If this update not caused by lead conversion skip the record.
       System.Debug('Lead Id: ' + l.Id + '\n' + ' ConvertedAcctId ' + l.ConvertedAccountId + 
             '  Old Converted: ' + Trigger.oldMap.get(l.id).ConvertedAccountId);
       if(l.ConvertedAccountId == null) continue;
       if(! (Trigger.oldMap.Get(l.id).ConvertedAccountId == null)) continue;
       // If it is a Referral check to see that the Account_Type is not null
       System.Debug('Record Type ' + l.RecordTypeId + '  Account Type ' + l.Account_Type__c);
       // RecordTypeId is for Referrals
       if(l.RecordTypeId == '01270000000Di2JAAS' && l.Account_Type__c == null) {
       	  System.Debug('Lead Name is ' + l.Name);
       	  System.Debug('First/Last Name is ' + l.FirstName + ' ' + l.LastName);
       	  System.Debug('Record Type is ' + l.RecordType.Name);
          l.AddError('Account Type must be set for Referrals before converting');
          continue;
       }
       // Save the account id in an array
       AccountIds.Add(l.ConvertedAccountId);  
       // Save opportunity in array if record type is a Referral
       if(l.RecordTypeId == '01270000000Di2JAAS' && l.ConvertedOpportunityId != null) {
         OppIds.Add(l.ConvertedOpportunityId);
       }
        
    }
    System.Debug('Number of accounts is ' + AccountIds.Size());
    // Update the SIC code in the accounts
    Map<Id, Account> AccountMap = new map<Id, Account>([select Id, SIC, Description, BOK_Referral_Id__c, CreatedDate 
         from Account where Id in :AccountIds]);
    Map<Id, Opportunity> OppMap = new map<Id, Opportunity>([select Id, Description 
         from Opportunity where Id in :OppIds]);
         
    Account acct;
    Opportunity opp;
    for(Lead l : Trigger.new){
       if(AccountMap.containsKey(l.ConvertedAccountId)){
          acct = AccountMap.Get(l.ConvertedAccountId);
          acct.SIC = l.SIC_Code__c;
          if(acct.Description == null){
            acct.Description = l.Description;
          }
          else {
            acct.Description += '\r' + l.Description;
          }
          if(acct.BOK_Referral_Id__c == null && l.BOK_Referral_Id__c != null) {
          	// Only set Referral_Id if this account created in last 2 minutes
          	if(acct.CreatedDate > DateTime.Now().AddMinutes(-2)){
            	acct.BOK_Referral_Id__c = l.BOK_Referral_Id__c;          		
          	}
          }
       }
       if(OppMap.containsKey(l.ConvertedOpportunityId)){
       	  opp = OppMap.Get(l.ConvertedOpportunityId);
       	  opp.Description = l.Description;
       }
    }  
    // Update the Accounts
    Update AccountMap.Values();
    // Update the Opportunities
    Update OppMap.Values();
}

 

 

mike.letullemike.letulle

Since you are not getting a helpful error, I would try breaking out the select that it is dying on into its own line without assigning it to a map and make sure you are getting something back.

 

 List<Account> accts = ([select Id, SIC, Description, BOK_Referral_Id__c, CreatedDate 
         from Account where Id in :AccountIds]);
System.debug(accts.size);

ctewellctewell

As of 9/27/2012 this error is no longer occurring.  I assume an update to Winter '13 code took place in the last 24 hours that fixed this.

 

 

This was selected as the best answer