+ Start a Discussion
selvakumar Anbazhaganselvakumar Anbazhagan 

Need Apex Test class for Trigger

Hi Everyone,

I need test calss coding for the trigger involved lead object. Can anyone please write me test class coding.

My trigger is given below.

trigger ReferralPartner_Lead on Lead (before Insert,before update) {   
    Set<String> referrerNames=new Set<String>();   
    for (Lead ld : trigger.new)
    {
        referrerNames.add(ld.Referral_Text__c);
    }
     System.debug('+++referaltext'+referrerNames);
    Map<String, Referral_Partner__c> refPtrsByName=new Map<String, Referral_Partner__c>();
    for (Referral_Partner__c refPtr : [select id, Name from Referral_Partner__c where name in :referrerNames])
    {
        refPtrsByName.put(refPtr.Name, refPtr);
    }
     System.debug('+++===referalName'+refPtrsByName);   
    for (Lead ld : trigger.new)
    {
        Referral_Partner__c refPtr=refPtrsByName.get(ld.Referral_Text__c);
        if (null!=refPtr)
        {
            ld.Referral_Partner__c=refPtr.id;
        }
       System.debug('+++++++++referalPart'+ld.Referral_Partner__c);
    }
}


Please help me on this.

Thanks in advance.

 
Best Answer chosen by selvakumar Anbazhagan
SantoshChitalkarSantoshChitalkar
Hi SelvaKumar,

Refer the following code - 

@isTest
public with sharing class TestCoverageImproverTest {
     public static testMethod void LeadCoverageTest(){
        //Insert Referral_Partner__c records, add all manadary fields here 
        Referral_Partner__c ref = new Referral_Partner__c(Name = 'Tester');
        insert ref;

        List<Lead> lstLead = new List<Lead>();
        Lead lead = new Lead(LastName='Test1', Company='Test1 Inc.', Status = 'Not Contacted',
                            Email='test6@duptest.com', Referral_Text__c = 'Tester');
        lstLead.add(lead);
        Lead lead1 = new Lead(LastName='Test2', Company='Test1 Inc.', Status = 'Not Contacted',
                            Email='test1@duptest.com', Referral_Text__c = 'Tester2');
        lstLead.add(lead1);
        try{
            insert lstLead;
        }
        catch(Exception e){}
     }
}

All Answers

PratikPratik (Salesforce Developers) 
Hi Selvakumar,

Please go through the guidelines mentioned in the below document:
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm

Thanks,
Pratik
SantoshChitalkarSantoshChitalkar
Hi SelvaKumar,

Refer the following code - 

@isTest
public with sharing class TestCoverageImproverTest {
     public static testMethod void LeadCoverageTest(){
        //Insert Referral_Partner__c records, add all manadary fields here 
        Referral_Partner__c ref = new Referral_Partner__c(Name = 'Tester');
        insert ref;

        List<Lead> lstLead = new List<Lead>();
        Lead lead = new Lead(LastName='Test1', Company='Test1 Inc.', Status = 'Not Contacted',
                            Email='test6@duptest.com', Referral_Text__c = 'Tester');
        lstLead.add(lead);
        Lead lead1 = new Lead(LastName='Test2', Company='Test1 Inc.', Status = 'Not Contacted',
                            Email='test1@duptest.com', Referral_Text__c = 'Tester2');
        lstLead.add(lead1);
        try{
            insert lstLead;
        }
        catch(Exception e){}
     }
}
This was selected as the best answer
sandeep sankhlasandeep sankhla
Hi Selvakumar,

Create a test class and create Lead and Referral_Partner__c  record where name will be same as lead Referral_Text__c  name, then it wil cover your trigger..

Referral_Partner__c objRP = new Referral_Partner__c(Name = 'Test');
insert objRP;

Lead objLead = new Lead(Referral_Text__c = 'Test');
insert objLead;
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 
SantoshChitalkarSantoshChitalkar
If this solves your problem. Mark it as solved and Choose best answer.
selvakumar Anbazhaganselvakumar Anbazhagan
Hi SantoshChitalkar,

Thanks a lot.. This test case is working fine.

I need a one more test case. can you try for it.

trigger BulkPricebookOpp on Opportunity (before insert,before update)
{   
    Id pbID = null;
    Id recordtypId = null;   
    pbID = [Select Name, Description From Pricebook2 where Name =: 'Bulk Price Book'].Id;
    recordtypId =  [Select SobjectType, Name, Id, DeveloperName From RecordType  Where SobjectType = 'Opportunity' And  Name = 'k) Bulk Services'].Id;   
   for(Opportunity obj : trigger.new)
   {
        if(obj.RecordTypeId  == recordtypId)
        {
            obj.Pricebook2Id = pbID;
        }
        else
        {
        if(Trigger.isUpdate){
                List<Pricebook2> prcbooklist = [select id,name from pricebook2];
                 Map<String,Id> prcbookMap = new Map<String,Id>();
                 Map<Id,String> cccAccmap = new Map<Id,String>();
                 Set<Opportunity> OppItrortunityUpdateIds = new Set<Opportunity>();
                 Set<Id> accountIds = new Set<Id>();                             
                if(!prcbooklist.isEmpty()){
                    for(Pricebook2 prcIter : prcbooklist){
                        prcbookMap.put(prcIter.name,prcIter.id);
                    }
                }
                System.debug('==prcmap'+prcbookMap);               
                for(Opportunity OppIdsItr : Trigger.new){
                    accountIds.add(OppIdsItr.accountid);
                    
                }
                for(Account AccItr : [SELECT id,Region__c FROM Account WHERE Id IN:accountIds]){
                    cccAccmap.put(AccItr.id,AccItr.Region__c);
                }           
                for(Opportunity OppItr : Trigger.new){
                    if(prcbookMap.size() > 0){    
                            
                        if (cccAccmap.get(OppItr.accountId) == 'P1'){
                            OppItr.Pricebook2Id = prcbookMap.get('Price Book - 1');
                        }
                        else if(cccAccmap.get(OppItr.accountId) == 'P2'){
                            OppItr.Pricebook2Id = prcbookMap.get('Price Book - 2');
                       }
                                            
                      }
                      System.debug('====assign'+OppItr.Pricebook2Id);
                }
            }
        }
   }
}