• Sravan Choula 19
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi all,

The Lead object has some custom fields and when it's converting to a Contact I have to set the values of these fields to the custom fields of the Contact.

There are two options 1 - a new Contact and 2 - the already existing Contact.

For the second option I need to set the values only if they were not set previously (do not overwrite the previously set values)

I've created the Trigger:

trigger AACopyLeadFields on Lead (after update) {
Map<Id,Lead> leadStatus = new Map<Id,Lead>(); // Map of the converted Contact ID and the Lead Status

for(Lead lead : Trigger.new) {
if (lead.IsConverted) {
leadStatus.put(lead.ConvertedContactId,lead);
}
}

List<Contact> conContacts = new List<Contact>();

for ( Contact c : [select Id, LeadSource, CreatedDate, Converted_from_Lead__c from Contact WHERE Contact.Id IN :leadStatus.keySet()]) {
Lead lead = leadStatus.get(c.Id);
if(c.Converted_from_Lead__c == null) {
c.LeadSource = lead.LeadSource;
c.Lead_Source_Ext__c = lead.Lead_Source_Ext__c;
c.FirmwareEmail__c = lead.FirmwareEmail__c;
c.NewsletterEmail__c = lead.NewsletterEmail__c;
c.SoftwareEmail__c = lead.SoftwareEmail__c;
c.TrainingEventsEmail__c = lead.TrainingEventsEmail__c;
c.Converter_from_Lead__c = lead.Id;
conContacts.add(c);
}
}

if(conContacts.size() > 0) update conContacts;
}

When I convert a Lead into a new Contact (contact.CreatedDate == lead.LastModifiedDate) it works fine, but when I convert a Lead into already existed Contact it shows the error:

"
There was an error converting the lead. Please resolve the following error and try again: AACopyLeadFields: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 0037F00001BJtnnQAD; first error: UNKNOWN_EXCEPTION, java.lang.NullPointerException: [] Trigger.AACopyLeadFields: line 27, column 1
"

There is no any issue in the Partial Copy Sandbox.

Did someone have the same issue?



Thank you.

Regards, Michael Novozhilov