+ Start a Discussion
Michael PuglieseMichael Pugliese 

Visual Flow - CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY

I have been working on a visual flow, launched from an account page to create cases. As an admin, I have no issues running it. However, some users are generating this error when the case is created at the end of the flow:

This error occurred when the flow tried to create records: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: CaseTrigger: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 5000V000018MPzLQAW; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Class.CaseServices.generateUniqueBase64IDs: line 58, column 1 Trigger.CaseTrigger: line 15, column 1. For details, see API Exceptions.

Here's the method from Case Services: 
public static void generateUniqueBase64IDs(List<Case> caseList)
    {
        List<Case> casesToUpdate = new List<Case>();
        for(Case inputCase: caseList)
        {
            Case c = new Case(id = inputCase.id);
            c.Base64ID__c = EncodingUtil.base64Encode(Blob.valueOf(c.id));
            casesToUpdate.add(c);
        }
        
        update casesToUpdate;
    }
And here's the case trigger:
trigger CaseTrigger on Case (after insert, before update, after update) 
{
  System.debug('\n\nGenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) = '+GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY)+'\n\n');
    if(Trigger.isBefore && Trigger.isUpdate)
    {
        if(GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) == '1')
        {
          CaseServices.addCaseTrackerAndCaseHistoryEntries(Trigger.newMap, Trigger.oldMap);
        }
    }
    if(Trigger.isAfter)
    {
        if(Trigger.isInsert)
        {
            CaseServices.generateUniqueBase64IDs(Trigger.new);
        }
        else if(Trigger.isUpdate)
        {
            if(GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) == '1')
            {
                //CaseServices.deleteCasesCreatedFromEmailWithoutThreadID(Trigger.newMap);
                CaseServices.closeAndUpdateChildCases(Trigger.newMap, Trigger.oldMap);
            }
        }
    }
    //CaseServices.closeAndUpdateChildCases(Trigger.newMap, Trigger.oldMap);
}

The user is able to create an identical case outside of a flow. 

Any ideas here? 

-Michael 
 
Best Answer chosen by Michael Pugliese
NForceNForce

Hi Michael,

You can't update Cases in after insert Case trigger. It's not the best practice. Instead,
move the service logic to before insert method.

public static void generateUniqueBase64IDs(List<Case> caseList)
{
    List<Case> casesToUpdate = new List<Case>();
    for(Case inputCase: caseList)
    {      
        inputcase.Base64ID__c = EncodingUtil.base64Encode(Blob.valueOf(c.id));
    }
}


trigger CaseTrigger on Case (after insert, before update, after update) 
{
System.debug('\n\nGenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) = '+GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY)+'\n\n');
if(Trigger.isBefore && Trigger.isUpdate)
{
    if(GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) == '1')
    {
      CaseServices.addCaseTrackerAndCaseHistoryEntries(Trigger.newMap, Trigger.oldMap);
    }

}
if(Trigger.isBefore && Trigger.isInsert){
 CaseServices.generateUniqueBase64IDs(Trigger.new);
}
if(Trigger.isAfter)
{
    
    if(Trigger.isUpdate)
    {
        if(GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) == '1')
        {
            //CaseServices.deleteCasesCreatedFromEmailWithoutThreadID(Trigger.newMap);
            CaseServices.closeAndUpdateChildCases(Trigger.newMap, Trigger.oldMap);
        }
    }
}
//CaseServices.closeAndUpdateChildCases(Trigger.newMap, Trigger.oldMap);
}

Thanks

All Answers

NForceNForce

Hi Michael,

You can't update Cases in after insert Case trigger. It's not the best practice. Instead,
move the service logic to before insert method.

public static void generateUniqueBase64IDs(List<Case> caseList)
{
    List<Case> casesToUpdate = new List<Case>();
    for(Case inputCase: caseList)
    {      
        inputcase.Base64ID__c = EncodingUtil.base64Encode(Blob.valueOf(c.id));
    }
}


trigger CaseTrigger on Case (after insert, before update, after update) 
{
System.debug('\n\nGenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) = '+GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY)+'\n\n');
if(Trigger.isBefore && Trigger.isUpdate)
{
    if(GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) == '1')
    {
      CaseServices.addCaseTrackerAndCaseHistoryEntries(Trigger.newMap, Trigger.oldMap);
    }

}
if(Trigger.isBefore && Trigger.isInsert){
 CaseServices.generateUniqueBase64IDs(Trigger.new);
}
if(Trigger.isAfter)
{
    
    if(Trigger.isUpdate)
    {
        if(GenericServices.getGeneralSettingValueForKey(Constants.CASE_TRIGGER_KEY) == '1')
        {
            //CaseServices.deleteCasesCreatedFromEmailWithoutThreadID(Trigger.newMap);
            CaseServices.closeAndUpdateChildCases(Trigger.newMap, Trigger.oldMap);
        }
    }
}
//CaseServices.closeAndUpdateChildCases(Trigger.newMap, Trigger.oldMap);
}

Thanks

This was selected as the best answer
Michael PuglieseMichael Pugliese
Hi! Thanks for the tip - the issue actually was a queue related problem that I figured out, but appreciate the best practice note as well.