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
Christian SmarslyChristian Smarsly 

auto populate entitlement on case

I am running a very basic setup on entitlements and milestones. That's why I am trying to auto populate entitlements on cases.

My approach in doing so is this:
trigger UpdateEntitlement on Case (after insert) {

if (trigger.isAfter && trigger.isInsert) {
 for (Case c : Trigger.new) {
  if (c.Entitlement == null) {
   c.Entitlement.id = [Select Id from Entitlement Where AccountId = :c.AccountId limit 1].id;
}}

}}

Unfortunately the Entitlement field remains blank. Does anyone know what I am doing wrong?
Christian SmarslyChristian Smarsly
Upon creation only the account and contact is prepopulated - entitlement is empty. The challenge is to get the matching entitlement for the selected account.
Cross object formula doesn't help much, because it's a lookup field.
anil savaliya 11anil savaliya 11
Ohh I see,then  you should write trigger Before Insert event 

Christian SmarslyChristian Smarsly
Before insert throws the following error:
UpdateEntitlement: execution of BeforeInsert caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.UpdateEntitlement
Denis VakulishinDenis Vakulishin
trigger UpdateEntitlement on Case (after insert) {

if (trigger.isAfter && trigger.isInsert) {
for (Case c : Trigger.new) {
  if (c.Entitlement == null) {
   try{
   c.Entitlement.id = [Select Id from Entitlement Where AccountId = :c.AccountId limit 1].id;
   }cathc(QueryException ex) {
//No records found. Maybe you should set it to Null
}
}}

}}
This means that no Entitlement objects fount with provided AccountId
Christian SmarslyChristian Smarsly
I double-checked with the workbench and debug log. The query shouldn't be the problem. 
Denis VakulishinDenis Vakulishin
Dou you have trigger on Entitlement object? If yes check it's logic too