• Balazs Belinszki 1
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies
Hi,

I'm trying to implement an APEX trigger on the Case object to automatically assign an Entitlement to the case when it is created via email-to-case.
I found a lot of sample codes but none of them is working for me.

Although I created an account and a contact belonging to it, when I create a new case per email in the trigger I always get null for the AccountId. 
Surprisingly when I open the case I can see the correct contact and account on it.

This is the code:
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
set<id> contactIds = new set<id>();
set<id> acctIds = new set<id>();
For(Case c :trigger.new) {
    system.debug('--- Contact id: '+c.ContactId);
    if(c.ContactId != null) {
        contactIds.add(c.ContactId);
        
    }
    system.debug('--- Account id: '+c.AccountId);
    if(c.AccountId != null) {
        acctIds.add(c.AccountId);
        
    }
}
    List <EntitlementContact> entlContacts = 
                [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId 
                From EntitlementContact e
                Where e.ContactId in :contactIds
                And e.Entitlement.EndDate >= Today 
                And e.Entitlement.StartDate <= Today];
        if(entlContacts.isEmpty()==false){
            for(Case c : Trigger.new){
                if(c.EntitlementId == null && c.ContactId != null){
                    for(EntitlementContact ec:entlContacts){
                        if(ec.ContactId==c.ContactId){
                            c.EntitlementId = ec.EntitlementId;
                            if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                                c.AssetId=ec.Entitlement.AssetId;
                            break;
                        }
                    } 
                }
            } 
        } else{
            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;
                                if(c.AssetId==null && e.AssetId!=null)
                                    c.AssetId=e.AssetId;
                                break;
                            }
                        } 
                    }
                } 
            }
        }
    }

In the debug log I have the following:
18:30:12:112 USER_DEBUG [5]|DEBUG|--- Contact id: 0030X00002NbGx4QAF
18:30:12:113 USER_DEBUG [10]|DEBUG|--- Account id: null

Also, if I add the Entitlement to the contact then it gets assigned correctly, but I do not want to link the Entitlements to contacts (only to accounts)
Hi,

I'm trying to implement an APEX trigger on the Case object to automatically assign an Entitlement to the case when it is created via email-to-case.
I found a lot of sample codes but none of them is working for me.

Although I created an account and a contact belonging to it, when I create a new case per email in the trigger I always get null for the AccountId. 
Surprisingly when I open the case I can see the correct contact and account on it.

This is the code:
trigger DefaultEntitlement on Case (Before Insert, Before Update) {
set<id> contactIds = new set<id>();
set<id> acctIds = new set<id>();
For(Case c :trigger.new) {
    system.debug('--- Contact id: '+c.ContactId);
    if(c.ContactId != null) {
        contactIds.add(c.ContactId);
        
    }
    system.debug('--- Account id: '+c.AccountId);
    if(c.AccountId != null) {
        acctIds.add(c.AccountId);
        
    }
}
    List <EntitlementContact> entlContacts = 
                [Select e.EntitlementId,e.ContactId,e.Entitlement.AssetId 
                From EntitlementContact e
                Where e.ContactId in :contactIds
                And e.Entitlement.EndDate >= Today 
                And e.Entitlement.StartDate <= Today];
        if(entlContacts.isEmpty()==false){
            for(Case c : Trigger.new){
                if(c.EntitlementId == null && c.ContactId != null){
                    for(EntitlementContact ec:entlContacts){
                        if(ec.ContactId==c.ContactId){
                            c.EntitlementId = ec.EntitlementId;
                            if(c.AssetId==null && ec.Entitlement.AssetId!=null)
                                c.AssetId=ec.Entitlement.AssetId;
                            break;
                        }
                    } 
                }
            } 
        } else{
            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;
                                if(c.AssetId==null && e.AssetId!=null)
                                    c.AssetId=e.AssetId;
                                break;
                            }
                        } 
                    }
                } 
            }
        }
    }

In the debug log I have the following:
18:30:12:112 USER_DEBUG [5]|DEBUG|--- Contact id: 0030X00002NbGx4QAF
18:30:12:113 USER_DEBUG [10]|DEBUG|--- Account id: null

Also, if I add the Entitlement to the contact then it gets assigned correctly, but I do not want to link the Entitlements to contacts (only to accounts)