0

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger StockReturnTrigger caused an unexpected exception, contact your administrator: StockReturnTrigger: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0: Trigger.StockReturnTrigger: line 6, column 1

trigger StockReturnTrigger on Stock_Return__c (before insert) {
        Purchase__c pr = new Purchase__c();
        list <Stock_Return__c> srt = new list<Stock_Return__c> ();

    list<Purchase__c> PrLst = [select  Quantity__c ,Invoice_Number__c from Purchase__c 
                                   where Invoice_Number__c =:srt[0].Invoice_Details__c ]; 
            for (Stock_Return__c sr : srt){
            for(Purchase__c prs  : PrLst){
                if(sr.Invoice_Details__c ==prs.Invoice_Number__c){

                if(sr.Quantity__c >= Prs.Quantity__c ){
                    sr.adderror('Return Quantity is more Than Puchase Quantity ');
                }

            }
        }
   }
}

can any one explain me.

1
1

Below code:

list <Stock_Return__c> srt = new list<Stock_Return__c> ();

initialize a empty list and by statement

 list<Purchase__c> PrLst = [select  Quantity__c ,Invoice_Number__c from Purchase__c 
                                   where Invoice_Number__c =:srt[0].Invoice_Details__c ];

you are using index 0 of a blank list by referring to srt[0].Invoice_Details__c which will throw list exception.

2
  • how to overcome it – nunna naresh Jan 25 '17 at 7:50
  • @nunnanaresh check if list is empty and code appropriate handling – Eric Jan 25 '17 at 8:26
1

A trigger needs to reference the Trigger context to know what records it should work on. Triggers must also be coded to deal with the case where many records are involved so seeing [0] in a trigger is a warning sign that the trigger logic may not work in all cases. But where queries are done, they should be done once not once per trigger context record.

Putting those ideas together you end up with something like this (may contain typos or errors as I can't test it and I may not correctly understand your data model):

trigger StockReturnTrigger on Stock_Return__c (before insert) {

    // Gather the set of invoice numbers from the trigger context
    Set<String> invoices = new Set<String>();
    for (Stock_Return__c sr : Trigger.new) {
        if (sr.Invoice_Details__c != null) invoices.add(sr.Invoice_Details__c);
    }

    // Query the Purchase quantities as a set
    Map<String, Purchase__c> m = new Map<String, Purchase__c>();
    for (Purchase__c p : [
            select Quantity__c
            from Purchase__c
            where Invoice_Number__c in :invoices
            ]) {
        m.put(p.Invoice_Number__c, p);
    }

    // Compare the trigger context quantities with the Purchased quantities
    for (Stock_Return__c sr : Trigger.new) {
        Purchase__c p = m.get(sr.Invoice_Details__c);
        if (p != null) {
            if (sr.Quantity__c >= p.Quantity__c) {
                sr.addError('Return Quantity is more Than Purchase Quantity ');
            }
        }
    }
}

As others have said, the immediate error you are seeing is caused by trying to access the first element of a list that contains zero elements.

0

Try to use below code: Before accessing the value of a list try check whether the list is empty or not.

trigger StockReturnTrigger on Stock_Return__c (before insert) {
    Purchase__c pr = new Purchase__c();
    //I don't understand where exactly the values are populated in srt list. Because every time the list is going to be empty.
    list <Stock_Return__c> srt = new list<Stock_Return__c> ();
    if(!srt.isEmpty()) {
      list<Purchase__c> PrLst = [select  Quantity__c ,Invoice_Number__c from Purchase__c 
                               where Invoice_Number__c =:srt[0].Invoice_Details__c ];

      for (Stock_Return__c sr : srt){
        for(Purchase__c prs  : PrLst){
            if(sr.Invoice_Details__c ==prs.Invoice_Number__c){
              if(sr.Quantity__c >= Prs.Quantity__c )
                sr.adderror('Return Quantity is more Than Puchase Quantity ');    
            }
        }
      } 
    }
}
2
  • As your comment states, the list will always be empty so this doesn't get the OP very far. It also won't compile because you have changed the scope of PrLst to be just within the block you added. – Keith C Jan 25 '17 at 9:37
  • @KeithC Yeah didn't noticed on that updated the answer, for empty list there is nothing in the code which populate the value in the list so i just highlighted that point. Whereas the question that is asked is of List out of bound – RCS Jan 25 '17 at 9:44
0

You are using Empty list (srt) that's why you get this error. you need to fetch records in list. You can use trigger.new for Stock_Return__c. Try to write bulkify trigger, avoid inner for loop.

Try to use below code:

trigger StockReturnTrigger on Stock_Return__c (before insert) {

  if(trigger.isBefore && trigger.isInsert){
    Map<String,Purchase__c> Mappurch = new Map<String,Purchase__c> ();
    Set<String>invnumSet=new Set<String>();

    for(Stock_Return__c srt : trigger.new){
        if(srt.Invoice_Details__c!='')
           invnumset.add(srt.Invoice_Details__c);
    }

    if(invnumset!=null && invnumset.size()>0){
       for(Purchase__c prch:[select  Quantity__c ,Invoice_Number__c  from Purchase__c  where Invoice_Number__c In:invnumset]){
          if(prch.Invoice_Number__c!='')
              Mappurch.put(prch.Invoice_Number__c, prch);
       }
   } 

   for(Stock_Return__c sr : trigger.new){
       if(Mappurch.containskey(sr.Invoice_Details__c)){

           if(sr.Quantity__c >= Mappurch.get (sr.Invoice_Details__c).Quantity__c )
              sr.adderror('Return Quantity is more Than Puchase   Quantity ');
        }
   }

}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.