• Sachin Sankad.ax1650
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

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.