+ Start a Discussion
Reshmi SmijuReshmi Smiju 

Issues with Lead to member Conversion

Hi,
While I am executing the class ‘LeadtoMember’, I am facing the the following issue.

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadConversion caused an unexpected exception, contact your administrator: LeadConversion: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00Q9000000pNJH4EAO; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00Q9000000pNJH4) is currently in trigger LeadConversion, therefore it cannot recursively delete itself: []: Class.LeadtoMemberConversn.conversion: line 18, column 1

============================

The class is as follows.
public class LeadtoMemberConversn {
List<Lead> leads;
List<Member__c> members;

public LeadtoMemberConversn(List<Lead> leadList){
leads=leadList;
members= new List <Member__c>();
}
public void conversion(){
for(Lead l: leads){
Member__c member = new member__c();
member.Name = l.FirstName +” +l.LastName;
member.Email__c = l.Email;
members.add(member);
}
insert members;
List<Lead> todel= leads.deepClone(true);
delete todel;
}

public List<Member__c> getMembers(){
return members;
}
}

And the trigger is
trigger LeadConversion on Lead (before update){
List<Lead> leads= new List<Lead>();
for(Lead l:Trigger.new){
if(l.Status== ‘Member’){
leads.add(l);
}
LeadtoMemberConversn lmc= new LeadtoMemberConversn(leads);
lmc.conversion();
System.debug(‘values’+ lmc.getMembers());
}
}

While saving the Lead record’s status to Members, I am getting the error.


Thanks in Advance.
Reshmi.
Best Answer chosen by Reshmi Smiju
SaranSaran
Hi Reshmi,

Dont delete your lead records in the trigger. You can pass your lead ID to a future method. In that future method you can query the lead and delete it. Since future method is a seperate transaction it will allows you to delete.

Thanks
 

All Answers

SaranSaran
Hi Reshmi,

The error is that you are trying to delete the current lead record in the same transaction which is not possible. thats why you are getting this error.
 
Krishna SambarajuKrishna Sambaraju
What is the reason for cloning the list of leads and then deleting them?
Reshmi SmijuReshmi Smiju
Hi,
After converting to member, I just want to delete the lead set.  Since leads points to Trigger.new, SF does not  allow it to be deleted.So I treid to Cloning and then deleteing them. pls let me know, if i am doing wrong.If so, let me know, the one workaround for this.
Thanks much:)
SaranSaran
Hi Reshmi,

Try with the below trigger and class

Below is the trigger to create a new member
 
trigger LeadConversion on Lead (before update){
List<Lead> leads= new List<Lead>();
set<id> leadDeleteSet = new set<Id>();
List<Member__c> members = new list<Member__c>();

for(Lead l : Trigger.new){
	if(l.Status != trigger.oldMap.get(l.id).Status && l.status == ‘Member’){
		Member__c member = new member__c();
		member.Name = l.FirstName +” +l.LastName;
		member.Email__c = l.Email;
		members.add(member);
		leadDeleteSet.add(l.id);
	}
}

insert members;

if(leadDeleteSet.size() > 0)
	futureclass.deleteLead(leadDeleteSet);

Below is the future class to delete the converted lead
 
public class futureclass
{
    @future
    public static void deleteLead(set<id> leadId)
    {
		list<lead> leadDeleteList = new list<Lead>();
		leadDeleteList = [select id from lead where id IN: leadId];
        delete leadDeleteList;
	}
}
Hope this might help you.

Thanks
 
Reshmi SmijuReshmi Smiju
HI Saran,

Thanks for ur response. But still i am facing the issue.

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger LeadConversion caused an unexpected exception, contact your administrator: LeadConversion: execution of BeforeUpdate caused by: System.DmlException: Delete failed. First exception on row 0 with id 00Q9000000pcL9lEAE; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00Q9000000pcL9l) is currently in trigger LeadConversion, therefore it cannot recursively delete itself: []: Class.LeadtoMemberConversn.deleteLead: line 22, column 1
 * = Required InformationLead Information


Even if  the Delete code is outsde the Trigger.new loop, the error message is same. which i am not understanding.  Pls let me know your thought.

Thanks.

 
SaranSaran
Hi Reshmi,

Dont delete your lead records in the trigger. You can pass your lead ID to a future method. In that future method you can query the lead and delete it. Since future method is a seperate transaction it will allows you to delete.

Thanks
 
This was selected as the best answer
SaranSaran
I hope changing the trigger from before update to after update will solve the issue.

Thanks
Reshmi SmijuReshmi Smiju
Thank you  much Saran. Its working :)