Ajax (1) Apex Class (12) Apex Trigger (2) Community (2) Home Page (1) HTML (4) Integration (3) JS (7) KB (1) Label (1) Licenses (1) Listing (1) Log (1) OOPs (5) Sharing (1) Static Resource (1) Test Class (3) URI (1) Visualforce (10)

Friday 13 June 2014

Stop Recursion in Trigger

Stop Recursion:
Trigger:
------------------
trigger trigSplitSalesInvoiceLine on c2g__codaInvoiceLineItem__c (After insert)
{
  if(StopRecursion.runOnce())
   {
     SalesInvoiceLineItemTriggerHandler goToHandler = new SalesInvoiceLineItemTriggerHandler(Trigger.isExecuting,Trigger.size);
     if(Trigger.isAfter && Trigger.isInsert)
      {
        goToHandler.OnAfterInsert(trigger.new);
      }
   }

}
-----------
Class:
public with sharing class SalesInvoiceLineItemTriggerHandler
{
      private boolean isExecuting = True;
      private integer BatchSize = 0;
     
      public SalesInvoiceLineItemTriggerHandler(boolean isExecuting, integer size)
      {
         isExecuting = isExecuting;
           BatchSize = size;
      }
      public void OnAfterInsert(List<c2g__codaInvoiceLineItem__c> triggerNew)
          {
              updateUnitRateWithCordRateInsertNewSILEqualPlacentaRate(triggerNew);
          }
          
       public void updateUnitRateWithCordRateInsertNewSILEqualPlacentaRate(List<c2g__codaInvoiceLineItem__c> lstSalesInvoiceLine)
       {
        if(lstSalesInvoiceLine!=null && lstSalesInvoiceLine.size()>0)
          {
            List<c2g__codaInvoiceLineItem__c > lstInvoiceLine=new List<c2g__codaInvoiceLineItem__c >();
            List<c2g__codaInvoiceLineItem__c > lstInvoiceLine2=new List<c2g__codaInvoiceLineItem__c >();
            for(c2g__codaInvoiceLineItem__c objInvoiceLine:lstSalesInvoiceLine)
               {
               if(objInvoiceLine.Cl_Cord_Rate__c!=null)
                {   
                 c2g__codaInvoiceLineItem__c objIL=new c2g__codaInvoiceLineItem__c(id =objInvoiceLine.id );
                 objIL.c2g__UnitPrice__c=objInvoiceLine.Cl_Cord_Rate__c;
                 objIL.c2g__Product__c=objInvoiceLine.c2g__Product__c;
                 objIL.c2g__Quantity__c=objInvoiceLine.c2g__Quantity__c;
                 lstInvoiceLine.add(objIL);
                }
               }
            for(c2g__codaInvoiceLineItem__c objInvoiceLine:lstSalesInvoiceLine)
               {
               if(objInvoiceLine.Cl_Placenta_Rate__c!=null)
                {
                c2g__codaInvoiceLineItem__c objNewIL=new c2g__codaInvoiceLineItem__c ();
                objNewIL.c2g__UnitPrice__c=objInvoiceLine.Cl_Placenta_Rate__c;
                objNewIL.c2g__Invoice__c=objInvoiceLine.c2g__Invoice__c;
                objNewIL.c2g__OwnerCompany__c=objInvoiceLine.c2g__OwnerCompany__c;
                objNewIL.c2g__Product__c=objInvoiceLine.c2g__Product__c;
                objNewIL.ffbilling__DeriveUnitPriceFromProduct__c=objInvoiceLine.ffbilling__DeriveUnitPriceFromProduct__c;
                lstInvoiceLine2.add(objNewIL);
               }
               }
            if(lstInvoiceLine.size()>0 && lstInvoiceLine2.size()>0)
            {   
            update lstInvoiceLine;
            insert lstInvoiceLine2;
            }
         }
 
       
     }
}

-----------------
Recursion Class:
public Class StopRecursion
{
private static boolean run = true;
public static boolean runOnce()
 {
    if(run)
    {
      run=false;
      return true;
    }
    else
    {
        return run;
    }
 }


}

No comments:

Post a Comment