You need to sign in to do that
Don't have an account?
TNieman
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'); } } } } } } }
Full handler code - based on a framework I found https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices
https://help.salesforce.com/articleView?id=users_profiles_apex_access.htm&type=0