• Mikhail Novozhilov
  • NEWBIE
  • 0 Points
  • Member since 2018
  • Integrated Control Technologies Limited

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    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
 
Hi there, my company hase the Professional Edition of Salesforse and I can't make any Apex Classes and Triggers. Here and here is written that it's possible to make a package and the install it into Professional Edition. 

I've made a package in Development Edition but when I tried to install it shows:
"
This app can't be installed.
There are problems that prevent this package from being installed.
Apex Classes(classes/AbelProductUpdateTriggerTest.cls-meta.xml) Missing feature
Installing this package requires the following feature and its associated permissions: Apex Classes
Apex Triggers(triggers/AbelProductUpdateTrigger.trigger) Missing feature
Installing this package requires the following feature and its associated permissions: Apex Triggers

"
So I can't understand is it possible or not?

Yhanks.
 
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
 
Hi there, my company hase the Professional Edition of Salesforse and I can't make any Apex Classes and Triggers. Here and here is written that it's possible to make a package and the install it into Professional Edition. 

I've made a package in Development Edition but when I tried to install it shows:
"
This app can't be installed.
There are problems that prevent this package from being installed.
Apex Classes(classes/AbelProductUpdateTriggerTest.cls-meta.xml) Missing feature
Installing this package requires the following feature and its associated permissions: Apex Classes
Apex Triggers(triggers/AbelProductUpdateTrigger.trigger) Missing feature
Installing this package requires the following feature and its associated permissions: Apex Triggers

"
So I can't understand is it possible or not?

Yhanks.