Question

I would like to create after update APEX for salesforce that can take a contact and recreate it as a Lead when a field on the contact is checked true. Any help would be appreciated.

Yeah so I think i need to make a list of the contacts that need to be moved into the lead. I'm forgetting the format to grab the fields from the contact so i can map them into the lead.

public void createLead_Update(List<Contact> oldContacts, List<Contact> newContacts) {

System.debug('createLead_Update: entering trigger');

List<ID> createNewLead = new List<ID>();
Lead lead = new Lead(); 
Contact aContact = newContacts[i];


for (integer i=0; i<newContacts.size(); i++) {
// find contacts where the create lead checkbox is checked.
// on update, we care if the value is changed
Contact newValues = newContacts[i];
Contact oldValues = oldContacts[i];
if (newValues.createlead__c != oldValues.createlead__c) {
createNewLead.add(new Lead(
lead.firstName = aContact.firstName));
}
insert new lead
}
  System.debug('createLead_Update: exiting trigger');

    }
Was it helpful?

Solution

List<Lead> newLeadsList= new List<Lead>();
for (integer i=0; i<newContacts.size(); i++) {
    if (newContacts[i].createlead__c != oldContacts[i].createlead__c && newContacts[i].createlead__c ) {
        newLeadsList.add(new Lead(lead.firstName = newContacts[i].firstName));
      }
}
insert newLeadsList;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top