+ Start a Discussion
Azrel Rahiman 11Azrel Rahiman 11 

Apex Trigger - Test Class at 71%

Hi All,

I have created a trigger in my Sandbox to play around with Apex Trigger. 

My purpose of this code is to check few field in Account from Task, whether they met a certain requirement. The trigger works but I'm stuck with my test class with 71%.
 

New to apex coding, need help to make it at least 80% above.

APEX TRIGGER

trigger checkAccountAge on Task (before insert) {

    Set<Id> accountIds = new Set<Id>();
    Set<Id> invalidacc = new Set<Id>();  
    
    for (Task t : trigger.new)
    {
        accountIds.add(t.WhatId);
    }
    
    List<Account> accounts = [SELECT Id,Name,Market_Share_Count__c,Owner_Profile_Name__c,No_of_Operating_Rooms__c FROM Account WHERE Id IN :accountIds];

    //validation
    if(accounts.size() > 0)
    {
        for(Account acc : accounts){
            if(acc.Market_Share_Count__c == 0 &&  acc.Owner_Profile_Name__c == 'System Admin'){
                invalidacc.add(acc.Id);
            }
        }
    }
    
    System.debug('invalidacc A '+invalidacc);

    if(invalidacc.size() > 0)
    {
        for (Task t : trigger.new){
            if(invalidacc.contains(t.WhatId)){
                t.addError('Please create Market Share information and fill in No. Operating Room in Account before create a task/log a call.');
            }
        }
    }
}
TEST CLASS
@istest
public class checkAccountAge_test{
    static testMethod void testmethod1() 
    {   
        Account acc = new Account(name='test');
        acc.RecordTypeId = '01290000001M1v3';
        acc.OwnerId = '005900000013z6x';
        acc.No_of_Operating_Rooms__c = 50;
        insert acc;
        
        Share_of_Space__c sos = new Share_of_Space__c();
        sos.Account__c = acc.id;
        sos.Record_Date__c = system.Today();
        sos.Share__c = 1;
        sos.Type__c = 'EXAM';
        insert sos;
        
        Task tk = new Task();
        tk.OwnerId = '005O0000003gIAz';
        tk.Subject = 'Call:';
        tk.ActivityDate = system.Today();
        tk.Activity_Bucket__c = 'FTF End User';
        tk.Activity_Type__c = 'Inservice';
        tk.whatId = acc.id;
        tk.Priority = 'Normal';
        tk.Status = 'Not Started';
        tk.RecordTypeId = '01290000001BRWo';
        tk.Time_Spent_Minutes__c = 120;
        insert tk;
        
        tk.Status ='Completed';
        update tk;
    }
}

 
Best Answer chosen by Azrel Rahiman 11
BALAJI CHBALAJI CH
Hi Azrel,

The validation condition should be satisfied i.e., Account's Market_Share_Count__c == 0 &&  acc.Owner_Profile_Name__c == 'System Admin'
Try the Test class without creating Share_of_Space__c record for Account and give Account's Owner_Profile_Name__c as 'System Admin' (if it is not a formula field).
 
@istest
public class checkAccountAge_test{
    static testMethod void testmethod1() 
    {   
        Account acc = new Account(name='test');
        acc.RecordTypeId = '01290000001M1v3';
        acc.OwnerId = '005900000013z6x';
        acc.No_of_Operating_Rooms__c = 50;
        acc.Owner_Profile__c = 'System Admin';
        insert acc;
        
        
        Task tk = new Task();
        tk.OwnerId = '005O0000003gIAz';
        tk.Subject = 'Call:';
        tk.ActivityDate = system.Today();
        tk.Activity_Bucket__c = 'FTF End User';
        tk.Activity_Type__c = 'Inservice';
        tk.whatId = acc.id;
        tk.Priority = 'Normal';
        tk.Status = 'Not Started';
        tk.RecordTypeId = '01290000001BRWo';
        tk.Time_Spent_Minutes__c = 120;
        insert tk;
        
        tk.Status ='Completed';
        update tk;
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
 

All Answers

BALAJI CHBALAJI CH
Hi Azrel,

The validation condition should be satisfied i.e., Account's Market_Share_Count__c == 0 &&  acc.Owner_Profile_Name__c == 'System Admin'
Try the Test class without creating Share_of_Space__c record for Account and give Account's Owner_Profile_Name__c as 'System Admin' (if it is not a formula field).
 
@istest
public class checkAccountAge_test{
    static testMethod void testmethod1() 
    {   
        Account acc = new Account(name='test');
        acc.RecordTypeId = '01290000001M1v3';
        acc.OwnerId = '005900000013z6x';
        acc.No_of_Operating_Rooms__c = 50;
        acc.Owner_Profile__c = 'System Admin';
        insert acc;
        
        
        Task tk = new Task();
        tk.OwnerId = '005O0000003gIAz';
        tk.Subject = 'Call:';
        tk.ActivityDate = system.Today();
        tk.Activity_Bucket__c = 'FTF End User';
        tk.Activity_Type__c = 'Inservice';
        tk.whatId = acc.id;
        tk.Priority = 'Normal';
        tk.Status = 'Not Started';
        tk.RecordTypeId = '01290000001BRWo';
        tk.Time_Spent_Minutes__c = 120;
        insert tk;
        
        tk.Status ='Completed';
        update tk;
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
 
This was selected as the best answer
Azrel Rahiman 11Azrel Rahiman 11
Hi Balaji,

That is correct, Owner_Profile_Name__c  is a formula field.
I have removed the insert of Share of Space. When I run the Apex Test, the Test Method Passed with Status of Failed.

Now my code coverage is 100%!

Thank you for the help