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
TNiemanTNieman 

Case beforeInsert Trigger not firing when case created in Community

I have a Case beforeInsert trigger that calls AddEntitlement method (code below).  Works fine except if case is created in the Community by a customer.  Our Customer Community is using Napili template and Lightning, would that have anything to do with it?
 
public static void AddEntitlement(list<Case> triggerCases){
		//find accounts with 2 entitlements
        list<Account> lstSpecialAcct = [SELECT Id, Name FROM Account WHERE Name = 'One' OR Name = 'Two' OR Name = 'Three'];

        map<string, Account> mapSpecialAccts = new map<string, Account>();
        for(Account a : lstSpecialAcct) {
            mapSpecialAccts.put(a.id, a);
        }
        
        list<Id> lstAccountIDs = new list<Id>();
        
        for(Case c : triggerCases) {
        //for(Case c : (List<Case>) Trigger.new) {
            if(c.AccountId != null && c.IsClosed == False && c.EntitlementId == null) {
                lstAccountIDs.add(c.AccountId);
            }
        }
        if(lstAccountIDs.size()>0) {
            list<Entitlement> lstEntitlements = [SELECT ID, Name, Status, BusinessHoursId, AccountId, StartDate, EndDate FROM Entitlement WHERE AccountID IN : lstAccountIDs AND Status = 'Active' ORDER BY Name];

            if(lstEntitlements.isEmpty()==false) {
				list<Entitlement> lstAssignedEnts = new list<Entitlement>();
				for(Case c : (List<Case>) Trigger.new) {
					if(c.EntitlementId == null && c.AccountId != null && c.IsClosed == false) {
                        lstAssignedEnts.clear();
						for(Entitlement e : lstEntitlements) {
							if(e.AccountId == c.AccountId) {
								lstAssignedEnts.add(e);
							}
						}
                        
                        if(lstAssignedEnts.isEmpty() == true){
                            system.debug('AddEntitlement Method - 0 Entitlements');
                            system.debug('  AccountId: ' + c.AccountId);
                            system.debug('  CaseId: ' + c.Id);
                            system.debug('  CaseNumber: ' + c.CaseNumber);
                        }

						if(lstAssignedEnts.size() == 1) {
                            system.debug('AddEntitlement Method - 1 Entitlement');
							c.EntitlementId = lstAssignedEnts.get(0).id;
                            c.BusinessHoursId = lstAssignedEnts.get(0).BusinessHoursId;
						}

						if(lstAssignedEnts.size() == 2 && mapSpecialAccts.containsKey(c.AccountId)) {
                             system.debug('AddEntitlement Method - 2 Entitlements');

							if(mapSpecialAccts.get(c.AccountId).Name == 'One') {
								//Category = Mobile gets Mobile Entitlement else gets Legacy entitlement
								system.debug('AddEntitlement Method - One');
                                if(c.Category__c == 'Mobile'){
                                    system.debug('AddEntitlement Method - One - Mobile is 1: ' +  lstAssignedEnts.get(1).Name);
                                 	c.EntitlementId = lstAssignedEnts.get(1).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(1).BusinessHoursID;
                            	}
                                else {
                                    system.debug('AddEntitlement Method - One - Legacy is 0: ' +  lstAssignedEnts.get(0).Name);
                                    c.EntitlementId = lstAssignedEnts.get(0).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(0).BusinessHoursID;
                                }
                            }	

                           	if(mapSpecialAccts.get(c.AccountId).Name == 'Two') {
                            	//Severity = Sev 2 and Priority = Critical gets Bespoke else gets Legacy Entitlement
                            	system.debug('AddEntitlement Method - Two');
                                if(c.Severity__c == 'Severity 2' && c.Priority == 'Critical'){
                                    system.debug('AddEntitlement Method - Two - Bespoke is 0: ' +  lstAssignedEnts.get(0).Name);
                                 	c.EntitlementId = lstAssignedEnts.get(0).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(0).BusinessHoursID;
                            	}
                                else {
                                    system.debug('AddEntitlement Method - Two - Legacy is 1: ' +  lstAssignedEnts.get(1).Name);
                                 	c.EntitlementId = lstAssignedEnts.get(1).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(1).BusinessHoursID;
                                }
                            }

                           	if(mapSpecialAccts.get(c.AccountId).Name == 'Three') {
                            	//needs a validation rule?? (ignores new cases(isNew?), only on updates)
                            	system.debug('AddEntitlement Method - Three');
                           	}
                            
                        }
					}
				}
            }
        }
    }

 
Amil_AbdallahAmil_Abdallah
You might want to provide your trigger logic as well. 
TNiemanTNieman
Trigger
 
trigger CaseTrigger on Case (before insert, before update, before delete,
                            after insert, after update, after delete, after undelete) {
    new CaseTriggerHandler().run();
}

Full handler code - based on a framework I found https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
public without sharing class CaseTriggerHandler extends TriggerHandler {
    public CaseTriggerHandler() {
        this.setMaxLoopCount(100);
    }
    
    protected override void beforeInsert() {
        system.debug('=====beforeInsert=====');
        AddEntitlement(trigger.new);
    }
    
    public static void AddEntitlement(list<Case> triggerCases){
		//find accounts with 2 entitlements
        list<Account> lstSpecialAcct = [SELECT Id, Name FROM Account WHERE Name = 'One' OR Name = 'Two' OR Name = 'Three'];

        map<string, Account> mapSpecialAccts = new map<string, Account>();
        for(Account a : lstSpecialAcct) {
            mapSpecialAccts.put(a.id, a);
        }
        
        list<Id> lstAccountIDs = new list<Id>();
        
        for(Case c : triggerCases) {
        //for(Case c : (List<Case>) Trigger.new) {
            if(c.AccountId != null && c.IsClosed == False && c.EntitlementId == null) {
                lstAccountIDs.add(c.AccountId);
            }
        }
        if(lstAccountIDs.size()>0) {
            list<Entitlement> lstEntitlements = [SELECT ID, Name, Status, BusinessHoursId, AccountId, StartDate, EndDate FROM Entitlement WHERE AccountID IN : lstAccountIDs AND Status = 'Active' ORDER BY Name];

            if(lstEntitlements.isEmpty()==false) {
				list<Entitlement> lstAssignedEnts = new list<Entitlement>();
				for(Case c : (List<Case>) Trigger.new) {
					if(c.EntitlementId == null && c.AccountId != null && c.IsClosed == false) {
                        lstAssignedEnts.clear();
						for(Entitlement e : lstEntitlements) {
							if(e.AccountId == c.AccountId) {
								lstAssignedEnts.add(e);
							}
						}
                        
                        if(lstAssignedEnts.isEmpty() == true){
                            system.debug('AddEntitlement Method - 0 Entitlements');
                            system.debug('  AccountId: ' + c.AccountId);
                            system.debug('  CaseId: ' + c.Id);
                            system.debug('  CaseNumber: ' + c.CaseNumber);
                        }

						if(lstAssignedEnts.size() == 1) {
                            system.debug('AddEntitlement Method - 1 Entitlement');
							c.EntitlementId = lstAssignedEnts.get(0).id;
                            c.BusinessHoursId = lstAssignedEnts.get(0).BusinessHoursId;
						}

						if(lstAssignedEnts.size() == 2 && mapSpecialAccts.containsKey(c.AccountId)) {
                             system.debug('AddEntitlement Method - 2 Entitlements');

							if(mapSpecialAccts.get(c.AccountId).Name == 'One') {
								//Category = Mobile gets Mobile Entitlement else gets Legacy entitlement
								system.debug('AddEntitlement Method - One');
                                if(c.Category__c == 'Mobile'){
                                    system.debug('AddEntitlement Method - One - Mobile is 1: ' +  lstAssignedEnts.get(1).Name);
                                 	c.EntitlementId = lstAssignedEnts.get(1).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(1).BusinessHoursID;
                            	}
                                else {
                                    system.debug('AddEntitlement Method - One - Legacy is 0: ' +  lstAssignedEnts.get(0).Name);
                                    c.EntitlementId = lstAssignedEnts.get(0).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(0).BusinessHoursID;
                                }
                            }	

                           	if(mapSpecialAccts.get(c.AccountId).Name == 'Two') {
                            	//Severity = Sev 2 and Priority = Critical gets Bespoke else gets Legacy Entitlement
                            	system.debug('AddEntitlement Method - Two');
                                if(c.Severity__c == 'Severity 2' && c.Priority == 'Critical'){
                                    system.debug('AddEntitlement Method - Two - Bespoke is 0: ' +  lstAssignedEnts.get(0).Name);
                                 	c.EntitlementId = lstAssignedEnts.get(0).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(0).BusinessHoursID;
                            	}
                                else {
                                    system.debug('AddEntitlement Method - Two - Legacy is 1: ' +  lstAssignedEnts.get(1).Name);
                                 	c.EntitlementId = lstAssignedEnts.get(1).Id;
                                    c.BusinessHoursId = lstAssignedEnts.get(1).BusinessHoursID;
                                }
                            }

                           	if(mapSpecialAccts.get(c.AccountId).Name == 'Three') {
                            	//needs a validation rule?? (ignores new cases(isNew?), only on updates)
                            	system.debug('AddEntitlement Method - Three');
                           	}
                            
                        }
					}
				}
            }
        }
    }

 
Amil_AbdallahAmil_Abdallah
Your trigger logic looks fine and I am familiar with the trigger framework you are using.  If I had to guess, the problem is actually due to using the trigger framework.  If your logic was in the trigger, I'm sure it would work fine, but since your logic requires an extension class you will need to grant apex class access to the community user profiles in order for the logic to fire.  You can test this by logging into the community as a full system admin and attempting to create a case and if it does work then you know it's permissions related.  See the following page for details regarding apex class access:

https://help.salesforce.com/articleView?id=users_profiles_apex_access.htm&type=0