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
sudhirn@merunetworks.comsudhirn@merunetworks.com 

Suggestion on writing the logic for test class on entitlement

Hi,

  Need a suggestion for writing a test class for below trigger logic is it updates entitlement when opportunity NS Start and NS End Dates are updated in opportunity product
 
trigger update_entitlement on Opportunity (after insert, after update) 
{
 
 set<id> oppid = new set<id>();

  for( opportunity op : trigger.new)
   {
     oppid.add(op.id);
     }
     
   EntitlementUtil.updateentitlement(oppid);
    
}
Below is the class that is getting called inside the trigger 
public with sharing class EntitlementUtil {


public static void updateentitlement(set<ID> opp){

  list<entitlement> ent = [select Opportunity__c,StartDate,EndDate from Entitlement where Opportunity__c in :opp]; 

   opportunity getopp = [select NS_Start_Date_Min__c,NS_End_Date_Max__c from opportunity where id = :opp];
  
   for( entitlement gent : ent)
   {
      gent.StartDate = getopp.NS_Start_Date_Min__c;
      gent.EndDate = getopp.NS_End_Date_Max__c;
      update gent;
    }


}

}
Please suggest me how should i be writing the logic to update the dates. 

Thanks
Sudhir
 
Stefan AbramiukStefan Abramiuk
HI Sudhir,

first of all (sorry that I'm pointing it out) your code is very messy.
 
trigger update_entitlement on Opportunity (after insert, after update) {
     
   EntitlementUtil.updateentitlement(Trigger.newMap);
}
public with sharing class EntitlementUtil {
	public static void updateentitlement(Map<Id, Opportunity> oppMap){

		Map<Id, List<Entitlement>> entListByOppId = new Map<Id, List<Entitlement>>();
		List<Entitlement> entList = new List<Entitlement>()
		for(Entitlement ent : [SELECT Opportunity__c,StartDate,EndDate FROM Entitlement WHERE Opportunity__c IN :oppMap.keySet()]){
			if(!entListByOppId.containsKey(ent.Opportunity__c){
				entListByOppId.put(ent.Opportunity__c, new List<Entitlement>());
			}
			entListByOppId.get(ent.Opportunity__c).add(ent);
			entList.add(ent);
		} 

		
	  
		for(Opportunity opp : oppMap.values()){
			if(entListByOppId.containsKey(opp.Id)){
				for(Entitlement ent : entListByOppId.get(opp.Id)){
					gent.StartDate = getopp.NS_Start_Date_Min__c;
					gent.EndDate = getopp.NS_End_Date_Max__c;
				}
			}
		}
		update entList;
	}

}

I'm not sure how it should work for after insert cause it may be that there is no relatation from opportunity to entitlement yet then.
But after update is easy.
As given: Prepare one Opportnintiy and one Entitlement.
As when: Update NS_Start_Date_Min__c on Opportunity
As then: Check if StartDate in Entitlement (in DB) is udpated.
 
sudhirn@merunetworks.comsudhirn@merunetworks.com

Hi Stefan, 

  Sorrry for the confusion logic is simple any time opportuntiy is updated it should update the entitlement start and end date from opportuninty start and end that is what in the class 

Thanks

Sudhir