• virendar singh 1
  • NEWBIE
  • 20 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
Hi
I am new to salesforce development. 
How can add trigger logic in below helperclass. 

this is the helperclass. 
public class Service_CaseTriggerHelper {
    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;
                     }
                 }
             }
         }
      }

     }

 }
}
This is the trigger. 
trigger Service_CaseTrigger on Case (Before Insert, Before Update) { 

    Switch on Trigger.operationType {

        when BEFORE_INSERT {

            Service_CaseTriggerHelper.HelperMethod(TRIGGER.NEW);

        }

        when BEFORE_UPDATE {

            Service_CaseTriggerHelper.HelperMethod(TRIGGER.NEW);

        }

    }

}

The below trigger to be add in above helperclass and trigger. 
trigger CompleteResolutionTimeMilestoneopen on Case (after update) {
Id recId = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Incident').getRecordTypeId();
    if (UserInfo.getUserType() == 'Standard'){
        DateTime completionDate = System.now(); 
            List<Id> updateCases = new List<Id>();
            for (Case c : Trigger.new){
                    if (((c.RecordTypeId == recId)&&(c.Status == 'open'))&&((c.SlaStartDate 
                        <= completionDate)&&(c.SlaExitDate == null)) )
        updateCases.add(c.Id);
        }
    if (updateCases.isEmpty() == false)
          milestoneUtils.completeMilestone(updateCases,'Initial Response', completionDate);
         
    

    }
}
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;
         }
     }
   }
}
}
}
}