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
StaciStaci 

Entitlements and Portal Users

I know Entitlements can be added to cases logged by community users automatically with code, but what about Portal users?  We are still using the portal but want to use entitlements.  Portal users are the main users that log cases.  Is this doable with the same code?
 
trigger CW_defaultEntitlement on Case (Before Insert, Before Update) {
Set<Id> acctIds = new Set<Id>();
for(Case c : Trigger.new) {
    acctIds.add(c.AccountId);
}
 

List <Entitlement> entls = [Select
e.StartDate, e.Id, e.EndDate, e.AccountId, e.AssetId
                                     From Entitlement e
                                     Where
e.AccountId in :acctIds and e.EndDate >=Today and e.StartDate <= Today];
        if(entls.isEmpty()==false){
            for(Case c : Trigger.New) {
                if(c.EntitlementId == null && c.AccountId != null){
                    for(Entitlement e:entls){
                        if(e.AccountId== c.AccountId){
                            c.EntitlementId = e.Id;
                           }
                     }
                }
             }
         }
}

 
Carlos Campillo GallegoCarlos Campillo Gallego
Hi Staci!

Cases should behave the same despite where they are created so that code should work. Anyway you could try doing this with process builder also. The only special thing you'd need to do is creating a hierarchy custom setting to store the entitlement Id so that you can reference it from your process.
Let me know if I can help you further :)

Regards
StaciStaci
Hi @Carlos Campillo Gallego

I'd rather just stick to the code.  I'm on a time crunch and I was hoping the code SF provided in the admin guide was working but its not.  I've tried this code now, which still only half works.  It goes through and it finds the correct account in the first system debug, but never hits the second debug and says Rows: 0 when it gets to the List<Entitlement> in the logs.  Any ideas?
trigger CW_defaultEntitlement on Case (Before Insert, Before Update) {
     
   List<Id> acctIds = new List<Id>();
   for (Case c: Trigger.new){
      if (c.EntitlementId == null && c.AccountId!= null){
    
         acctIds.add(c.AccountId);
      }
   }
   
   System.debug('*********************Accountssssss'+acctIds);
   
   
         List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, e.AccountId
                                     From Entitlement e
                                     Where e.AccountId in:acctIds And e.EndDate >= Today And e.StartDate <= Today];
     
         if(entls.isEmpty()==false){
            for(Case c : Trigger.New) {
                if(c.EntitlementId == null && c.AccountId != null){
                 System.debug('****************Account'+acctIds);
                    for(Entitlement e:entls){
                        if(e.AccountId== c.AccountId){
                            c.EntitlementId = e.Id;
                     }
                  } // end for
               }
            } // end for
         }
      }

 
Carlos Campillo GallegoCarlos Campillo Gallego
Have you added the entitlement to the account? You'll have to show the related list in the account page layout and add it, I think that's why you are not getting to the second debug, the query does not retrieve any entitlement related to the account.
Give it a try and tell me your progress :)

Regards
StaciStaci
@Carlos Campillo Gallego  

Yes I have the related list on the account

User-added image