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
AdminuserAdminuser 

I need test class following trigger

Hi,
can any one help test class for following trigger
trigger CaseMilestone on Case (after update) {
    try{        
        set <Id> caseIds =new  set <Id>();
        list<Commision__c> Comlist =new  list<Commision__c>();        
        for(case c:trigger.new)
        {
            if(c.Status == 'closed' && c.Milestone_Approval_Status__c ==TRUE){
                caseIds.add(c.id);  
            }
        } 
        system.debug('caseIds :'+caseIds);
        
        List<CaseMilestone> cmsToUpdate = [select Id, completionDate,MilestoneTypeId,CreatedById,BusinessHoursId,CaseId,ElapsedTimeInDays,ElapsedTimeInHrs,IsCompleted,IsViolated  from CaseMilestone cm  where caseId in :caseIds /* and cm.MilestoneType.Name=:Milestone2 */ and completionDate != null limit 1];
        system.debug('cmsToUpdate :'+cmsToUpdate); 
        for(CaseMilestone cm :cmsToUpdate)
        {
            Commision__c com = new Commision__c();
            com.Case__c = cm.CaseId;
            com.OwnerId =cm.CreatedById;
            com.Owner__c=cm.CreatedById;
            com.IsCompleted__c=cm.IsCompleted;
            com.CompletionDate__c=cm.completionDate;   
            com.Milestone_Name__c=cm.MilestoneTypeId;
            Comlist.add(com);
        }
        insert Comlist;
           
        system.debug('Comlist :'+Comlist); 
    }
    catch (Exception e){
        system.debug('Exeption ::'+e + 'Line Number ::' +e.getLineNumber() + 'getCause ::' +e.getCause()+ 'getMessage ::'+e.getMessage());
    }
}

Thanks,
Apoorv Saxena 4Apoorv Saxena 4
Hi Mahesh,

Try the following code :
 
@isTest

public class CaseMilestoneTest{
	static testMethod void myTest(){
		Case c = new Case();
		c.Status = 'closed';
		c.Milestone_Approval_Status__c = TRUE;
		c.Origin = 'Phone';
		/* 
		Set all required fields to insert a case record.
		*/
		insert c;
	}
}

Hope this helps!

Please mark this question as Solved if you think your question was answered, so that others could view it as a proper solution.

Thanks,
Apoorv
AdminuserAdminuser
Thank you for replay but I need test class for 'CaseMilestone' ...
Beacuse this fallowing part is not covering
    List<CaseMilestone> cmsToUpdate = [select Id, completionDate,MilestoneTypeId,CreatedById,BusinessHoursId,CaseId,ElapsedTimeInDays,ElapsedTimeInHrs,IsCompleted,IsViolated  from CaseMilestone cm  where caseId in :caseIds /* and cm.MilestoneType.Name=:Milestone2 */ and completionDate != null limit 1];
        system.debug('cmsToUpdate :'+cmsToUpdate); 
        for(CaseMilestone cm :cmsToUpdate)
        {
            Commision__c com = new Commision__c();
            com.Case__c = cm.CaseId;
            com.OwnerId =cm.CreatedById;
            com.Owner__c=cm.CreatedById;
            com.IsCompleted__c=cm.IsCompleted;
            com.CompletionDate__c=cm.completionDate;   
            com.Milestone_Name__c=cm.MilestoneTypeId;
            Comlist.add(com);
        }
        insert Comlist;


Thanks,
Pankaj PPankaj P

Hi Mahesh,
You have already got solution for your test class above, here is just a suggestion from my side ono your code to make it a bit better.

With trigger.new list you get all data for those records so you don't need to query it again. you can directly run only one for loop on trigger.new list and as per conditions then process further. here you save one query and one for loop which in turns affects processing time and salesforce query limits when you have multiple triggers or workflows on the same object. Also always have asserts in test methods so that when your functionality grows you get to know what imact of new changes is on existing functionality.
Please see changes below : 

 

trigger CaseMilestone on Case (after update) {
    try{        
        list<Commision__c> Comlist =new  list<Commision__c>();        
        for(case c:trigger.new)
        {
            if(c.Status == 'closed' && c.Milestone_Approval_Status__c ==TRUE){
                Commision__c com = new Commision__c();
                com.Case__c = c.CaseId;
                com.OwnerId = c.CreatedById;
                com.Owner__c = c.CreatedById;
                com.IsCompleted__c = c.IsCompleted;
                com.CompletionDate__c = c.completionDate;   
                com.Milestone_Name__c = c.MilestoneTypeId;
                Comlist.add(com);
            }
        } 
        // always have a null/blank check before DML operations
        if (Comlist.size() > 0 ) {
            insert Comlist;  
        }
        system.debug('Comlist :'+Comlist); 
    }
    catch (Exception e){
        system.debug('Exeption ::'+e + 'Line Number ::' +e.getLineNumber() + 'getCause ::' +e.getCause()+ 'getMessage ::'+e.getMessage());
    }
}

Thanks,
Pankaj.