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
Vinicius PassosVinicius Passos 

Help needed for auto associate entitlement to email-to-case

Hi, I'm not a Salesforce Developer but I'm working in apex code to automatically associate an entitlement to email-to case.

I found this code below, but I'm facing some problems

In line 5: Variable does not exist: contactIds
In line 25: Variable does not exist: accId

trigger DefaultEntitlement on Case (Before Insert) {
    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 :accId 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;
                            }
                        } 
                    }
                } 
            }
        }
    }
Best Answer chosen by Vinicius Passos
Prakash NawalePrakash Nawale
Vinicius Passos,
Please try with below code.
trigger DefaultEntitlement on Case (Before Insert) {

		set<Id> contactIds = new set<Id>();
		set<Id> accId = new set<Id>();
         for(Case c : Trigger.new){
				if(c.contactId !=null){
					contactIds.add(c.contactId);
					}
				if(c.AccountId !=null){
					accId.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 :accId 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;
                            }
                        } 
                    }
                } 
            }
        }
    }

Please let me know incase if you need any assistnace.

All Answers

Prakash NawalePrakash Nawale
Vinicius Passos,
Please try with below code.
trigger DefaultEntitlement on Case (Before Insert) {

		set<Id> contactIds = new set<Id>();
		set<Id> accId = new set<Id>();
         for(Case c : Trigger.new){
				if(c.contactId !=null){
					contactIds.add(c.contactId);
					}
				if(c.AccountId !=null){
					accId.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 :accId 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;
                            }
                        } 
                    }
                } 
            }
        }
    }

Please let me know incase if you need any assistnace.
This was selected as the best answer
Vinicius PassosVinicius Passos

Hi Prakash Nawale, I did the changes that you mentioned and solve the problems. But unfortunelly the code doesn't work

The case remains without entitlement when create by email-to-case

Prakash NawalePrakash Nawale
Hi Viniclus,

Can you please create Entitlement data to meet conidtions in code before creating case from email to Case.
 
Vinicius PassosVinicius Passos
Thanks Prakash, I already created an entitlement wich is associate to an account, not the contact.
But is not clearly to me what is wrong
Prakash NawalePrakash Nawale
Vinicius Passos,

In Trigger system first checks case has account or contact yes then find entilement based on account or contact if match found then use that entitlement id to put in case.entitlement.
It's very simple logic.

if you need more details please ping me on Skype : nawaleprakash and mark this as best answer.

Regards,
Prakash
Vinicius PassosVinicius Passos
Thank you so much for your help Prakash Nawale, I made some changes and now the code works
 
trigger DefaultEntitlement on Case (Before Insert, Before Update) { 

		set<Id> contactIds = new set<Id>();
		set<Id> accId = new set<Id>();
         for(Case c : Trigger.new){
				if(c.contactId !=null){
					contactIds.add(c.contactId);
					}
				if(c.AccountId !=null){
					accId.add(c.AccountId);
					}             	
		 }
            List <Entitlement> entls = [Select e.StartDate, e.Id, e.EndDate, 
                    e.AccountId, e.AssetId
                    From Entitlement e
                    Where e.AccountId in :accId] ;
            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;
                            }
                        } 
                    }
                } 
            }
        }