2

and thanks in advance for any suggestions, greatly appreciated!

we created a trigger that queries the accounts and creates custom invoice records for each stop on our service trucks. each route is designated with a color and a service week per month (week 1, week2, etc) so that when you pick a color and a week, any account that meets that criteria will have an invoice record created. we created workflows to push fields from the related account to each individual invoice record, but these workflows are causing trigger errors on our invoice creation trigger. any idea what to do, based on this error message?

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger Logistic_trigger_run caused an unexpected exception, contact your administrator: Logistic_trigger_run: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, A workflow or approval field update caused an error when saving this record. Contact your administrator to resolve it. Division undefined: []: Trigger.Logistic_trigger_run: line 177, column 1

Here is the trigger code for one combination of color (route) and week:

trigger Logistic_trigger_run on Run__c (after insert) {
    List<Logistic__c> ordtoinsert = new list<Logistic__c>();
    for(Run__c o: Trigger.new){
        if(o.Color__c=='Orange'){
            if(o.Week_Cycle__c =='Week 1' ){
                List<Account> toinsert = [Select id, Name from Account where run__c ='Orange' AND (Wk1__c = TRUE) ]; // 3DRobotics
                for(Account newOr: toinsert){
                    Logistic__c LogisticObj =new Logistic__c();
                    LogisticObj.Account1__c=newOr.Id;
                    LogisticObj.Date_Unloaded__c=date.today(); 
                    LogisticObj.run__c=o.id;                              

                    ordtoinsert.add(LogisticObj);
                }
               insert ordtoinsert;
                system.debug('---->'+toinsert);
  • 1
    You could have an active workflow or approval process on account for the field update.Can you please check. – SalesforceAspiration Jul 23 '14 at 14:59
  • 1
    Can you post the code around line 177 from this trigger? – Mark Pond Jul 23 '14 at 15:38
1

You are doing your insert inside of your loop still. Move it all the way out of the loop.

trigger Logistic_trigger_run on Run__c (after insert) {
  List<Logistic__c> ordtoinsert = new list<Logistic__c>();
  for(Run__c o: Trigger.new){
    if(o.Color__c=='Orange'){
      if(o.Week_Cycle__c =='Week 1' ){
        List<Account> toinsert = [Select id, Name from Account where run__c ='Orange' AND (Wk1__c = TRUE) ]; // 3DRobotics
        for(Account newOr: toinsert){
          Logistic__c LogisticObj =new Logistic__c();
          LogisticObj.Account1__c=newOr.Id;
          LogisticObj.Date_Unloaded__c=date.today(); 
          LogisticObj.run__c=o.id;                              

          ordtoinsert.add(LogisticObj);
        }

        system.debug('---->'+toinsert);
      }
    }
  }

  insert ordtoinsert;
}
  • 1
    This doesn't answer the OP's question, but you're right about the bulkification. You should also note that the query is inside a loop, too (toInsert). – sfdcfox Jul 23 '14 at 17:47
  • @sfdcfox Actually it does answer OP's question. It is about bulkification and it isn't. If there is more than a single item, it will try inserting the same item multiple times throwing an error. – dphil Jul 23 '14 at 18:03
  • 1
    "A workflow or approval field update caused an error when saving this record. Contact your administrator to resolve it. Division undefined" -- the specific problem here is that a workflow is causing an error. We have to resolve that problem before we get to the other, simpler stuff. – sfdcfox Jul 23 '14 at 18:32
1

Based on this help topic, it appears you're trying to divide by zero in a formula referenced in a field update. Check your field updates for any that update by formula within your object, and make sure they cannot divide by zero. You'll probably need to use an if statement for this:

IF(FieldB__c = 0, 0, FieldA__c / FieldB__c)
  • This answers the question, but as noted in the other answer, the posted code has additional issues to address. – Jeremy Nottingham Oct 26 '14 at 4:05

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.