+ Start a Discussion
amateur1amateur1 

hi

this is the trigger i have written and i am getting error for 100 records as dml statements error how to rectify the error plz help me how i can rectify the error

trigger WOW_Fulfill_UpdateITQty on Fulfillement__c (before insert,after update) {


for(Fulfillement__c IFM :Trigger.new){
item__c i8=[select id,type__c from item__c where id=:ifm.item__c];

//trigger executes when the type value equals fifo
if(i8.type__c=='fifo')


{
//retrieving the IT where  warehouse and item eqauls to that of fulfilements
Inventory_Transaction__c[] IT = [select   id,
                                        Item__c,
                                        Qty_In__c,
                                       Dev_Bucket_Qty_Remaining__c,
                                        Qty_Out__c,
                                        warehouse__c
                                             from Inventory_Transaction__c 
                                             where Item__c =:IFM.Item__c AND 
                                             Warehouse__c =:IFM.warehouse__c 
                                             
                                             Order By Date__c asc];
      //AND Dev_Bucket_Qty_Remaining__c != 0
      //if there are no records then error 
        if(it.size()==0)
    {
    
    IFM.addError('Record is not available');
    }
    else
    {
  //taking sum of al the bucket qty in IT                                           
Integer count =[select Count() from Inventory_Transaction__c where Item__c =:IFM.Item__c AND 
                                             Warehouse__c =:IFM.warehouse__c ];

Decimal TOTALQTY = IFM.Qty_Out__c;
Integer i=0;
List<AggregateResult> tq=[select SUM(Dev_Bucket_Qty_Remaining__c)som1 from Inventory_Transaction__c where Item__c =:IFM.Item__c AND 
                                             Warehouse__c =:IFM.warehouse__c ];
Double doubledata =(Double)tq[0].get('som1');
integer trq =integer.valueof(doubledata);
system.debug( 'Integer value ' + trq);

if(TOTALQTY>0)
{

 if(trq>=TOTALQTY)
  {
   if(TOTALQTY>=IT[i].Dev_Bucket_Qty_Remaining__c)
   {
    Decimal j=0;
    j=TOTALQTY-IT[i].Dev_Bucket_Qty_Remaining__c;
    if(IT[i].Qty_Out__c!=null)
    {
     IT[i].Qty_Out__c=IT[i].Qty_Out__c+IT[i].Dev_Bucket_Qty_Remaining__c;
    }
    else
    {
     IT[i].Qty_Out__c=IT[i].Dev_Bucket_Qty_Remaining__c;
    }
    TOTALQTY=j;
    IT[i].Dev_Bucket_Qty_Remaining__c=0;
    upsert IT[i];
    
    //Creating fulfilement history for the updatedIT
    Fulfillement_History__c ih=new Fulfillement_History__c();
    ih.Inventory_Transaction__c=it[i].id;
  
    ih.Fulfillement__c=ifm.id;
    ih.Qty_Out__c=it[i].Qty_Out__c;
    insert ih;
    //incrementing i
    for(i=1;i<count;i++)
    {
     if(TOTALQTY>0)
     {
      if(TOTALQTY>=IT[i].Dev_Bucket_Qty_Remaining__c)
      {
       Decimal k=0;
       k=TOTALQTY-IT[i].Dev_Bucket_Qty_Remaining__c;
       if(IT[i].Qty_Out__c!=null)
        {
         IT[i].Qty_Out__c=IT[i].Qty_Out__c+IT[i].Dev_Bucket_Qty_Remaining__c;
        }
    else
       {
        IT[i].Qty_Out__c=IT[i].Dev_Bucket_Qty_Remaining__c;
       }
       
      TOTALQTY=k;
      
      IT[i].Dev_Bucket_Qty_Remaining__c=0;
      upsert IT[i];
      Fulfillement_History__c ih1=new Fulfillement_History__c();
    ih1.Inventory_Transaction__c=it[i].id;
   
    ih1.Fulfillement__c=ifm.id;
    ih1.Qty_Out__c=it[i].Qty_Out__c;
    insert ih1;
     }
    else
    {
      Decimal l=0;
      l=IT[i].Dev_Bucket_Qty_Remaining__c-TOTALQTY;
      if(IT[i].Qty_Out__c!=null)
        {
         IT[i].Qty_Out__c=IT[i].Qty_Out__c+TOTALQTY;
        }
    else
       {
        IT[i].Qty_Out__c=TOTALQTY;
       }
     IT[i].Dev_Bucket_Qty_Remaining__c=l;
     TOTALQTY=0;
     upsert IT[i];
     Fulfillement_History__c ih2=new Fulfillement_History__c();
    ih2.Inventory_Transaction__c=it[i].id;
    
    ih2.Fulfillement__c=ifm.id;
    ih2.Qty_Out__c=it[i].Qty_Out__c;
    insert ih2;
    }
   }
  }
 }
  else if(IT[i].Dev_Bucket_Qty_Remaining__c>=TOTALQTY)
  {
   Decimal m=0;
   m=IT[i].Dev_Bucket_Qty_Remaining__c-TOTALQTY;
   if(IT[i].Qty_Out__c!=null)
        {
         IT[i].Qty_Out__c=IT[i].Qty_Out__c+TOTALQTY;
        }
    else
       {
        IT[i].Qty_Out__c=TOTALQTY;
       }
     IT[i].Dev_Bucket_Qty_Remaining__c=m;
     TOTALQTY=0;
     upsert IT[i];
     Fulfillement_History__c ih3=new Fulfillement_History__c();
    ih3.Inventory_Transaction__c=it[i].id;
    
    ih3.Fulfillement__c=ifm.id;
    ih3.Qty_Out__c=it[i].Qty_Out__c;
    insert ih3;
     
    }
   }
   //error message when the quantity is more than given
   else
   {
    IFM.Invoice_Line_Item__c.addError('Quantity is more than the required Remaining Quantity for Invoice line Item ');
   }
  }
   else
   {
    IFM.addError('Quantity is more than the required Remaining Quantity');
   }
  }
  }
  
  
  //trigger executes when the type value equals serial number
  
  
 
  
}
}

 

Flight PlanFlight Plan

 

Steps to rectify the code :

1. Dont add any SOSL and DML statement in for loop (see governer limit for this)

2. Trigger code should always be bulkify refer Docs

3. In trigger always use Map, Set and List

 

In your case you can rectify the code as follow

 

 

Set<ID> itemSet = new Set<ID>();
Map<ID,item__c> itemMap= new Map<ID,item__c>();


// List<Fulfillement__c> fmList = Trigger.new;
for(Fulfillement__c IFM : fmList){
    itemSet.add(ifm.item__c);
    itemMap.put(ifm.item__c,IFM);
}

List<item__c> itemList = [select id,type__c from item__c where id in : itemSet  and type__c=='fifo'];

 

then add your logic for reteriving the Inventory_Transaction__c 

 

Hope this would help you

 

Thanx

FP

 

 

amateur1amateur1

if i do this will it reduce the dml statements

amateur1amateur1

i am ageeting error as dml statements so how can i reduce the dml staements