+ Start a Discussion
Salesforce2015Salesforce2015 

Test Class for Trigger

Hi Experts,

below is my trigger, i need test class for my trigger.


trigger CreateChargeCodeAutomatically on MPM4_BASE__Milestone1_Project__c (after insert) {
    
    Set<Id> projectidWherechageCodeExist= new Set<Id>();
    Set<Id> prjIdSet= new Set<Id>();
    for(MPM4_BASE__Milestone1_Project__c prj :trigger.new){
       prjIdSet.add(prj.Id);
    }

    List<Charge_Code__c> chargeCodeList=[SELECT Id,Project__r.Id FROM Charge_Code__c WHERE ID IN :prjIdSet];
    if(chargeCodeList.size() > 0){
       for(Charge_Code__c chr :chargeCodeList){
         projectidWherechageCodeExist.add(chr.Project__c);
       }
    }

    prjIdSet.removeAll(projectidWherechageCodeExist);
    
    List<Charge_Code__c> listToCreate=new List<Charge_Code__c>();
    
    for(MPM4_BASE__Milestone1_Project__c prj :[SELECT Id FROM MPM4_BASE__Milestone1_Project__c WHERE ID IN : prjIdSet]){
       
       for(Project_Charge_Code__c chrge : [SELECT Id,Name FROM Project_Charge_Code__c ]){
            
            Charge_Code__c chrgg=new Charge_Code__c();
            chrgg.Charge_Code__c = chrge.Id;
            chrgg.Project__c = prj.Id;
            listToCreate.add(chrgg);
                   
       }
    }
    
    try{
        insert listToCreate;
    }
    catch(DmlException de){
       System.debug('@@@@SFDC::'+de);
    }

}


Thanks,
Manu
Best Answer chosen by Salesforce2015
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
 
@isTest
public class CreateChargeCodeAutomaticallyTest
{
    static testMethod void CreateChargeCodeAutoTest()
	{
		
		MPM4_BASE__Milestone1_Project__c milObj = new MPM4_BASE__Milestone1_Project__c();
			milObj.Name = 'Salesforce.com';
			// insert all Mandatory Field data
		insert milObj;

		Charge_Code__c chargeObj = new Charge_Code__c();
			chargeObj.Project__c = milObj.id;
			// insert all Mandatory Field data
		insert chargeObj;
		
		Project_Charge_Code__c projObj = new Project_Charge_Code__c();
			projObj.Name ='Test';
			// insert all Mandatory Field data
		insert projObj;
		
		Test.StartTest();

			MPM4_BASE__Milestone1_Project__c milObj1 = new MPM4_BASE__Milestone1_Project__c();
				milObj1.Name = 'Salesforce.com';
				// insert all Mandatory Field data
			insert milObj1;
		
		Test.StopTest();
		
    }
}

For test data please add all mandatory field for  MPM4_BASE__Milestone1_Project__c  ,Project_Charge_Code__c and Charge_Code__c object.
Please Remove Name field from all object if Name is autoNumber.

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
 
@isTest
public class CreateChargeCodeAutomaticallyTest
{
    static testMethod void CreateChargeCodeAutoTest()
	{
		
		MPM4_BASE__Milestone1_Project__c milObj = new MPM4_BASE__Milestone1_Project__c();
			milObj.Name = 'Salesforce.com';
			// insert all Mandatory Field data
		insert milObj;

		Charge_Code__c chargeObj = new Charge_Code__c();
			chargeObj.Project__c = milObj.id;
			// insert all Mandatory Field data
		insert chargeObj;
		
		Project_Charge_Code__c projObj = new Project_Charge_Code__c();
			projObj.Name ='Test';
			// insert all Mandatory Field data
		insert projObj;
		
		Test.StartTest();

			MPM4_BASE__Milestone1_Project__c milObj1 = new MPM4_BASE__Milestone1_Project__c();
				milObj1.Name = 'Salesforce.com';
				// insert all Mandatory Field data
			insert milObj1;
		
		Test.StopTest();
		
    }
}

For test data please add all mandatory field for  MPM4_BASE__Milestone1_Project__c  ,Project_Charge_Code__c and Charge_Code__c object.
Please Remove Name field from all object if Name is autoNumber.

 
This was selected as the best answer
Salesforce2015Salesforce2015
Hi Amit,

It showing Code Coverage 63%. Is it ok for trigger (or) we need to cover more than 75%.
Amit Chaudhary 8Amit Chaudhary 8
Hi Manu,

As per salesforce best pratice code coverage should be more the 75% but you can deploy your code with 63% also. Because limit is 75% more over all code coverage.
List<Charge_Code__c> chargeCodeList=[SELECT Id,Project__r.Id FROM Charge_Code__c WHERE ID IN :prjIdSet];
    if(chargeCodeList.size() > 0){
       for(Charge_Code__c chr :chargeCodeList){
         projectidWherechageCodeExist.add(chr.Project__c);
       }
    }
It look like above code will not execute in your trigger as in below line you are using "prjIdSet" set in "Charge_Code__c " object ID (in where condition ) but in "prjIdSet" you are storing "MPM4_BASE__Milestone1_Project__c" object id. I hope you can remove above code from your trigger.

[SELECT Id,Project__r.Id FROM Charge_Code__c WHERE ID IN :prjIdSet];


Please mark this as solution by selecting it as best answer if this solves your Test class problem, So that if anyone has this issue this post can help
 
umesh atryumesh atry
1% Coverage is sufficeint for Trigger Test class.
You can deploy it into Production.