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
ram sbram sb 

Apex Trigger Before Update Issue

Trigger not updating the records.When I Debug the Trigger.I am getting the following error like: Update failed. First exception on row 0 with id XXX; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Case_Staging: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 1 with id XXX.
Could any one please help to resolve the error? Thank you very much!
Apex Trigger
trigger Case_Staging on Case_Staging__c (before insert, before update) {

    if(Trigger.isBefore && Trigger.isUpdate)
        Case_Staging_TriggerHandler.beforeUpdate(Trigger.new, Trigger.oldMap);
        
}
Trigger Handler
public class Case_Staging_Utility {
    
    // Case Staging Statuses
    public static String CASEBUILD_STATUS() {return 'Case Build';}
    public static String APPROVED_STATUS() {return 'Approved';}
    public static String REJECTED_STATUS() {return 'Rejected';}
    
    // returns map w key of Case_Staging__c and a list of related parties
    public static Map<Id, List<Party__c>> getRelatedParties(List<Case_Staging__c> cs) {
        Map<Id, List<Party__c>> relatedPartiesByCaseStagingIdMap = new Map<Id, List<Party__c>>();
        
        for(Party__c p : [SELECT Id, Case__c, Case_Staging__c FROM Party__c WHERE Case_Staging__c IN :cs]) {
            if(relatedPartiesByCaseStagingIdMap.get(p.Case_Staging__c) == null)
                relatedPartiesByCaseStagingIdMap.put(p.Case_Staging__c, new List<Party__c>());
            // sort by case_staging__c
            List<Party__c> relatedParties = relatedPartiesByCaseStagingIdMap.get(p.Case_Staging__c);
            relatedParties.add(p);
            relatedPartiesByCaseStagingIdMap.put(p.Case_Staging__c, relatedParties);
        }
        
        return relatedPartiesByCaseStagingIdMap;
    }
    
    // returns map w key of Case_Staging__c and a list of related representative__c
    public static Map<Id, List<Representative__c>> getRelatedRepresentatives(List<Case_Staging__c> cs) {
        Map<Id, List<Representative__c>> relatedRepsByCaseStagingIdMap = new Map<Id, List<Representative__c>>();
        
        for(Representative__c r : [SELECT Id, Case__c, Case_Staging__c FROM Representative__c WHERE Case_Staging__c IN :cs]) {
            if(relatedRepsByCaseStagingIdMap.get(r.Case_Staging__c) == null)
                relatedRepsByCaseStagingIdMap.put(r.Case_Staging__c, new List<Representative__c>());
            // sort by case_staging__c
            List<Representative__c> relatedReps = relatedRepsByCaseStagingIdMap.get(r.Case_Staging__c);
            relatedReps.add(r);
            relatedRepsByCaseStagingIdMap.put(r.Case_Staging__c, relatedReps);
        }
        
        return relatedRepsByCaseStagingIdMap;
    }
    
    // returns map w key of Case_Staging__c and a list of related event_filing__c
    public static Map<Id, List<Event_Filing__c>> getRelatedEventFilings(List<Case_Staging__c> cs) {
        Map<Id, List<Event_Filing__c>> relatedEventFilingsByCaseStagingIdMap = new Map<Id, List<Event_Filing__c>>();
        
        for(Event_Filing__c ev : [SELECT Id, Case__c, Case_Staging__c FROM Event_Filing__c WHERE Case_Staging__c IN :cs]) {
            if(relatedEventFilingsByCaseStagingIdMap.get(ev.Case_Staging__c) == null)
                relatedEventFilingsByCaseStagingIdMap.put(ev.Case_Staging__c, new List<Event_Filing__c>());
            // sort by case_staging__c
            List<Event_Filing__c> relatedEventFilings = relatedEventFilingsByCaseStagingIdMap.get(ev.Case_Staging__c);
            relatedEventFilings.add(ev);
            relatedEventFilingsByCaseStagingIdMap.put(ev.Case_Staging__c, relatedEventFilings);
        }
        
        return relatedEventFilingsByCaseStagingIdMap;
    }
    
}




 
ShirishaShirisha (Salesforce Developers) 
Hi Ram,

The provided error message is not clear.So,can you please share the complete error message.Also,please capture the debug logs to see,whichis causing the issue.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
ram sbram sb

Debug log:
15:47:43.168 (2174356625)|CODE_UNIT_FINISHED|Case_Staging on Case_Staging trigger event BeforeUpdate|__sfdc_trigger/Case_Staging
15:47:43.168 (2217094286)|DML_END|[76]
15:47:43.168 (2217438499)|EXCEPTION_THROWN|[76]|System.DmlException: Update failed. First exception on row 0 with id a0Nr0000001E61xEAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Case_Staging: execution of BeforeUpdate

caused by: System.DmlException: Update failed. First exception on row 1 with id a0Wr0000004UrVREA0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Union and Employer Info Copied to Case” process failed. Give your Salesforce admin these details. An error occurred when executing a flow interview.
 Error ID: 61754093-421228 (2100134868): []

Class.Case_Staging_TriggerHandler.handleCaseBuildCases: line 58, column 1
Class.Case_Staging_TriggerHandler.beforeUpdate: line 22, column 1
Trigger.Case_Staging: line 4, column 1: []
15:47:43.168 (2218206314)|HEAP_ALLOCATE|[76]|Bytes:721
15:47:43.168 (2218349271)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id a0Nr0000001E61xEAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Case_Staging: execution of BeforeUpdate

caused by: System.DmlException: Update failed. First exception on row 1 with id a0Wr0000004UrVREA0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Union and Employer Info Copied to Case” process failed. Give your Salesforce admin these details. An error occurred when executing a flow interview.
 Error ID: 61754093-421228 (2100134868): []

Class.Case_Staging_TriggerHandler.handleCaseBuildCases: line 58, column 1
Class.Case_Staging_TriggerHandler.beforeUpdate: line 22, column 1
Trigger.Case_Staging: line 4, column 1: []

Class.DuplicateCheckCmp_Controller.convertCaseStagingToCase: line 76, column 1
15:47:43.168 (2218403604)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id a0Nr0000001E61xEAC; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Case_Staging: execution of BeforeUpdate

caused by: System.DmlException: Update failed. First exception on row 1 with id a0Wr0000004UrVREA0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Union and Employer Info Copied to Case” process failed. Give your Salesforce admin these details. An error occurred when executing a flow interview.
 Error ID: 61754093-421228 (2100134868): []

Class.Case_Staging_TriggerHandler.handleCaseBuildCases: line 58, column 1
Class.Case_Staging_TriggerHandler.beforeUpdate: line 22, column 1
Trigger.Case_Staging: line 4, column 1: []

The above post I have posted Trigger and Trigger Utility
Trigger Handler
public class Case_Staging_TriggerHandler {
    
    public static void beforeUpdate(List<Case_Staging__c> updatedCaseStagings, Map<Id, Case_Staging__c> oldCaseStagingsMap) {
        List<Case_Staging__c> caseBuildCaseStagings = new List<Case_Staging__c>();
        
        // filter triggered records to optimize performance
        for(Case_Staging__c cs : updatedCaseStagings) {
            Case_Staging__c oldCs = oldCaseStagingsMap.get(cs.Id);
            
            // case build cases
            if(cs.Case_Status__c != oldCs.Case_Status__c && cs.Case_Status__c == Case_Staging_Utility.CASEBUILD_STATUS())
                caseBuildCaseStagings.add(cs);
                
        }
        
        if(caseBuildCaseStagings.size() > 0)
            handleCaseBuildCases(caseBuildCaseStagings);
                
    }
    
    public static void handleCaseBuildCases(List<Case_Staging__c> caseBuildCaseStagings) {
        Map<Id, List<Party__c>> relatedPartiesMap = Case_Staging_Utility.getRelatedParties(caseBuildCaseStagings);
        Map<Id, List<Representative__c>> relatedRepsMap = Case_Staging_Utility.getRelatedRepresentatives(caseBuildCaseStagings);
        Map<Id, List<Event_Filing__c>> relatedEventFilingsMap = Case_Staging_Utility.getRelatedEventFilings(caseBuildCaseStagings);
        
        List<Case> newCases = new List<Case>();
        List<Party__c> updateParties = new List<Party__c>();
        List<Representative__c> updateReps = new List<Representative__c>();
        List<Event_Filing__c> updateEventFilings = new List<Event_Filing__c>();
        
        // new case from case staging
        for(Case_Staging__c cs : caseBuildCaseStagings) {
            newCases.add(Case_Utility.createNewCaseFromCaseStaging(cs));
        }
        insert newCases;
        
        // reparent the related records
        for(Case c : newCases) {
            List<Party__c> relatedParties = relatedPartiesMap.get(c.Case_Intake__c);
            if(relatedParties != null && relatedParties.size() > 0)
                updateParties.addAll((List<Party__c>)reparentRelatedRecords(c.Id, relatedParties));
            List<Representative__c> relatedReps = relatedRepsMap.get(c.Case_Intake__c);
            if(relatedReps != null && relatedReps.size() > 0)
                updateReps.addAll((List<Representative__c>)reparentRelatedRecords(c.Id, relatedReps));
            List<Event_Filing__c> relatedEventFilings = relatedEventFilingsMap.get(c.Case_Intake__c);
            if(relatedEventFilings != null && relatedEventFilings.size() > 0)
                updateEventFilings.addAll((List<Event_Filing__c>)reparentRelatedRecords(c.Id, relatedEventFilings));
        }
        
        if(updateParties.size() > 0)
            update updateParties;
        if(updateReps.size() > 0)
            update updateReps;
        if(updateEventFilings.size() > 0)
            update updateEventFilings;
    }
    
    public static List<SObject> reparentRelatedRecords(Id newCaseId, List<SObject> relatedRecords) {
        for(SObject obj : relatedRecords) {
            obj.put('Case__c', newCaseId);
        }
        return relatedRecords;
    }
    
}
ShirishaShirisha (Salesforce Developers) 
Hi Ram,

Can you please create the case with the salesforce.com support since you are getting the Gack error.We can take a look and will provide you more details.

Thank you!

Regards,
Shirisha Pathuri
Andrew GAndrew G
Hi.  
To help your troubleshooting, questions:

Is this code already in Production?  or Sandbox?

If it's Production, what changed in Production since it can't be code as it gets deployed?  Is there a new Process Builder?  Change in FLS? 

If testing in sandbox, 
Is there a Process Builder called “Union and Employer Info Copied to Case”?  If so, what happens if we deactivate the Flow and run the Trigger?

The code errors seem to point to the Event_Filings records being updated.  (noting a small descrepancy between code line numbering and the line numbers in the log).  Do these (Event_Filings) do some update which fires the Process Builder noted above?

I would also check the security model as well (both object and field) of all the objects involved in the code process.  Particularly that Event Filing object and anything affected by the process  “Union and Employer Info Copied to Case”



happy hunting
Andrew
 
ram sbram sb
Thank you Andrew and Shirisha!

This functionality was working in the production before Spring'20 platform realse. "Union and Employer Info Copied to Case" in flows. Do we have any latest updates in flows?
I can create a case to salesforce to understand latest flow updates.