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
DekorDekor 

Setting apex trigger not to create entitlement automatically if entitlement already exists

During setting up of entitlements I've created an Apex trigger that automatically assigns an entitlement to new/updated accounts where type = customer.  This is working, however I've discovered that everytime an account is updated, another entitlement is automatically created.

So I need to modify my trigger to first check there isn't an existing entitlement set up.  Below is the trigger and I've added:

(entitlement.accountid != account.id)

However this doesn't seem to do the task.  Any ideas on how I can modify my trigger to not create the entitlement when there is already an existing entitlement in the entitlement table matching the account ID on the account I'm updating. 
 
trigger CreateEntitlement on Account (after insert,before update) {

List<Entitlement> createentitlement = new List <Entitlement> {};

for (Account acc : trigger.new) {

if ((acc.type == 'customer')&&(entitlement.accountid != account.id)) /* If the Account type is set to customer and no entitlement already exists*/

{
createentitlement.add(new Entitlement (
Name = acc.name, /* Give a standard name*/
AccountId = acc.Id, /* Link the Entitlement to the account */
SlaProcessId = '55220000000L9IJAA0', /* Link it to a defined entitlement process */
StartDate = system.Today(),
Type = 'Phone Support',
BusinessHoursId = '01m200000009dGQAAY'
    
));
}
}
try {
insert createentitlement ;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}

 
Best Answer chosen by Dekor
Shashikant SharmaShashikant Sharma
You need to first get existing entilement and create a map , use below code example for it:
Map<Id, Entitlement> mapOfAccountIdToEntitlement = new Map<Id, Entitlement>();

for( Entitlement et : [ Select AccountId From Entitlement Where AccountId in: trigger.new ) {
mapOfAccountIdToEntitlement.put( et.AccountId , et );
}

// then in you code where you have &&(entitlement.accountid != account.id))  check frommap instead like
Boolean alreadyhasEntitlement = mapOfAccountIdToEntitlement.containsKey(acc.Id) ;
if( (acc.type == 'customer') && ( !alreadyhasEntitlement)  ) {
// do action when no existing entitlement
}


 

All Answers

Shashikant SharmaShashikant Sharma
You need to first get existing entilement and create a map , use below code example for it:
Map<Id, Entitlement> mapOfAccountIdToEntitlement = new Map<Id, Entitlement>();

for( Entitlement et : [ Select AccountId From Entitlement Where AccountId in: trigger.new ) {
mapOfAccountIdToEntitlement.put( et.AccountId , et );
}

// then in you code where you have &&(entitlement.accountid != account.id))  check frommap instead like
Boolean alreadyhasEntitlement = mapOfAccountIdToEntitlement.containsKey(acc.Id) ;
if( (acc.type == 'customer') && ( !alreadyhasEntitlement)  ) {
// do action when no existing entitlement
}


 
This was selected as the best answer
DekorDekor
Ok, so I have now got this but it fails to save due to expecting a curly bracket at end of line 70, not close bracket.  Apologies, I'm still getting to grips with apex coding. 
 
trigger CreateEntitlement on Account (after insert,before update) {

List<Entitlement> createentitlement = new List <Entitlement> {};

Map<Id, Entitlement> mapOfAccountIdToEntitlement = new Map<Id, Entitlement>();

for( Entitlement et : [ Select AccountId From Entitlement Where AccountId in: trigger.new ) {
mapOfAccountIdToEntitlement.put( et.AccountId , et );
}

for (Account acc : trigger.new) {
Boolean alreadyhasEntitlement = mapOfAccountIdToEntitlement.containsKey(acc.Id) ;
if( (acc.type == 'customer') && ( !alreadyhasEntitlement)  ) /* If the Account type is set to customer */

{
createentitlement.add(new Entitlement (
Name = acc.name, /* Give a standard name*/
AccountId = acc.Id, /* Link the Entitlement to the account */
SlaProcessId = '55220000000L9IJAA0', /* Link it to a defined entitlement process */
StartDate = system.Today(),
Type = 'Phone Support',
BusinessHoursId = '01m200000009dGQAAY'
    
));
}
}
try {
insert createentitlement ;
}
catch (Exception Ex)
{
system.debug(Ex);
}
}

 
DekorDekor
Ah, spotted a missing ] after the SOQL on line 7.  Got it working now.  Thanks!
Shashikant SharmaShashikant Sharma
Good to know it worked for you. :)