+ Start a Discussion
Scott WalkerScott Walker 

Process builder error. Really need help!

So I have an issue with a flow.  When I did a test it worked but now its not and I have no idea why.  The error message makes very little sense to me but appears to be an issue with the student field?

Can anyone help me with this:
Error element myRule_1_A1 (FlowRecordCreate).
This error occurred when the flow tried to create records: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: Meal_Transaction_AIAIAD: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 Trigger.Meal_Transaction_AIAIAD: line 24, column 1. For details, see API Exceptions.
This report lists the elements that the flow interview executed. The report is a beta feature.
We welcome your feedback on IdeaExchange.
Flow Details
Flow Name: Payment_Meals_Update_Ver_3
Type: Workflow
Version: 1
Status: Active
Flow Interview Details
Interview Label: Payment_Meals_Update_Ver_3-1_InterviewLabel
Current User: Scott Walker (00XX000000#XXXXXXX)
Start time: 4/3/2017 4:46 PM
Duration: 0 seconds
How the Interview Started
Scott Walker (00X0000000000) started the flow interview.
Some of this flow's variables were set when the interview started.
myVariable_old = null
myVariable_current = a00000000aa0000008
ASSIGNMENT: myVariable_waitStartTimeAssignment
{!myVariable_waitStartTimeVariable} Equals {!Flow.CurrentDateTime}
Result
{!myVariable_waitStartTimeVariable} = "4/3/2017 4:46 PM"
DECISION: myDecision
Executed this outcome: myRule_1
Outcome conditions: and
1. {!myVariable_current.Payment_for__c} (Lunch Billing) Equals Lunch Billing
Logic: All conditions must be true (AND)
RECORD CREATE: myRule_1_A1
Create one Meal_Transaction__c record where:
Student__c = {!myVariable_current.Student__c} (null)
Transaction_Amount__c = {!myVariable_current.Amount_Paid__c} (1)
Transaction_Date__c = {!myVariable_current.CreatedDate} (4/3/2017 4:46 PM)
Transaction_Type__c = Credit
Result
Failed to create record.

Error Occurred: This error occurred when the flow tried to create records: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: Meal_Transaction_AIAIAD: execution of BeforeInsert caused by: System.ListException: List index out of bounds: 0 Trigger.Meal_Transaction_AIAIAD: line 24, column 1. For details, see API Exceptions.

I have mapped the fields and I believe they are correct, as follows:

User-added image

the fields on the right are:

Student__c
Amount_Paid__c
CreatedDate


Any help really appreciated I really need to get this to work
 
Best Answer chosen by Scott Walker
karthikeyan perumalkarthikeyan perumal
In quick find box: type "apex classes"  and click on the that. its take you to the apex classes list page. ,There click on New button  

Copy the class code from here and past it there. then save the class. 

refer the image below. 
User-added image

For replace a trigger code: 

open your trigger. click on Edit button on that page, copy this trigger code and replace there. save it. 

hope this will help you. 

Thanks
karthik
 

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

Lets do the check firs: 
make your trigger inactive : "Trigger.Meal_Transaction_AIAIAD" 

Then try to excute process builder alone.  if its work perfectly  then below is the reason for your issue. 

may be your trigger "Trigger.Meal_Transaction_AIAIAD" and process builder insert falls in to Recursive  event. make sure your trigger run only once when the event triggerd and use trigger context variable, make some boolean value set to true. if trigger run once make that false to avoid the trigger running and running again. 

conside below example code for trigger Optimization.

Sample class code: 

https://help.salesforce.com/articleView?id=000133752&type=1
 
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Sample Trigger Code:
 
trigger updateTrigger on anyObject(after update) {

    if(checkRecursive.runOnce())
    {
    //write your code here            
    }

}

Hope this will help you. 

Thanks
karthik

 
Scott WalkerScott Walker
Hi there

Thaks so much for this.  The process builder does seem to work fine on its own.  I am not a coder (yet, im learning) but how do I update what I have to make it function as I should, I got the code above from an error report that gets sent to me in an email due to failure.  I am not entirely sure what I am doing 

Once again, thanks so much for your help here.  
karthikeyan perumalkarthikeyan perumal
Hello, 

Get this trigger code "Trigger.Meal_Transaction_AIAIAD"  and try to share it here.. i will look in to this trigger part. 

Thanks
karthik
 
Scott WalkerScott Walker
Sorry just saw this, OK I am trying to work out how t find this.
Scott WalkerScott Walker
Ok believe this is it:
trigger Meal_Transaction_AIAIAD on Meal_Transaction__c (after delete, after insert, after update, before insert) {
  if (!core_triggerUtils.bTriggersDisabled()){  
    if(!core_triggerUtils.b){        // should the recordset be processed?
      
      Set<ID> studentIDSet = new Set<ID>();
      Set<ID> schoolIDSet = new Set<ID>();
      List<ID>schoolList = new List<ID>();
      List<Meal_Transaction__c> mealList = new List<Meal_Transaction__c>();
      if(trigger.isdelete){
        mealList=trigger.old;      // mealList should equal the current records
      }
      else{
        mealList=trigger.new;      // mealList should equal the future records
      }
      Map<ID,Setup__c> setupMap = new Map<ID,Setup__c>();
      for(Meal_Transaction__c mt:mealList){
        studentIDSet.add(mt.Student__c);
      }
      for(Student__c s:[select ID, School__c from Student__c where ID IN :studentIDSet]){
        schoolIDSet.add(s.School__c);
        schoolList.add(s.School__c);
      }
      setupMap = core_SoqlUtils.getActiveSetups(schoolIDSet);
      Id setupID = setupMap.values()[0].Id;
      
      Meals_Setup__c mealSetup = [Select m.Schoolwide_Free_Breakfast__c, 
                         m.State_Reduced_Lunch_Reimbursement__c, 
                         m.State_Full_Price_Lunch_Reimbursement__c, 
                         m.State_Free_Lunch_Reimbursement__c, 
                         m.Setup__c, 
                         m.Fed_Reduced_Lunch_Reimbursement__c, 
                         m.Fed_Full_Price_Lunch_Reimbursement__c, 
                         m.Fed_Free_Lunch_Reimbursement__c, 
                         m.Additional_Reimbursement_3_Amount__c, 
                         m.Additional_Reimbursement_2_Amount__c, 
                         m.Additional_Reimbursement_1_Amount__c,
                         m.State_Reduced_Breakfast_Reimbursement__c, 
                         m.State_Full_Price_Breakfast_Reimbursement__c, 
                         m.State_Free_Breakfast_Reimbursement__c, 
                         m.Fed_Reduced_Breakfast_Reimbursement__c, 
                         m.Fed_Full_Price_Breakfast_Reimbursement__c, 
                         m.Fed_Free_Breakfast_Reimbursement__c,
                         m.Additional_Reimbursement_3_Applies_To__c, 
                         m.Additional_Reimbursement_2_Applies_To__c, 
                         m.Additional_Reimbursement_1_Applies_To__c  
                         From Meals_Setup__c m 
                         where Setup__c = :setupID 
                         AND Active__c = true Limit 1];
                         
      if(trigger.isBefore){          // trigger is executing prior to insertion of record
        Map<ID,String> codeMap = new Map<Id,String>();
        for(Meal_Transaction__c mt:trigger.new){
          if(mt.Transaction_Type__c == 'Sale'){
            codeMap.put(mt.Student__c, null);
          }
        }
        for(Student__c s:[select Id, Name, Meals_Category__c from Student__c where Id IN :codeMap.keyset()]){
          codeMap.put(s.Id, s.Meals_Category__c);
        }
        for(Meal_Transaction__c mt:trigger.new){
          mt.Cost_Type__c = codeMap.get(mt.Student__c);
        }
      }
  
      if(trigger.isDelete){          // trigger executes upon deletion
        Meals_ProcessTransactions.isafter(trigger.old, true, mealSetup, schoolList[0]);
      }
    
      if(trigger.isafter&&(trigger.isInsert||trigger.isUpdate)){      // trigger executes after insertion and is either an insert or update operation
        Meals_ProcessTransactions.isafter(trigger.new, false, mealSetup, schoolList[0]);
      }
    }
  }
}

 
karthikeyan perumalkarthikeyan perumal
Hello, 

First setp : Create a new class  using below code.
public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Replace you trigger code with below code:  take a backup of your code before replace.
 
trigger Meal_Transaction_AIAIAD on Meal_Transaction__c (after delete, after insert, after update, before insert) {

 if(checkRecursive.runOnce()) {
 
  if (!core_triggerUtils.bTriggersDisabled()){  
    if(!core_triggerUtils.b){        // should the recordset be processed?
      
      Set<ID> studentIDSet = new Set<ID>();
      Set<ID> schoolIDSet = new Set<ID>();
      List<ID>schoolList = new List<ID>();
      List<Meal_Transaction__c> mealList = new List<Meal_Transaction__c>();
      if(trigger.isdelete){
        mealList=trigger.old;      // mealList should equal the current records
      }
      else{
        mealList=trigger.new;      // mealList should equal the future records
      }
      Map<ID,Setup__c> setupMap = new Map<ID,Setup__c>();
      for(Meal_Transaction__c mt:mealList){
        studentIDSet.add(mt.Student__c);
      }
      for(Student__c s:[select ID, School__c from Student__c where ID IN :studentIDSet]){
        schoolIDSet.add(s.School__c);
        schoolList.add(s.School__c);
      }
      setupMap = core_SoqlUtils.getActiveSetups(schoolIDSet);
      Id setupID = setupMap.values()[0].Id;
      
      Meals_Setup__c mealSetup = [Select m.Schoolwide_Free_Breakfast__c, 
                         m.State_Reduced_Lunch_Reimbursement__c, 
                         m.State_Full_Price_Lunch_Reimbursement__c, 
                         m.State_Free_Lunch_Reimbursement__c, 
                         m.Setup__c, 
                         m.Fed_Reduced_Lunch_Reimbursement__c, 
                         m.Fed_Full_Price_Lunch_Reimbursement__c, 
                         m.Fed_Free_Lunch_Reimbursement__c, 
                         m.Additional_Reimbursement_3_Amount__c, 
                         m.Additional_Reimbursement_2_Amount__c, 
                         m.Additional_Reimbursement_1_Amount__c,
                         m.State_Reduced_Breakfast_Reimbursement__c, 
                         m.State_Full_Price_Breakfast_Reimbursement__c, 
                         m.State_Free_Breakfast_Reimbursement__c, 
                         m.Fed_Reduced_Breakfast_Reimbursement__c, 
                         m.Fed_Full_Price_Breakfast_Reimbursement__c, 
                         m.Fed_Free_Breakfast_Reimbursement__c,
                         m.Additional_Reimbursement_3_Applies_To__c, 
                         m.Additional_Reimbursement_2_Applies_To__c, 
                         m.Additional_Reimbursement_1_Applies_To__c  
                         From Meals_Setup__c m 
                         where Setup__c = :setupID 
                         AND Active__c = true Limit 1];
                         
      if(trigger.isBefore){          // trigger is executing prior to insertion of record
        Map<ID,String> codeMap = new Map<Id,String>();
        for(Meal_Transaction__c mt:trigger.new){
          if(mt.Transaction_Type__c == 'Sale'){
            codeMap.put(mt.Student__c, null);
          }
        }
        for(Student__c s:[select Id, Name, Meals_Category__c from Student__c where Id IN :codeMap.keyset()]){
          codeMap.put(s.Id, s.Meals_Category__c);
        }
        for(Meal_Transaction__c mt:trigger.new){
          mt.Cost_Type__c = codeMap.get(mt.Student__c);
        }
      }
  
      if(trigger.isDelete){          // trigger executes upon deletion
        Meals_ProcessTransactions.isafter(trigger.old, true, mealSetup, schoolList[0]);
      }
    
      if(trigger.isafter&&(trigger.isInsert||trigger.isUpdate)){      // trigger executes after insertion and is either an insert or update operation
        Meals_ProcessTransactions.isafter(trigger.new, false, mealSetup, schoolList[0]);
      }
    }
    }
  } // Recursive IF Ending.. 
}


Now activate your trigger and try to check your process builder test. it should work. 

Hope this will help you. 

Thanks
karthik
 
Scott WalkerScott Walker
Oh wow thanks!!! OK this is going to seem like a dumb quesiton but i know how to replace the trigger code but how do I create the class?
Scott WalkerScott Walker
Actually not sure how to replace the code either, so sorry totally new to this stuff
karthikeyan perumalkarthikeyan perumal
In quick find box: type "apex classes"  and click on the that. its take you to the apex classes list page. ,There click on New button  

Copy the class code from here and past it there. then save the class. 

refer the image below. 
User-added image

For replace a trigger code: 

open your trigger. click on Edit button on that page, copy this trigger code and replace there. save it. 

hope this will help you. 

Thanks
karthik
 
This was selected as the best answer
Scott WalkerScott Walker
OK thanks so much.  I will let you know hwo this goes!