You need to sign in to do that
Don't have an account?
Salesforce2015
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
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
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
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.
It showing Code Coverage 63%. Is it ok for trigger (or) we need to cover more than 75%.
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. 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
You can deploy it into Production.