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
SamwisematrixSamwisematrix 

Help with Apex test class

I have a trigger that works very well, but I cannot come up with a class that covers more than 75%

 

Trigger UniqueTask on Task(before insert, before update){
    
    //The map... key:Id, value: list of tasks
    Map<Id, List<Task>> whatIdsMap = new Map<Id, List<Task>>();
    //For each task being inserted, updated
    for(Task t : trigger.new){
      
        if(t.whatId != null){
           
        
            if(!whatIdsMap.containsKey(t.WhatId)){//If not found
                //Create temporary list for tasks
                List<Task> temp = new List<Task>();
                //Add current task to temp list
                temp.add(t);
              
                whatIdsMap.put(t.WhatId, temp);
            }else{//If found
                //Add current task to existing list of tasks under this WhoId
                whatIdsMap.get(t.WhatId).add(t);
            }//End if else
        }//End if
    }//End for
    
  
    for(Account Acc : [Select Id,Account_Owner_Role__c, Days_with_no_Activity__c from Account where Id in :whatIdsMap.keySet()]){
        //Get the tasks for this account and iterate through them
        //Update field value
        if(acc.Days_with_no_Activity__c >120){
        for(Task t :whatIdsMap.get(acc.Id)){
            If(t.Task_Owner_Role__c==acc.Account_Owner_Role__c&&t.Status=='Completed'){
            t.Unique_Activity__c = True;
        }//End for
    }
    }
    }//End for
}//End trigger

SFDC-NOOBSFDC-NOOB

I'm by no means an expert but I think for the test class you should enter a new account and then create a new task.  You should at least get partial coverage for what you're trying to do.  Try somthing like this.

 

@isTest

Class TestUniqueTaskOnTriggerTest {

@istest static void test(){

account a = new account(Name = 'test');

insert a;

 

task t = new task(parentid=a.id, title = 'Test Task');

test.starttest();

insert t;

test.stoptest();

}

}

SamwisematrixSamwisematrix

Thanks for your responce. 

 

Its kicking back an error: Invalid field parentid for SObject Task at line 7 column 28

 

SamwisematrixSamwisematrix

I changed Parentid to Whatid and it worked fine, and i changed title to subject.  But it still is only giving me 69%, which was what my other test class was giving me. I have highlighted in Red the parts that I cant figure out how to test.

 

 

Trigger UniqueTask on Task(before insert, before update){
    
    //The map... key:Id, value: list of tasks
    Map<Id, List<Task>> whatIdsMap = new Map<Id, List<Task>>();
    //For each task being inserted, updated
    for(Task t : trigger.new){
        //If this task has a WhoId...
        if(t.whatId != null){
           
           
            if(!whatIdsMap.containsKey(t.WhatId)){//If not found
                //Create temporary list for tasks
                List<Task> temp = new List<Task>();
                //Add current task to temp list
                temp.add(t);
                
                whatIdsMap.put(t.WhatId, temp);
            }else{//If found
                
                whatIdsMap.get(t.WhatId).add(t);
            }//End if else
        }//End if
    }//End for
    
   
    for(Account Acc : [Select Id,Account_Owner_Role__c, Days_with_no_Activity__c from Account where Id in :whatIdsMap.keySet()]){
        //Get the tasks for this account and iterate through them
        //Update field value
        if(acc.Days_with_no_Activity__c >120){
        for(Task t :whatIdsMap.get(acc.Id)){
            If(t.Task_Owner_Role__c==acc.Account_Owner_Role__c&&t.Status=='Completed'){
            t.Unique_Activity__c = True;
        }//End for
    }
    }
    }//End for
}//End trigger

souvik9086souvik9086

Write it like this

 

@isTest

Class TestUniqueTaskOnTriggerTest {

@istest static void test(){

account a = new account(Name = 'test',Days_with_no_Activity__c =121);

insert a;

 

task t = new task(whatId=a.id, title = 'Test Task');

test.starttest();

insert t;

test.stoptest();

}

}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

SamwisematrixSamwisematrix

I tried that with one of my other classes, but i get an error.

 

Error: Compile Error: Field is not writeable: Account.Days_with_no_Activity__c at line 4 column 65

souvik9086souvik9086

Is Days_with_no_Activity__c this field formula field? If so please address from which values have been fetched in this field.

Then assign that field value in the test class.

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

 

SamwisematrixSamwisematrix

Thank you again.  Unfortunately, the field it pulls from is also unable to be written, and THAT field is edited by a trigger.

 

Any thoughts on a way around?