+ 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 Lead on Lead (after insert, after update) {
   if(trigger.isInsert || trigger.isUpdate){
		List<lead> leads = new List<Lead>();
		for(lead l : trigger.new){
 			if(l.PagerDuty_Account_ID__c != null) {



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();
          String oppName = lead.Company;
         LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
         Database.LeadConvertResult lcr = Database.convertLead(lc);

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();

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.
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?

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!