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
Balazs Belinszki 1Balazs Belinszki 1 

AccountId is always null in before insert trigger

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)
KrishnaAvvaKrishnaAvva
Hi,

Please change your code from "AccountId" to "Account.Id".
Please mark this as SOLVED if it had helped you. Thanks!

Regards,
Krishna Avva
Balazs Belinszki 1Balazs Belinszki 1
Hi Krishna,

I tried your suggestion, but unfortunatelly it didn't solve the problem. I still get null for the Account Id.

Regards,
Balazs
gaurav gupta 253gaurav gupta 253
Try in AfterInsert
Balazs Belinszki 1Balazs Belinszki 1
Hi Gaurav,

If I use AfterInsert then I can see the AccountId in the debug log, but I'm not able to update the newly created Case. It fails with the bellow error:
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : DefaultEntitlement: execution of AfterInsert

caused by: System.FinalException: Record is read-only

Trigger.DefaultEntitlement: line 46, column 1


 
gaurav gupta 253gaurav gupta 253
Use @future and try to make all this as asynchronous call..