• Wayne McMahon
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
trigger updateLeadTest on Lead (before insert) {

     //create a map to hold emails/lead                          
    Map<String, Lead> leadMap = new Map<String, Lead>();
    Lead lead2 = new lead();

    for (Lead Lead : System.Trigger.new) {
        
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
        if ((Lead.Email != null) && (System.Trigger.isInsert || (Lead.Email != System.Trigger.oldMap.get(Lead.Id).Email))) {
        
            // Make sure another new Contact isn't also a duplicate  
    
            if (leadMap.containsKey(Lead.Email)) {
                Lead.Email.addError('Another new lead has the '+ 'same email address.');
            } else {
                leadMap.put(Lead.Email, Lead);
            }
       }
    }

    //put new emails into set
    for(Lead l:Trigger.new){
      if(l.email != null)leadMap.put(l.email,l);
    }

    list<lead> deleteList = new list<lead>();//these are only new records (b)
    list<lead> updateList = new list<lead>();//these are only existing records (a)
    
    // Using a single database query, find all the Contacts in  
    
    // the database that have the same email address as any  
    
    // of the Contacts being inserted or updated.  
    
    for (Lead lead : [SELECT Email, Phone, Company FROM Lead WHERE Email IN :leadMap.KeySet()]) {
        lead2 = leadMap.get(lead.Email);
        if(lead2.email != null && lead2.email == lead.email && lead2.Company == lead.Company){

          // newLead.Email.addError('A Lead with this email address already exists.');
          lead b = new lead();
          b = leadMap.get(lead.email);
             

          // if(lead2.Email == lead.Email){
            
               
              //lets update lead2 with some items from Lead
              //Always update the phone number from Lead if it is not null

              if(b.Phone != null)lead.Phone  = b.Phone;
                
              //update the email from b --> a ONLY if a is null and b is not, so not overwrite a
          
              //now, add a to the list to be updated with new values.
              updatelist.add(lead);
              
               [Select Id From Company__c Where Id IN :Trigger.new];
              //now, lets get rid of B
              deleteList.add(b);
            
         // }
       }else{
           
       }

  }
  if(deleteList.size() > 0){
      delete deleteList;
  }
  if(updatelist.size() > 0){
    update updateList;
 }
}

I am trying to update a lead if the email address already exists. This trigger updates the old record but I need to delete the newly created record and it gives me the 'DML statement cannot operate on trigger.new or trigger.old site:developer.salesforce.com' error.

Any ideas?

 
We have a website Moving2Canada.com in which users create an account and can update their profile. All this profile information gets pushed to a Salesforce lead. The lead's unique identifier is the user email. 

We are going launching a sister website Moving2Ireland.com. This website will too have a profile that is similar to the Moving2Canada profile and that a user will create. We want the user to be able to sign up to Moving2Ireland with the same email address as M2C. The problem is pushing the data to Salesforce. If a user already has an account on M2C and then creates one on M2I with the same email address, then all the profile data from M2C will be overwritten by M2I. We will have some fields in the lead that will be the same across both sites (e.g. date of birth, name etc) but most of the data will be unique to both sites and we would like to keep them separate 

Ideally we would have two separate leads for each account on each website but these leads would have the same email so there is the duplicate problem. The other possibility I see is to create new fields in the Lead for each unique field we want to have, so say we have a field called 'Job Title'. This field would become 'M2C Job Title' and we would create a new field called 'M2I Job Title'. But we will be launching other sister websites in the future so this solution could get very messy down the line. I think the multiple leads per unique email solution is cleaner. 

Is this a a common problem? Any help is greatly appreciated.
Thanks
 
trigger updateLeadTest on Lead (before insert) {

     //create a map to hold emails/lead                          
    Map<String, Lead> leadMap = new Map<String, Lead>();
    Lead lead2 = new lead();

    for (Lead Lead : System.Trigger.new) {
        
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
        if ((Lead.Email != null) && (System.Trigger.isInsert || (Lead.Email != System.Trigger.oldMap.get(Lead.Id).Email))) {
        
            // Make sure another new Contact isn't also a duplicate  
    
            if (leadMap.containsKey(Lead.Email)) {
                Lead.Email.addError('Another new lead has the '+ 'same email address.');
            } else {
                leadMap.put(Lead.Email, Lead);
            }
       }
    }

    //put new emails into set
    for(Lead l:Trigger.new){
      if(l.email != null)leadMap.put(l.email,l);
    }

    list<lead> deleteList = new list<lead>();//these are only new records (b)
    list<lead> updateList = new list<lead>();//these are only existing records (a)
    
    // Using a single database query, find all the Contacts in  
    
    // the database that have the same email address as any  
    
    // of the Contacts being inserted or updated.  
    
    for (Lead lead : [SELECT Email, Phone, Company FROM Lead WHERE Email IN :leadMap.KeySet()]) {
        lead2 = leadMap.get(lead.Email);
        if(lead2.email != null && lead2.email == lead.email && lead2.Company == lead.Company){

          // newLead.Email.addError('A Lead with this email address already exists.');
          lead b = new lead();
          b = leadMap.get(lead.email);
             

          // if(lead2.Email == lead.Email){
            
               
              //lets update lead2 with some items from Lead
              //Always update the phone number from Lead if it is not null

              if(b.Phone != null)lead.Phone  = b.Phone;
                
              //update the email from b --> a ONLY if a is null and b is not, so not overwrite a
          
              //now, add a to the list to be updated with new values.
              updatelist.add(lead);
              
               [Select Id From Company__c Where Id IN :Trigger.new];
              //now, lets get rid of B
              deleteList.add(b);
            
         // }
       }else{
           
       }

  }
  if(deleteList.size() > 0){
      delete deleteList;
  }
  if(updatelist.size() > 0){
    update updateList;
 }
}

I am trying to update a lead if the email address already exists. This trigger updates the old record but I need to delete the newly created record and it gives me the 'DML statement cannot operate on trigger.new or trigger.old site:developer.salesforce.com' error.

Any ideas?