+ Start a Discussion
SambitNayakSambitNayak 

Test Class on a trigger which restricts the user to enter more than 100%

Hi,
I have a custom object: Team Member. It is looking up to another custom object Vendor_Contract (Vendor Contract to Team Member is 1 to many relationship). My trigger accurately restricts users to create mulitple Team Members with total allocations exceeding above 100 % for a particular Vendor Contract. For example Vendor 1 has 3 Team Members. The allocation sum of these 3 team members cant exceed 100%.
I have written the following Test class which is unable to provide coverage. Not sure how to handle this. Any help will be definitely helpful.

 @isTest static void TestCreateNewLabContAllocMoreThan100(){
        List<Team_Member__c> tms= New List<Team_Member__c>();

        Vendor_Contract__c ven= New Vendor_Contract__c(Name='Test Vendor');
        Insert ven;

        Insert team;
        for(Integer i=0; i<200; i++){
            Team_Member__c tm= New Team_Member__c(Vendor_Contract__c=ven.id, Allocation_to_Team__c=1);
            tms.add(tm);
        }
         
        String message;
        Test.startTest();
        try{
            insert tms;
        }catch(Exception e){
            message = e.getMessage();
        }
        Test.stopTest();   
        List<Vendor_Contract__c> venCont = [SELECT Id, (SELECT Id, Allocation_to_Team__c FROM Team_Members__r) FROM Vendor_Contract__c WHERE Id =:ven.Id];
        for(Team_Member__c tm:venCont.Team_Members__r){
            system.assertEquals(tm.Allocation_to_Team__c, 1);            
        }
        
    } 
ankit bansalankit bansal
Hi Sambit,
can you also paste the code which you are trying to cover and which portion of it is you are unable to cover?
SambitNayakSambitNayak
Thanks so much Ankit. As a matter of fact, I got this resolved.
You are right. I had an issue in the Code.