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
Gabe RothmanGabe Rothman 

Can anyone tell me why this unit test is failing? Pulling my hair out trying to figure it out...

Trigger:

trigger Lead on Lead (after insert, after update) {
 
   if(trigger.isInsert || trigger.isUpdate){
  	 	ConvertLeadOnActiveService.convertLead(trigger.new);
   }
	
   if(trigger.isInsert){
		List<lead> leads = new List<Lead>();
		for(lead l : trigger.new){
 			if(l.PagerDuty_Account_ID__c != null) {
  			leads.add(l);
			}
		}
		CreatePddOnLeadService.createPDD(leads);
   }

   if(trigger.isUpdate){
		CreatePddOnLeadService.createPDD(CreatePddOnLeadService.filterPDIDIsNull(trigger.oldmap,trigger.new));
		ReassociatePDDToAccountService.movePDD(ReassociatePDDToAccountService.filterConverted(trigger.oldmap,trigger.new));
   }

}

Service Class
public with sharing class ConvertLeadOnActiveService {
   
    public static void convertLead(list<lead> convertList) {
   
     for (Lead lead : convertList) {
        if (lead.isConverted == false && lead.State__c == 'Active'){
      
         Database.LeadConvert lc = new Database.LeadConvert();
         lc.setLeadId(lead.Id);
      
          String oppName = lead.Company;
         lc.setOpportunityName(oppName);
      
         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());
      
      }
    }
  }
}


Test:
@isTest
public class TestConvertLeadonActive {
        static testMethod void verifyleadConvert(){
        
       Lead l = new Lead();
       l.FirstName = 'Test';
       l.LastName = 'Test';
       l.Company = 'Test, Inc.';
       l.Status = 'A. New-Open';
       l.State__c = 'trial';
       l.Email = 'test@test.com';
       l.State__c = 'Active';
       insert l;    
       
       List<Lead> retrieveLead = [SELECT id FROM Lead WHERE id =: l.Id AND isConverted = true];
        
      Integer size = retrieveLead.size();
      system.assertEquals(1,size);            

               
        }
    }
Error Message:

Class.TestConvertLeadonActive.verifyleadConvert: line 13, column 1
14:47:09.467 (8467634625)|FATAL_ERROR|System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Lead: execution of AfterInsert

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: 1544795524-319229 (-1093901949): []

Andy BoettcherAndy Boettcher
Gotta love those error codes.  =)

The best bet would be to start inserting some System.Debug statements throughout your code and comment out a bunch to see EXACTLY where it is failing.  It could be the trigger stepping on itself (you appear to be doing many things to the same record at the same time) or just a violation of the order of execution somewhere.

Just start small and step it through.
Cloud_forceCloud_force
you are converting lead in you class... and after that you you are trying to manipulate field data in that lead... why would you do that? after lead is converted its gone.. it is converetd into an account.. is that as per requirement?

thanks,
http://www.forcexplore.com/2014/07/wrapper-class-in-salesforce.html
Gabe RothmanGabe Rothman
@sfdc_wave - are you referring to the service class or the test class?  I am not sure I see where in the code I'm manipulating data on the lead after convert.  The service class is supposed to perform a fairly simple action: if the State__c field is set to 'active' the lead should convert.   Thanks a bunch for the response, I appreciate it!