0

ERROR: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, LeadConvert: execution of BeforeInsert

caused by: System.SObjectException: Field mkto_si__Last_Interesting_Moment__c is not editable

Class.Class_Lead_TriggerFunction.mapContactFields: line 157, column 1 Class.Class_Lead_TriggerFunction.leadDedupCheck: line 63, column 1 Class.Class_Lead_TriggerFunction.beforeInsert: line 10, column 1 Trigger.LeadConvert: line 6, column 1: []

Field mkto_si__Last_Interesting_Moment__c is a formula field of a managed package and not been used in the code/trigger.

TriggerHelper class:

public static void leadDedupCheck(list<Lead> newList){
      set<Id> leadIdSetToDelete = new set<Id>();

      list<Lead> leadListToUpdate = new list<Lead>();
      list<Contact> contactListToUpdate = new list<Contact>();

    map<string,Lead> emailLeadMap = new map<string,Lead>();
    map<string,Lead> exisitngEmailLeadMap = new map<string,Lead>();
    map<string,Contact> exisitngEmailContactMap = new map<string,Contact>();

      for(Lead newLead : newList){ 
      if(newLead.Email != null){ 
        emailLeadMap.put(newLead.Email,newLead);
      }
    }
    system.debug('emailLeadMap*size()*******************************'+emailLeadMap.size());
    system.debug('emailLeadMap**************************************'+emailLeadMap);
    if(emailLeadMap.size() > 0){
      string leadQuery = constructQuery('Lead_Conversion_Field_Mapping','Lead',emailLeadMap.keyset()) + ' AND IsConverted = false';
      for(Lead existingLead : Database.query(leadQuery)){
        exisitngEmailLeadMap.put(existingLead.email,existingLead);
      }
      system.debug('exisitngEmailLeadMap*****************************'+exisitngEmailLeadMap); 
      for(Contact existingContact : Database.query(constructQuery('Lead_Conversion_Field_Mapping','Contact',emailLeadMap.keyset()))){
        exisitngEmailContactMap.put(existingContact.email,existingContact);
      }
      system.debug('exisitngEmailContactMap***********************************'+exisitngEmailContactMap);
      for(Lead leadToProcess : emailLeadMap.values()){
        if(exisitngEmailLeadMap.size() > 0 && exisitngEmailLeadMap.get(leadToProcess.email) != null){
          Lead leadToUpdate = mapLeadFields(exisitngEmailLeadMap.get(leadToProcess.email),leadToProcess);
          system.debug('leadToUpdate***********************************'+leadToUpdate);
          leadListToUpdate.add(leadToUpdate);
          leadIdSetToDelete.add(leadToProcess.Id);
          leadToProcess.Is_Duplicate__c = true;
          leadToProcess.Delete_Date_Time__c = (DateTime.now()).addMinutes(1);
        }
        else if(exisitngEmailContactMap.size() > 0 && exisitngEmailContactMap.get(leadToProcess.email) != null){
          Contact contactToUpdate = mapContactFields(exisitngEmailContactMap.get(leadToProcess.email),leadToProcess);
          system.debug('contactToUpdate***********************************'+contactToUpdate);
          contactListToUpdate.add(contactToUpdate);
          leadIdSetToDelete.add(leadToProcess.Id);
          leadToProcess.Is_Duplicate__c = true;
          leadToProcess.Delete_Date_Time__c = (DateTime.now()).addMinutes(1);        
        }
      }
      if(leadListToUpdate.size() > 0){
        update leadListToUpdate;
      }
      if(contactListToUpdate.size() > 0){
        update contactListToUpdate;
      }      
    }               
    }
    public static void deleteDuplicateLeads(list<Lead> newList,map<Id,Lead> oldMap){
    set<Id> leadIdSetToDelete = new set<Id>();
      for(Lead newLead : newList){
         if(trigger.isUpdate && oldMap != null && oldMap.get(newLead.Id).Ready_To_Delete__c != newLead.Ready_To_Delete__c && newLead.Ready_To_Delete__c){
        leadIdSetToDelete.add(newLead.Id);
      }          
    }
    if(leadIdSetToDelete.size() > 0){
      delete [select id from Lead where id in : leadIdSetToDelete];  
    }     
    }    
    
    public static void afterLeadConvert(list<Lead> newList,map<Id,Lead> oldMap){
      map<String,Id> accountNameIdMap = new map<string,Id>();
      set<string> leadCompanySet = new set<string>();
    list<Lead> leadListToConvert = new list<Lead>();
     
      for(Lead newLead : newList){
       if((trigger.isInsert || (trigger.isUpdate && oldMap.get(newLead.Id).LeadSource != newLead.LeadSource)) && newLead.Is_Duplicate__c == false && !(newLead.LeadSource != null && newLead.LeadSource.startsWith('SFDC'))){
        leadListToConvert.add(newLead);
        if(lead.Company != null){
          leadCompanySet.add(newLead.Company);
        }
       }  
    }
    system.debug('leadCompanySet*************************************'+leadCompanySet);
    if(leadCompanySet.size() > 0){
      for(Account account : [select Id,Name from Account where Name in : leadCompanySet]){
        accountNameIdMap.put(account.Name,account.Id);
              
      }
    }
    system.debug('accountNameIdMap*************************************'+accountNameIdMap);
    for(Lead newLead : leadListToConvert){
      Database.LeadConvert lc = new database.LeadConvert(); 
      lc.setLeadId(newLead.Id);
      if(accountNameIdMap.get(newLead.company) != null){
        lc.setAccountId(accountNameIdMap.get(newLead.company));
      }
         lc.setDoNotCreateOpportunity(true);         
      lc.setConvertedStatus(convertStatus.MasterLabel);
      Database.LeadConvertResult lcr = Database.convertLead(lc); 
      System.assert(lcr.isSuccess());
    }
    }
  
  public static String constructQuery(String fieldSetName, String ObjectName,set<string> valueSet){
    list<String> literals = new list<String>();
    Map<String, Schema.SObjectType> GlobalDescribeMap = Schema.getGlobalDescribe(); 
      Schema.SObjectType SObjectTypeObj = GlobalDescribeMap.get(ObjectName);
      Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe();
    Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(fieldSetName);
    String query = 'SELECT ';
        for(Schema.FieldSetMember f : fieldSetObj.getFields()){
            query += f.getFieldPath() + ', ';
        }
    for(string value : valueSet){
      literals.add('\'' + value + '\'');
    }  
        query += 'Id FROM '+objectName+' where Email IN ' + '(' + String.join(literals, ',') + ')';
    system.debug('Query**************************'+query);
    return query;
  }
  
  public static Lead mapLeadFields(Lead oldLead,Lead newLead){
    List<Schema.FieldSetMember> schemaset = SObjectType.Lead.FieldSets.Lead_Conversion_Field_Mapping.getFields();
    for(integer a=0; a<schemaset.size(); a++){
      if(oldLead.get(schemaset[a].getFieldPath())  == null || oldLead.get(schemaset[a].getFieldPath())  == ' ' || string.ValueOf(schemaset[a].getType()) == 'BOOLEAN'){
        oldLead.put(schemaset[a].getFieldPath(),newLead.get(schemaset[a].getFieldPath()));
      }
    }
    return oldLead;
  }
  
  public static Contact mapContactFields(Contact contactToUpdate,Lead newLead){
    List<Schema.FieldSetMember> schemaset = SObjectType.Contact.FieldSets.Lead_Conversion_Field_Mapping.getFields();
    for(integer a=0; a<schemaset.size(); a++){      
      if(contactToUpdate.get(schemaset[a].getFieldPath())  == null || contactToUpdate.get(schemaset[a].getFieldPath())  == ' ' || string.ValueOf(schemaset[a].getType()) == 'BOOLEAN'){
        contactToUpdate.put(schemaset[a].getFieldPath(),newLead.get(schemaset[a].getFieldPath()));
      }
    }
    return contactToUpdate;
  }   

What could be the probable issue?

TestClass:

@isTest(SeeAllData=false)
private class Class_Lead_TriggerFunction_Test {

    static testMethod void my_Class_Lead_TriggerFunction_Test(){
      
      test.startTest();  
        // TO DO: implement unit test
        Lead newLead = new Lead(
          FirstName = null,
          LastName = 'LN',
          Email = 'test@test.com',
          Company = 'Test Company',
          LeadSource = 'Test Lead Source',
          Is_Duplicate__c = false,
          Ready_To_Delete__c = false
        );
        insert newLead;
        
        Lead newLead1 = new Lead(
          FirstName = 'FN',
          LastName = 'LN',
          Email = 'test@test.com',
          Company = 'Test Company',
          LeadSource = 'Test Lead Source',
          Is_Duplicate__c = true,
          Ready_To_Delete__c = false,
          isConverted = false
        );
        insert newLead1;
        newLead1.Ready_To_Delete__c = true;
        update newLead1;
        
        test.stopTest();
    }

    static testMethod void my_Class_Contact_Dedup_Test() {
    test.startTest();
    Account testAccount = new Account(
      Name = 'Test Account'
    );
    insert testAccount;  

    Contact testContact = new Contact(
      FirstName = null,
      LastName = 'LN',
          Email = 'test1@test.com',
      AccountId = testAccount.Id      
    ); 
    insert testContact;

        // TO DO: implement unit test
        Lead newLead = new Lead(
          FirstName = 'FN',
          LastName = 'LN',
          Email = 'test1@test.com',
          Company = 'Test Company',
          LeadSource = 'Test Lead Source',
          Is_Duplicate__c = false
        );
        insert newLead;
    
        test.stopTest();
    }
}
2
  • Please include the actual trigger code that produces this error - limiting as best you can to the relevant lines. – David Reed May 13 '19 at 12:16
  • Test methods failing, code coverage low. – Richa Upadhyay May 13 '19 at 12:22
1

This method

 public static Contact mapContactFields(Contact contactToUpdate,Lead newLead){
    List<Schema.FieldSetMember> schemaset = SObjectType.Contact.FieldSets.Lead_Conversion_Field_Mapping.getFields();
    for(integer a=0; a<schemaset.size(); a++){      
      if(contactToUpdate.get(schemaset[a].getFieldPath())  == null || contactToUpdate.get(schemaset[a].getFieldPath())  == ' ' || string.ValueOf(schemaset[a].getType()) == 'BOOLEAN'){
        contactToUpdate.put(schemaset[a].getFieldPath(),newLead.get(schemaset[a].getFieldPath()));
      }
    }
    return contactToUpdate;
  }   

has only one dependency: the field set Contact.Lead_Conversion_Field_Mapping. It seems that the field mkto_si__Last_Interesting_Moment__c has been included in that field set by mistake. Removing it should cure the issue.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.