function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Casey Hardesty 10Casey Hardesty 10 

workflow error because of related trigger? Please help...

I'm running into an issue with a workflow rule I created recently. Before I get into the specifics, I did reach out to Salesforce support and their reply was the error I'm getting for the workflow rule somehow has something to do with a Trigger related to the same object. Because I don't have premium support, that was all he could help with.

So here is the details:

The Error

Subject: Salesforce workflow could not perform a pending action
Details:
Object Type: Shipping

Record: S-306653
https://na6.salesforce.com/a0T8000000B5wWY

Workflow Rules attempted: add to Shipping Delivered campaign after 10/days
https://na6.salesforce.com/01Q80000000iYVn

Workflow Actions attempted: 10 day undelivered = true
https://na6.salesforce.com/04Y800000005bDd

Error Number: 714219628-12693 (1918307596)

Workflow Rule
Evaluation Criteria: Evaluate the rule when a record is created, and any time it's edited to subsequently meet criteria
Rule Criteria
AND(!ISBLANK( Tracking_Url__c ), ISBLANK( Error_Message_textarea__c ), Status_Code__c != "D" )
Time-Dependent Workflow Action
After 10 days from Created Date, Field update - update a boolean field to True

Trigger on Shipping__c object (supposedly what is causing the issue)

trigger ShippingTrigger on Shipping__c (after insert, after update, before insert) {
       Boolean isContact = False;
       Id leadOrContactId = Null;
       List<CampaignMember> cm = new List<CampaignMember>();
       List<Lead> l = new List<Lead>();
       List<Contact> c = new List<Contact>();
       DateTime myDate = system.now();
       String createdByName = '';
       if (Trigger.isAfter && Trigger.isUpdate) {
            for (Shipping__c s: trigger.new) {
                createdByName = s.CreatedByName__c;
                System.debug(createdByName);
                if ((Trigger.oldMap.get(s.Id).Status_Code__c != 'D' && s.Status_Code__c == 'D') || (Trigger.oldMap.get(s.Id).X10_Day_Undelivered__c == False && s.X10_Day_Undelivered__c == True)) {
                     if (s.Contact__c != Null) {
                          isContact = True;
                          leadOrContactId = s.Contact__c;
                          System.debug('First If: ' + leadOrContactId);
                      } else if (s.Lead__c != Null) {
                         leadOrContactId = s.Lead__c;
                         System.debug('First Else If: ' + leadOrContactId);
               } System.debug(isContact);
               if (isContact == True) {
                      cm = [Select Id, ContactId, CampaignId FROM CampaignMember WHERE CampaignId = '70180000001FPAn' AND ContactId = :leadOrContactId];
                } else if (isContact == False) {
                      cm = [Select Id, LeadId, CampaignId FROM CampaignMember WHERE CampaignId = '70180000001FOXJ' AND LeadId = :leadOrContactId];
                }
                if (cm.size() > 0 && isContact == False && leadOrContactId != Null) {
                     l = [Select Id, Package_Delivered_Date_Time__c, Shipping_Createdby__c FROM Lead WHERE Id = :leadOrContactId LIMIT 1];
                     if (l.size() > 0) { for (Lead ld: l) {
                          ld.Package_Delivered_Date_Time__c = myDate;
                          ld.Shipping_Createdby__c = createdByName;
                      }
                Update l;
                }
             } else if (cm.size() > 0 && isContact == True && leadOrContactId != Null) {
                   c = [Select Id, Package_Delivered_Date_Time__c, Shipping_Createdby__c FROM Contact WHERE Id = :leadOrContactId LIMIT 1];
                   if (c.size() > 0) {
                      for (Contact cs: c) {
                          cs.Package_Delivered_Date_Time__c = myDate;
                          cs.Shipping_Createdby__c = createdByName;
                      }
                      Update c;
                  }
             } else if (isContact == True && leadOrContactId != Null) {
                 c = [Select Id, Package_Delivered_Date_Time__c, Shipping_Createdby__c FROM Contact WHERE Id = :leadOrContactId LIMIT 1];
                 if (c.size() > 0) {
                    for (Contact cs: c) {
                        cs.Package_Delivered_Date_Time__c = myDate;
                        cs.Shipping_Createdby__c = createdByName;
                     } Update c;
                  }
                  CampaignMember mem = new CampaignMember(campaignid = '70180000001FPAn', contactId = leadOrContactId);
                   insert mem;
               } else if (isContact == False && leadOrContactId != Null) {
                    l = [Select Id, Package_Delivered_Date_Time__c, Shipping_Createdby__c FROM Lead WHERE Id = :leadOrContactId LIMIT 1];
                    if (l.size() > 0) { for (Lead ld: l) {
                        ld.Package_Delivered_Date_Time__c = myDate;
                        ld.Shipping_Createdby__c = createdByName;
                    }
                    Update l;
                }
                CampaignMember mem = new CampaignMember(campaignid = '70180000001FOXJ', leadid = leadOrContactId);
                insert mem;
                }
             }
           }
         }
         if (Trigger.isBefore && Trigger.isInsert) {
             for (Shipping__c s: trigger.new) {
                 if (s.Material_Short_Code__c == 'Pak_NA' ) {
                   s.Mail_Class__c = 'First'; s.Parcel_Type__c = 'Parcel';
                 } else if (s.Material_Short_Code__c == 'Pak_APPT' ) {
                    s.Mail_Class__c = 'Priority';
                    s.Parcel_Type__c = 'FlatRateEnvelope';
                 } else if (s.Material_Short_Code__c == 'GC-20'){
                    s.Mail_Class__c = 'First'; s.Parcel_Type__c = 'Parcel';
                 } else {
                   s.Mail_Class__c = 'Priority';
                   s.Parcel_Type__c = 'FlatRateEnvelope';
                 }
               }
             }
             List<Gift_Card__c> toUpdate = new List<Gift_Card__c>();
             for (Shipping__c s : trigger.new) { // assumes a gift card can only be associated with 1 shipping record
                 if (s.Is_Printed__c && s.Gift_Card__c != null && (trigger.isinsert || !trigger.oldmap.get(s.Id).Is_Printed__c)) {
                  toUpdate.add(new Gift_Card__c(Id = s.Gift_Card__c, Status__c = 'Issued'));
                  }
              }
              if (!toUpdate.isEmpty()) update toUpdate;
     } 

     Ok, anybody have any ideas. Thank you in advance for any help you may provide.
Best Answer chosen by Casey Hardesty 10
jigarshahjigarshah
Casey,

Error
As per the Salesforce Help Documentation (https://help.salesforce.com/articleView?id=000199607&type=1) this means that "Typically this error means that there is some apex that would function incorrectly, or some limit that would be breached if the action was carried out."

Root Cause
This happens because, when you save a record your Timebased Workflow is activated. Time based workflows are queued and do not execute their actions immediately versus immediate workflow actions. When the workflow executes asynchronously and attempts to performs the field update, it causes your Apex trigger to execute and hit governor limits which causes the Workflow Field Update to fail and hence you recieve an email with the error message around the same.

Troubleshooting & Resolution
In order to troubleshoot this, convert the timebased workflow into an immediately executign action based workflow so as the get the exact error being encountered in Apex, which is causing the workflow action to fail. Once you know the issue, fix the Apex code and test it out and you can then convert the action based workflow into a timebased workflow.

Also, I noticed a few caveats in your Apex code which would cause governor limits ot be hit.

1. Your Apex code is not bulkified. Soql queries and DML statements such as insert etc. are being performed within a for loop. This would cause you to hit the execution governor limits for Soql and DML statement. Use collections - List, Maps, Set instead, to perform Soql queries and DML statements on a group of records rather a single record.  Read this article (https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code) to understand further on, how your Apex code can be bulkified.

2. Additionally, you have hard coded Record Ids within your code which is a bad practice and will cause your Apex code to fail, if it is deployed to another Salesforce instance since those Record Ids would not exist there. Record Ids on 2 different Salesforce instances are not the same except for a Full Copy Sandbox. Try to query the appropriate record using a name or an attribute instead of an Id.

Please do not forget to mark this thread as SOLVED and the asnwer as the BEST ANSWER if it helps resolve your issue.