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
GMASJGMASJ 

Trigger is not firing.

Hi, 

  I want to update entitlement fields from opportunitylines below is the code written not sure what the issue is it is not firring the update. Please suggest me the change.
 
trigger update_entitlement_product on OpportunityLineItem (after insert, after update) {

   system.debug('Opportunity Line Items Trigger Started.....');

   EntitlementUtil.UpdateEntitlementDate(trigger.new);

}
public with sharing class EntitlementUtil {

public static void UpdateEntitlementDate(List<OpportunityLineItem> newLst){

 set<id> oppid = new set<id>();
 list<entitlement> ent = new list<entitlement>();
 
  for(OpportunityLineItem opp: newLst){
   oppid.add(opp.id);
  }
  
 for(opportunity ops: [select NS_Start_Date_Min__c,NS_End_Date_Max__c from opportunity where id in :oppid]){
    system.debug('Opportunity Lines NS_Start_Date_Min__c :' + ops.NS_Start_Date_Min__c);
    system.debug('Opportunity Lines NS_End_Date_Max__c :' + ops.NS_End_Date_Max__c);
    
   for(entitlement etl : [select Opportunity__c,StartDate,EndDate from Entitlement where Opportunity__c in :oppid]){
    system.debug('Entitlement StartDate :' + etl.StartDate);
    system.debug('Entitlement EndDate :' + etl.EndDate);
    
      etl.StartDate = ops.NS_Start_Date_Min__c;
      etl.EndDate = ops.NS_End_Date_Max__c;
      ent.add(etl);
    }  
  }
 
  if(ent.size() > 0){
    system.debug('Entitlement dates are updated');
    update ent;
  }
  
}
}
Thanks
SD

 
Best Answer chosen by GMASJ
VamsiVamsi
Hi,

Please find the optimized version of the above code...
 
public with sharing class EntitlementUtil 
{

public static void UpdateEntitlementDate(List<OpportunityLineItem> newLst)
{


 set<id> oppid = new set<id>();
 list<entitlement> ent = new list<entitlement>();
 
  for(OpportunityLineItem opp: newLst)
  {
   oppid.add(opp.OpportunityID);
  }
  
    Map<ID,opportunity> Ops = new Map<ID,opportunity>([select id,NS_Start_Date_Min__c,NS_End_Date_Max__c from opportunity where id in :oppid]);
	
    for(entitlement etl : [select Opportunity__c,StartDate,EndDate from Entitlement where Opportunity__c in :oppid])
	{
	 if(Ops.containsKey(etl.Opportunity__c))
	 {
		  system.debug('Entitlement StartDate :' + etl.StartDate);
		  system.debug('Entitlement EndDate :' + etl.EndDate);
		 
  		  etl.StartDate = ops.get(etl.Opportunity__c).NS_Start_Date_Min__c;
		  etl.EndDate = ops.get(etl.Opportunity__c).NS_End_Date_Max__c;
		  ent.add(etl);
	  }
    }  
 
  if(ent.size() > 0)
  {
    system.debug('Entitlement dates are updated');
    update ent;
  }
  
}
}

Hope this helps...!!!

Please mark as best answer if the above helps....!!!

All Answers

VamsiVamsi
Hi,

Were you able to get these two statements executed ?

system.debug('Opportunity Lines NS_Start_Date_Min__c :' + ops.NS_Start_Date_Min__c);
14    system.debug('Opportunity Lines NS_End_Date_Max__c :' + ops.NS_End_Date_Max__c);

Bcoz, I could see you are trying to get Opportunities with Opportunity line item ids ... and you have included a for loop inside a for lool with query 

 
Maharajan CMaharajan C
Hi,

Change the entire second for like below:

for(entitlement etl : [select Opportunity__c,StartDate,EndDate from Entitlement where Opportunity__c = : ops.id])
{
entitlement ET = new entitlement(id = etl.id);
ET.StartDate = ops.NS_Start_Date_Min__c;
ET.EndDate = ops.NS_End_Date_Max__c;
system.debug('Entitlement StartDate :' + ET.StartDate);
system.debug('Entitlement EndDate :' + ET.EndDate);
ent.add(ET);
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj
 
VamsiVamsi
Hi,

Please find the optimized version of the above code...
 
public with sharing class EntitlementUtil 
{

public static void UpdateEntitlementDate(List<OpportunityLineItem> newLst)
{


 set<id> oppid = new set<id>();
 list<entitlement> ent = new list<entitlement>();
 
  for(OpportunityLineItem opp: newLst)
  {
   oppid.add(opp.OpportunityID);
  }
  
    Map<ID,opportunity> Ops = new Map<ID,opportunity>([select id,NS_Start_Date_Min__c,NS_End_Date_Max__c from opportunity where id in :oppid]);
	
    for(entitlement etl : [select Opportunity__c,StartDate,EndDate from Entitlement where Opportunity__c in :oppid])
	{
	 if(Ops.containsKey(etl.Opportunity__c))
	 {
		  system.debug('Entitlement StartDate :' + etl.StartDate);
		  system.debug('Entitlement EndDate :' + etl.EndDate);
		 
  		  etl.StartDate = ops.get(etl.Opportunity__c).NS_Start_Date_Min__c;
		  etl.EndDate = ops.get(etl.Opportunity__c).NS_End_Date_Max__c;
		  ent.add(etl);
	  }
    }  
 
  if(ent.size() > 0)
  {
    system.debug('Entitlement dates are updated');
    update ent;
  }
  
}
}

Hope this helps...!!!

Please mark as best answer if the above helps....!!!
This was selected as the best answer
GMASJGMASJ
Thanks Vamsi you code is working I have a problem in updating only the entitlement enddate.  When opportunity is associated to entitlement I try to update the enddate it is auto deleting the enddate when updated. 

Please let me know if you know why it is auto deleted. There is not other process or custom code is in conflict.  

Thanks
Sudhir
VamsiVamsi
Hi Sudhir, Can you elaborate the issue I'm not sure I get it completely. When you update the Opp line item is the Entitlement end date being auto deleted ?
GMASJGMASJ
Hi Vamsi, 

   Yes your right also even if we directly update from entitlement enddate it is getting auto deleted. 
 
   This delete is happenig only if opportuntiy is associated to entitlement. if there is no opportunity it is allowing me to update the enddate. 

    Opportunity look up is in below screen shot under Opportunity information. 


User-added image

Thanks
Sudhir
 
VamsiVamsi
Hi, Please make sure there aren't any field updates or triggers on Entitlements
GMASJGMASJ
Thanks Vamsi you resolve my issue.