+ Start a Discussion
Sheila RashadSheila Rashad 

Reason: Apex trigger LeadConvert caused an unexpected exception, contact your administrator: LeadConvert: execution of AfterInsert

Hello everone, I need help on the following error message:
Reason: Apex trigger LeadConvert caused an unexpected exception, contact your administrator: LeadConvert: execution of AfterInsert

caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Your lead is missing a field mapping for the Pet Primary Caregiver,Household Allergies,Household Pets,Top Dogg K9 Exposure fields. 

LeadConvert Trigger:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
trigger LeadConvert on Lead (after insert) {

    ID acctID, ContID;
    /* Get the Id and MasterLabel from LeadStatus object */
    LeadStatus convertStatus = [ select Id, MasterLabel from LeadStatus where IsConverted = true limit 1 ];
    
  List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

  for (Lead lead: Trigger.new) {
    /* if (!lead.isConverted && lead.WebForm__c == 'Free Trial') { */
        

        try {
        Account acct = new Account(Name=lead.LastName + ' Household', recordtypeid = '0123i000000paRjAAI');
        insert acct;
        // Once the account is inserted, the sObject will be populated with an ID. Get this ID.
        acctID = acct.ID;
      Contact cont = new Contact( Contact_type__c='Applicant', AccountId=acctID);
        insert cont;
            contID = cont.ID;
      
    } catch(DmlException e) {
        System.debug('An unexpected error has occurred: ' + e.getMessage());
    }
        
        

        if (!lead.isConverted) {
      Database.LeadConvert lc = new Database.LeadConvert();
      lc.setLeadId(lead.Id);
            
            //String oppName = lead.Name;
      //lc.setOpportunityName(oppName);
      lc.setAccountId(acctID);
      lc.setConvertedStatus(convertStatus.MasterLabel);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
      System.assert(lcr.isSuccess());
            
      //leadConverts.add(lc);
    }
  }

  //if (!leadConverts.isEmpty()) { 
    //List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);

Not sure if I should make these fields not required on the Salesforce side to prevent error or deactivate the trigger. Also discovered that a blank application can be submitted which should not be the case. Please advise how to remove the error message and if it should be addressed on the application side on the website or the Salesforce side. 
ShirishaShirisha (Salesforce Developers) 
Hi Sheila,

Greetings!

Seems like the required fields are missing while creating the Account and Contact records.So,I would suggest you to try by mapping the fields to account and Contact records to avoid getting error.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Sunil Kumar Reddy Y 9Sunil Kumar Reddy Y 9
Hi Sheila, Try to map the required fields while converting or there is a another way to skip these validations. Below is the screenshot. uncheck the "Required Validation for Converted Leads" checkbox under Lead settings.
User-added image

Please mark this as best answer if this answered your question.
alphonso worrellalphonso worrell
tplink repeater http://tplink-repeater.net provides details regarding the New Extender Setup. Visit the setup page
akkkakkk
Hi Sheila Rashad,

Used this Trigger for Lead Convert and set the according to condition
//auto convert the lead based on the Condition like lead come from web the convert 
Trigger AutoConverter on Lead (after insert) {
     LeadStatus convertStatus = [
          select MasterLabel
          from LeadStatus
          where IsConverted = true
          limit 1
     ];
     List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

     for (Lead lead: Trigger.new) {
          if (!lead.isConverted && lead.LeadSource == 'web') {
               Database.LeadConvert lc = new Database.LeadConvert();
               String oppName = lead.Name;
               
               lc.setLeadId(lead.Id);
               lc.setOpportunityName(oppName);
               lc.setConvertedStatus(convertStatus.MasterLabel);
               
               leadConverts.add(lc);
          }
     }

     if (!leadConverts.isEmpty()) {
          List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
}