You need to sign in to do that
Don't have an account?
Test Class for trigger
Hi All,
Here by i'm adding my trigger & triggerHandler class that i have written. Can anybody give me TestClass for this?Urgent reply needed.
Trigger :-
trigger CreateContractTrigger on Opportunity (after update)
{
CreateContractTriggerHandler ccth = new CreateContractTriggerHandler();
ccth.onChangeStageUpdate(trigger.new,trigger.oldMap);
}
Trigger Handler Class :-
public with sharing class CreateContractTriggerHandler
{
public List<Contract> contractsList;
public List<Opportunity> opportunityEligibleList = new List<Opportunity>();
public void onChangeStageUpdate(List<Opportunity> opportunityList, Map<Id, Opportunity> opportunityOldMap)
{
contractsList = new List<Contract>();
List<Opportunity> opportunityEligibleList = new List<Opportunity>();
Map<String,list<OpportunityLineItem>> opportunityToOppLineItemMap = new Map <String,list<OpportunityLineItem>>();
for(Opportunity opportunity : opportunityList)
{
Opportunity opportunityOld = new Opportunity();
if (opportunityOldMap.containsKey(opportunity.Id))
opportunityOld = opportunityOldMap.get(opportunity.Id);
if(opportunityOld.StageName != opportunity.StageName && opportunity.StageName =='Closed Won')
{
opportunityEligibleList.add(opportunity);
}
}
list<OpportunityLineItem> opportunityLineItemList = [select id,CreatedDate,ServiceDate,OpportunityId from OpportunityLineItem
where OpportunityId in :opportunityEligibleList];
for (OpportunityLineItem opplineItem : opportunityLineItemList)
{
if (opportunityToOppLineItemMap.containsKey(opplineItem.OpportunityId))
{
opportunityToOppLineItemMap.get(opplineItem.OpportunityId).add(opplineItem);
}
else
{
opportunityToOppLineItemMap.put(opplineItem.OpportunityId,new list<OpportunityLineItem>{opplineItem});
}
}
for(Opportunity opportunity : opportunityEligibleList)
{
if(opportunityToOppLineItemMap.containsKey(opportunity.id))
{
OpportunityLineItem oppLineItem = opportunityToOppLineItemMap.get(opportunity.id)[0];
Contract con = new Contract();
con.Opportunity__c = opportunity.Id;
con.AccountId = opportunity.AccountId;
System.debug('Contract Account Id======='+con.AccountId);
con.Status ='Draft';
con.CurrencyIsoCode =' NZD ';
con.StartDate = oppLineItem.ServiceDate;
con.Rolling_Contract__c = ' Rolling Contract ';
con.Description ='Test';
contractsList.add(con);
System.debug('contract created========');
}
}
insert contractsList;
}
}
This trigger is written to create contract on opportunity whose stage field is updated to Closed Won.
just create a testmethod in your handler class and create an Opportunity, then update the Opp. This will fire the trigger.