You need to sign in to do that
Don't have an account?
Wayne McMahon
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?
I keep getting this error when trying to delete a record - DML statement cannot operate on trigger.new or trigger.old site:developer.salesforce.com
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?
Still get same error
Thanks,
Aman
Where do I create a future method? I'm new to Salesforce.
call this in the trigger itself probably
a slight change please see to it, I forgot to write the class name in hurry.
Thanks,
Aman Pathak
I have this solution nearly working. But the problem is the function is running 'before insert' and therefor the new lead that needs to be deleted doesn't have an id yet (id is set to null when I check). Do I need to set up another function for 'After Insert', pass the id and delete it in that function?