You need to sign in to do that
Don't have an account?

hi
can anyone help me writing test class for the following trigger below
trigger displayCommitment on Commitment__c (after insert)
{
if (Trigger.isAfter)
{
Set<String> set_string = new Set<String>();
Map<String, Commitment__c> map_pref = new Map<String,Commitment__c>();
for ( Commitment__c cmmit : Trigger.new )
{
if (cmmit.Preference__c != null)
{
set_string.add(cmmit.Preference__c);
map_pref.put(cmmit.Preference__c, cmmit);
}
}
if (set_string.size() > 0)
{
List<Opportunity> list_opportunity = new List<Opportunity>();
for (Opportunity obj : [Select Id, Preference__c From Opportunity Where Preference__c In : set_string])
{
Commitment__c cObj = map_pref.get(obj.Preference__c);
Opportunity updateOpportunity = obj;
//updateOpportunity.Commitment_Name__c = cObj.Name;
updateOpportunity.Commitment_TypePreference__c = cObj.Commitment_Type__c;
updateOpportunity.StartDate__c = cObj.Start_Date__c;
updateOpportunity.EndDate__c =cObj.End_Date__c;
updateOpportunity.Commitment_Details__c =cObj.Commitment_Details__c;
updateOpportunity.contact__c =cObj.Contact__r.name;
updateOpportunity.Status__c =cObj.Status__c;
updateOpportunity.Child__c =cObj.Child__r.name;
system.debug('<<<<<<<<test>>>>>>'+cObj.Child__r.name);
list_opportunity.add(updateOpportunity);
System.Debug('<<Sucess>>');
}
if (list_opportunity != null && list_opportunity.size() > 0) update list_opportunity;
}
}
}
Hi,
Try the below code snippet as reference:
@isTest
private class testTriggerTestCoverage
{
public static testMethod void unitTest()
{
Opportunity opp1=new Opportunity(name='test1',Preference__c='aaa',CloseDate=system.today(),StageName='Prospecting');
insert opp1;
Opportunity opp2=new Opportunity(name='test1',Preference__c='aaa',CloseDate=system.today(),StageName='Prospecting');
insert opp2;
Opportunity opp3=new Opportunity(name='test1',Preference__c='aaa',CloseDate=system.today(),StageName='Prospecting');
insert opp3;
list<Commitment__c> cc=new list<Commitment__c>();
Commitment__c c1=new Commitment__c(name='c1',Preference__c='aaa',Commitment_Type__c='',Commitment_Details__c='',Status__c='');
cc.add(c1);
Commitment__c c2=new Commitment__c(name='c1',Preference__c='aaa');
cc.add(c2);
Commitment__c c3=new Commitment__c(name='c1',Preference__c='aaa');
cc.add(c3);
Commitment__c c4=new Commitment__c(name='c1',Preference__c='aaa');
cc.add(c4);
insert cc;
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
All Answers
Hi,
Try the below code snippet as reference:
@isTest
private class testTriggerTestCoverage
{
public static testMethod void unitTest()
{
Opportunity opp1=new Opportunity(name='test1',Preference__c='aaa',CloseDate=system.today(),StageName='Prospecting');
insert opp1;
Opportunity opp2=new Opportunity(name='test1',Preference__c='aaa',CloseDate=system.today(),StageName='Prospecting');
insert opp2;
Opportunity opp3=new Opportunity(name='test1',Preference__c='aaa',CloseDate=system.today(),StageName='Prospecting');
insert opp3;
list<Commitment__c> cc=new list<Commitment__c>();
Commitment__c c1=new Commitment__c(name='c1',Preference__c='aaa',Commitment_Type__c='',Commitment_Details__c='',Status__c='');
cc.add(c1);
Commitment__c c2=new Commitment__c(name='c1',Preference__c='aaa');
cc.add(c2);
Commitment__c c3=new Commitment__c(name='c1',Preference__c='aaa');
cc.add(c3);
Commitment__c c4=new Commitment__c(name='c1',Preference__c='aaa');
cc.add(c4);
insert cc;
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thanks , it worked :)