function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
selva kumar 14selva kumar 14 

Need test case for the trigger

Hi everyone,

I need test cases for the both triggers. Please any one help on this.

Trigger 1 :

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);
    }
}


Trigger 2 :

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);
                }
            }
        }
   }
}



Thanks in advance.

 
ManojjenaManojjena
Hi Selva,

Have you tried with any test class ,Are you facing any issue on test class .
Please follow below points and try to start writing test class any issue please let us know .
:
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_best_practices.htm



1.Test class must start with @isTest annotation if class class version is more than 25 
2.Test environment support @testVisible,@testSetUp as well 
3.Unit test is to test particular piece of code working properly or not .
4.Unit test method takes no argument ,commit no data to database ,send no email ,flagged with testMethod keyword .
5.To deploy to production atleat 75% code coverage is requird and al test case should pass .
6.System.debug statemet ar enot counted as a part of apex code coverage .
7.Test method and test classes are not counted as a part of code coverage .
9.We should not focus on the  percentage of code coverage ,we should make sure that every use case should coverd including positive, negetive,bulk and single record .
10.Single Action -To verify that the the single record producess the correct an dexpected result .
11.Bulk action -Any apex record triger ,class or extention must be invoked for 1-200 records .
12.Positive behaviour : Test every expected behaviour occurs through every expected permutation , i,e user filled out every correctly data and not go past the limit .
13.Negetive Testcase :-Not to add future date ,Not to stecify negetive amount.
14.Restricted User :-Test whether a user with restricted access usded in yur code .
15.Conditional and ternary operator are not considered executed unless both positive and negetive branches are executed .
16.Unit test are class methods that verify whether a particular piece of code is error free or not .
17.Test class should be annoted with isTest .
18.isTest annotation with test method  is equivalant to  testMethod keyword .
19.Test method should static and no void return type .
20.Test class and method default access is private ,no matter to add access specifier .
21.classes with isTest annotation can't be a interface or enum .
22.Test method code can't be invoked by non test request .
23.Stating with salesforce API 28.0 test method can not reside inside non test classes .
24.@Testvisible annotaion to make visible private methods inside test classes.
25.Test method can not be used to test webservice call out .Instaed use call out mock .
26.You cann't  send email fron test method.
27.User,profile,organisation,AsyncApexjob,Corntrigger,RecordType,ApexClass,ApexComponent,ApexPage we can access without (seeAllData=true) .
28.SeeAllData=true will not work for API 23 version eailer .
29.Acessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
30.Create TestFactory class with isTest annotation to exclude from organisation code size limit .
31.@testSetup to create test records once in a method  and use in every test method in the test class .
32.We can run unit test for a specic class,set of classes and all classes .
33.We can run unit test by using Salesforce Standrad UI,Force.com IDE ,Console ,API.
34.Maximum number of test classes run per 24 hour of period is  not grater of 500 or 10 multiplication of test classes of your organisation.
35.As apex runs in system mode so the permission and record sharing are not taken into account . So we need to use system.runAs to enforce record sharing .
36.System.runAs will not enforce user permission or field level permission .
37.Every test to runAs count against the total number of DML issued in the process .