1

I wrote a trigger and a test class to get the code coverage.

My test class is running successfully but I am getting 0% code coverage, can anyone helps me out in this issue if possible please.enter image description here

trigger:

trigger ContactCreationFromLead on Lead (After insert, before Update) {
//trigger to create contact, relationship, affiliation records from lead.
List<Lead> em = new List<Lead>();
List<Contact> conInsertList = new List<Contact>();
List<hed__Affiliation__c> affiliation = new List <hed__Affiliation__c>();
List<hed__Relationship__c> relation = new List<hed__Relationship__c>();
List<hed__Affiliation__c> affiliationListToBeInserted = new List<hed__Affiliation__c>();
List<String> listEmail = new List<String>();
List<String> listname = new List<String>();
Set<String> otherProgramsValueSet = new Set<String>();
if(trigger.isInsert || trigger.isUpdate){
for (Lead em : Trigger.new) {
    if(em.Email != null){
        listEmail.add(em.Email);
    }
    if(em.What_is_your_primary_program_of_interest__c != null ){
        listname.add(em.What_is_your_primary_program_of_interest__c);
    }
    if(em.Other_Programs_of_Interest__c != null){
        otherProgramsValueSet.addAll(em.Other_Programs_of_Interest__c.split(';'));
    }
}
}
//to catch the account Id that matches the lead primary program of interest
//account name should match with primary program of interest picklist values
List<Account> account = [SELECT Id,Name,Pardot_Program_Name__c from Account WHERE Pardot_Program_Name__c = :listname];
String record;
for(Account acc : account){
    record = acc.Id; 
}
List<Contact> cem = [SELECT Id, Email FROM Contact WHERE Email = :listEmail];
String cemail;
for(Contact ce : cem){
    cemail = ce.Email;
}
Id recordtype1 = [select Id From RecordType WHERE DeveloperName='Lead'].Id;
Id recordtype2 = [select Id From RecordType WHERE DeveloperName='Parent'].Id;
//code to create contact for student if form is filled by student
for(Lead ld : Trigger.new) {
    if (ld.Email != cemail && ld.Parent_or_guardian__c == false) {

        Contact cnt = new Contact();           
            cnt.FirstName = ld.FirstName;
            cnt.LastName = ld.LastName;
            cnt.Email = ld.Email;
            cnt.Student_Type__c = ld.Student_Type__c;
        conInsertList.add(cnt);
    }
    //code to create contacts for parent and student if form is filled by parent
    else{
        if(ld.Email != cemail && ld.Parent_or_guardian__c == true ){
            Contact cnt1 = new Contact();
            cnt1.RecordTypeId = recordtype2;
            cnt1.FirstName = ld.Parent_First_Name__c;
            cnt1.LastName = ld.Parent_Last_Name__c;
            cnt1.Email = ld.Parent_Email__c;
        conInsertList.add(cnt1);
            Contact cnt2 = new Contact();
            cnt2.RecordTypeId = recordtype1;
            cnt2.FirstName = ld.FirstName;
            cnt2.LastName = ld.LastName;
            cnt2.Email = ld.Email;
        conInsertList.add(cnt2);
        }
    }
}
if(conInsertList.size()>0){
    INSERT conInsertList;
    List<Id> conInsert = new List<Id>();
    //catch the inserted contact ids to create relationship record
    if(conInsertList.size()>1){
    for(Integer i = 0, s = conInsertList.size(); i < s; i += 2){
        //creates relation record by catching current student and parent record id's
        hed__Relationship__c hedrel = new hed__Relationship__c();
        hedrel.hed__Contact__c = conInsertList[i + 1].Id;
        hedrel.hed__RelatedContact__c = conInsertList[i].Id;
        hedrel.hed__Type__c = 'Parent';
        relation.add(hedrel);
        //creates affiliation record if filled by parent
        if(record != null || listname != null){
        hed__Affiliation__c hedaff = new hed__Affiliation__c();
        hedaff.hed__Account__c = record;
        hedaff.hed__Contact__c = conInsertList[i + 1].Id;
        hedaff.hed__Primary__c = true;
        affiliation.add(hedaff);
        }
        //creates affiliation records for other programs multi select picklist field
        if(otherProgramsValueSet != null){
        Map<String,Id> accNamesToIdsMap = new Map<String,Id>();
        for(Account acc : [select id,Pardot_Program_Name__c from account where Pardot_Program_Name__c in : otherProgramsValueSet]){
            accNamesToIdsMap.put(acc.Pardot_Program_Name__c,acc.Id);
        }
            for(Id accId : accNamesToIdsMap.values()){
              affiliationListToBeInserted.add(new hed__Affiliation__c(hed__Account__c= accId,
                                                       hed__Contact__c=conInsertList[i + 1].Id));
            }
        }
    }
    }
    else{
        //creates Affiliation record if filled by student only
        if(conInsertList.size()==1){
        for(Integer i = 0, s = conInsertList.size(); i < s; i += 2){
            if(record != null){
        hed__Affiliation__c hedaff = new hed__Affiliation__c();
        hedaff.hed__Account__c = record ;
        hedaff.hed__Contact__c = conInsertList[i].Id;
        hedaff.hed__Primary__c = true;
        affiliation.add(hedaff);
            }
            if(otherProgramsValueSet != null){
            Map<String,Id> accNamesToIdsMap = new Map<String,Id>();
            for(Account acc : [select id,Pardot_Program_Name__c from account where Pardot_Program_Name__c in : otherProgramsValueSet]){
                accNamesToIdsMap.put(acc.Pardot_Program_Name__c,acc.Id);
            }
            for(Id accId : accNamesToIdsMap.values()){
    affiliationListToBeInserted.add(new hed__Affiliation__c(hed__Account__c=accId,hed__Contact__c=conInsertList[i].Id));
}
            }
    }
    }
    }
    insert relation;
    insert affiliation;
    insert affiliationListToBeInserted;
}
LeadStatus convertStatus = [Select MasterLabel from LeadStatus where IsConverted = true limit 1];

List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
map<Id,Contact> conMap = new map<Id,Contact>();
for (Lead lead: Trigger.new) {
 if (!lead.isConverted && lead.Status == 'New' ) {
 Database.LeadConvert lc = new Database.LeadConvert();
 String opportunityName = lead.Name;

        lc.setLeadId(lead.Id);
        lc.setSendNotificationEmail(false);
        lc.setDoNotCreateOpportunity(true); 
        String currentemail = lead.Email;
        List<Contact> id1 = [SELECT Id, Email,AccountId FROM Contact WHERE Email = :currentemail AND recordtype.name = 'Lead'];
            String Idcon;
            string accname123;
             for(Contact ce1 : id1){
                      Idcon = ce1.Id;
                      accname123 = ce1.AccountId;
          }
              lc.setContactId(Idcon);
              lc.setAccountId(accname123);
              lc.setConvertedStatus(convertStatus.MasterLabel);
              leadConverts.add(lc);
        }
        }
            if (!leadConverts.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
     }
     }

TestClass:

@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 l = new Lead(LastName='Lead1', Email='Lead1@g.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 l;
    test.startTest();
    if(l.Parent_or_guardian__c=false){
    Contact c = new Contact(AccountId=acc.Id,LastName=l.LastName,Email=l.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='Lead2', Email='Lead1@g2.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,RecordTypeId='012P00000005ZDuIAM');
        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(l.id);

           leadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);

           Database.LeadConvertResult lcr = Database.convertLead(lc);
           System.assert(lcr.isSuccess());
    test.stopTest();
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy