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
virendar singh 1virendar singh 1 

Helper class for trigger

Hi, 

I am new to salesforce development.
How to write helper class for below apex trigger.
the handler class should have same logic. 

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) {
  contactIds.add(c.ContactId);
  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;
         }
     }
   }
}
}
}
}
Best Answer chosen by virendar singh 1
Suraj Tripathi 47Suraj Tripathi 47

Hi Virendar

TRIGGER 

  trigger DefaultEntitlement on Case (Before Insert, Before Update)

    Switch on Trigger.operationType {

        when BEFORE_INSERT {

            DefaultEntitlementHelper.HelperMethod(TRIGGER.NEW);

        }

        WHEN BEFORE_UPDATE {

            DefaultEntitlementHelper.HelperMethod(TRIGGER.NEW);

        }

    }

}
 

HELPER CLASS 

public class DefaultEntitlementHelper {

    public static void HelperMethod(List<Case> Caselist) {

        Set<Id> contactIds = new Set<Id>();

 Set<Id> acctIds = new Set<Id>();

 for (Case c : Caselist) {

     contactIds.add(c.ContactId);

     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 : Caselist){

         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 : Caselist){

             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;

                     }

                 }

             }

         }

     }

 }
}

}
Hope this helps, If you find your solution please mark it as the best answer.

Thanks