I am writing a test class but on running I am getting the following error continuously.
System.DmlException: ConvertLead failed. First exception on row 0; first error: CANNOT_UPDATE_CONVERTED_LEAD, This lead was already converted to the contact Lead1234 on 25/07/2019.: []
Can anyone helps me out on this issue?
My apex class is below.
@isTest
public class TestContactCreationFromLead {
static testMethod void ContactCreationFromLeadtrigger(){
Account acc = new Account(Name='TestAccount',Pardot_Program_Name__c='Accounting and Finance');
insert acc;
Account acc1 = new Account(Name='TestAccount',Pardot_Program_Name__c='Disability Studies');
insert acc1;
Lead le1 = new Lead(LastName='Lead1234', Email='Lead1@gtg4.com', Status='New',Company='Test',Expected_Year_of_Enrolment__c=2028,What_is_your_primary_program_of_interest__c='Accounting and Finance',Other_Programs_of_Interest__c='Disability Studies',Parent_or_guardian__c=false);
insert le1;
test.startTest();
if(le1.Parent_or_guardian__c=false){
Contact c = new Contact(AccountId=acc.Id,LastName=le1.LastName,Email=le1.Email);
insert C;
hed__Affiliation__c hed = new hed__Affiliation__c(hed__Account__c=acc.Id,hed__Contact__c=c.Id,hed__Primary__c=true);
insert hed;
hed__Affiliation__c hed1 = new hed__Affiliation__c(hed__Account__c=acc1.Id,hed__Contact__c=c.Id,hed__Primary__c=false);
insert hed1;
}
Lead l1 = new Lead(LastName='Lead245', Email='Lead1@g234.com', Status='New',Company='Test',Expected_Year_of_Enrolment__c=2028,What_is_your_primary_program_of_interest__c='Accounting and Finance',Other_Programs_of_Interest__c='Disability Studies',Parent_First_Name__c='ParentF',Parent_Last_Name__c='ParentL',Parent_Email__c='Parentemail@g.in',Parent_or_guardian__c=true);
insert l1;
if(l1.Parent_or_guardian__c=true){
Contact c1= new Contact(AccountId=acc.Id,LastName=l1.LastName,Email=l1.Email);
insert c1;
Contact c2= new Contact(AccountId=acc.Id,LastName=l1.Parent_Last_Name__c,FirstName=l1.Parent_First_Name__c,Email=l1.Parent_Email__c);
Insert c2;
hed__Affiliation__c hed2 = new hed__Affiliation__c(hed__Account__c=acc.Id,hed__Contact__c=c1.Id,hed__Primary__c=true);
insert hed2;
hed__Affiliation__c hed3 = new hed__Affiliation__c(hed__Account__c=acc1.Id,hed__Contact__c=c1.Id,hed__Primary__c=false);
insert hed3;
hed__Relationship__c rel = new hed__Relationship__c(hed__Contact__c=c1.Id,hed__RelatedContact__c=c2.Id,hed__Type__c='Parent');
insert rel;
}
database.leadConvert lc = new database.leadConvert();
lc.setLeadId(le1.id);
leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);
String currentemail = le1.Email;
List<Contact> id1 = [SELECT Id, Email,AccountId FROM Contact WHERE Email = :currentemail];
String Idcon;
string accname123;
for(Contact ce1 : id1){
Idcon = ce1.Id;
accname123 = ce1.AccountId;
}
lc.setContactId(Idcon);
lc.setAccountId(accname123);
Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
test.stopTest();
}
}