+ Start a Discussion
Zabi MohammedZabi Mohammed 

Test calss for trigger code cover

hi all,

  i am writhing the bellow test class to cover the code to my class this class will cover the other trigger in this we are inserting the object
when we inserting the parent object values the cheld trigger is having the other trigger its showing exception please help me on this

/**
*    Class : BondDetail Test class
**/
@isTest
class BondDetailTest{
    static testMethod void TestBondDetail() {
        TestCases_Helper.insertCustomSettingData(Branch__c.sObjectType, 'BranchCodeCustomSettings');
        Bond__c bondRec = new Bond__c();
        bondRec.Bond_Code__c = 'TESTNTPL123';
        bondRec.Bond_Status__c = Label.Bond_Inprogress_Status;
        bondRec.Issue_To_Date__c = Date.today();
        bondRec.Private_Public_Division__c = 'PO';
        insert bondRec;
        
        PageReference pRef = Page.BondDetail;
        pRef.getParameters().put('Id', bondRec.id);
        Test.setCurrentPage(pRef);
        
        Test.startTest();
            BondDetail  BD= new BondDetail();
            System.assertNotEquals(null, BD.BondID, 'Bond ID should be available.');
            System.assertEquals(BD.BondID, bondRec.id, 'ID\'s should match');
            BD.CreateAllocation();
            
            BD.back();
        Test.stopTest();
        
    }
 }

rigger PublicBondBranchAllocationTrigger  on Bond__c (before Insert, before update,after insert) {
    list<Branch_Allocation__c> BranchAllocationList=new list<Branch_Allocation__c>();
    Map<String,Branch__c> BranchMap = Branch__c.getAll();
    list<Branch__c> BranchList = BranchMap.values();
    if (Trigger.isAfter) {
    for(Bond__c bond : Trigger.new){
        /*if(bond.Private_Public_Division__c.Contains('PO')){
            for(Branch__c branch : BranchList ){
                Branch_Allocation__c branchallocation=new Branch_Allocation__c();
                    branchallocation.BondCode__c = bond.id;
                    branchallocation.BR_Code__c = branch.User_Branch_code__c;
                    branchallocation.Branch_Name__c= branch.Name;
                BranchAllocationList.add(branchallocation);
            }
        }*/
        if(bond.Private_Public_Division__c.Contains('PP')){
            for(Branch__c branch : BranchList ){
                if(bond.Coordinator_Branch_Code__c!=null){
                    if(bond.Coordinator_Branch_Code__c==branch.User_Branch_code__c){
                        Branch_Allocation__c branchallocation=new Branch_Allocation__c();
                        branchallocation.BondCode__c = bond.id;
                        branchallocation.UnitCount__c=40;
                        if(bond.UnitAmount__c != null){
                            branchallocation.Allocated_Amount__c=(bond.UnitAmount__c)*(branchallocation.UnitCount__c);
                        }
                        branchallocation.BR_Code__c = branch.User_Branch_code__c;
                        branchallocation.Branch_Name__c= branch.Name;
                        BranchAllocationList.add(branchallocation);
                    }
                }
             }
        }
    }
        system.debug('.......................'+BranchAllocationList);
        if(BranchAllocationList.size()>0){
            insert BranchAllocationList;
        }
    }
    
     
     if (Trigger.isbefore){
        Map<string, Id> RecordTypeMap = new Map<string, Id>();
        for(RecordType RT:[Select DeveloperName, id, name from RecordType where isactive = true and Sobjecttype='Bond__c']){
            if(RT.DeveloperName == 'Private_Bond'){
                RecordTypeMap.put('PP', RT.id);
            }
            else if(RT.DeveloperName == 'Public_Bond'){
                RecordTypeMap.put('PO', RT.id);
            }
        }
        
        for(Bond__c bond : Trigger.new){
           if(bond.Private_Public_Division__c == null){
               if(RecordTypeMap.containskey('PO')){
                   bond.RecordTypeId = RecordTypeMap.get('PO');
               }
           }
           if(bond.Private_Public_Division__c != null && bond.Private_Public_Division__c.Contains('PO')){
               if(RecordTypeMap.containskey('PO')){
                   bond.RecordTypeId = RecordTypeMap.get('PO');
               }
           }
           else if( bond.Private_Public_Division__c != null && bond.Private_Public_Division__c.Contains('PP')){
               if(RecordTypeMap.containskey('PP')){
                   bond.RecordTypeId = RecordTypeMap.get('PP');
               }
           }
        }
        
     }
    
}
in this Branch_Allocation__c is a chield object and  Bond__c is a parent but i am getting error like after insert no records found
Andy BoettcherAndy Boettcher
What is the exception being thrown?  The more information you provide, the easier it is for the community to help.  =)
Abhi_TripathiAbhi_Tripathi
First of all you are looping through BranchList which is empty so according to the code you posted it will never work.

Note : Please do share the error being thrown ( Thanks Andy)