I wanted to updated the End date whenever the start date is update and it should always calculated the end date as below End Date = start Date + warranty duration field.
the issue here is warranty duration field is picklist field and start date and end date is date field. I tried multiple ways but my end date is not populating. Is there any suggestion. it would be really helpful
Below is code only for this scenario which I have tried.
public class WarrantyClass {
public void updateWarrantyRecords(List<Surgery__c> newSurglist, Map<Id, Surgery__c> oldSurgMap){
List<NMD_Warranty__c> updateWarrantiesList = new List<NMD_Warranty__c>();
Map<Id, List<NMD_Warranty__c>> warrantyMap = new Map<Id, List<NMD_Warranty__c>>();
Set<Id> processedWarrantiesIds = new Set<Id>();
Map<String, String> inactiveReasonMapping = new Map<String, String>{
'Device Damaged' => 'Damaged',
'Patient Deceased' => 'Deceased' ,
'Device Lost' => 'Lost',
'Device Stolen' => 'Stolen',
'Rep Entry Error' => 'Cancelled Surgery',
'Order Cancelled' => 'Cancelled Surgery'
};
List<NMD_Warranty__c> relatedWarranties = [Select id, Surgery__c, Inactive_Reason__c, Active__c,
Start_Date__c, NMD_Serial_Number__c from NMD_Warranty__c
where Surgery__c IN :newSurglist];
for(NMD_Warranty__c warranty : relatedWarranties){
if(!warrantyMap.containskey(warranty.Surgery__c)){
warrantyMap.put(warranty.Surgery__c, new List<NMD_Warranty__c>());
warrantyMap.get(warranty.Surgery__c).add(warranty);
}
for(Surgery__c surg : newSurglist){
if(warrantyMap.containskey(surg.Id)){
List<NMD_Warranty__c> warranties = warrantyMap.get(surg.Id);
for(NMD_Warranty__c war : warranties){
if(!processedWarrantiesIds.contains(war.Id)){
if(surg.Cancellation_Reason__c != oldSurgMap.get(surg.Id).Cancellation_Reason__c && war.Active__c == true) {
string inactiveReason = inactiveReasonMapping.get(surg.Cancellation_Reason__c);
system.debug('inactiveReason in@@ :'+ inactiveReason);
if(inactiveReason != null){
war.Inactive_Reason__c = inactiveReason;
}
}
if (surg.Procedure_Date__c != oldSurgMap.get(surg.Id)?.Procedure_Date__c && war.Active__c == true) {
System.debug('Procedure Date changed: ' + surg.Procedure_Date__c);
war.Start_Date__c = Date.valueOF(surg.Procedure_Date__c);
System.debug('Start Date updated: ' + war.Start_Date__c);
//war.End_Date__c = Date.valueOF(surg.Procedure_Date__c);
if(surg.Model_Number__r.Warranty_Duration__c != null){
Map<String, Integer> warDurationMap = new Map<String, Integer>{
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'10' => 10
};
System.debug('In warDurationMap: ' + warDurationMap);
Integer warrantyears = warDurationMap.get(surg.Model_Number__r.Warranty_Duration__c);
war.End_Date__c = war.Start_Date__c.addYears(warrantyears);
System.debug('End Date updated: ' + war.End_Date__c);
}
}
if(surg.Patient__c != oldSurgMap.get(surg.Id).Patient__c){
war.Patient__c = surg.Patient__c;
}
}
updateWarrantiesList.add(war);
processedWarrantiesIds.add(war.id);
}
}
if(!updateWarrantiesList.isEmpty()){
update updateWarrantiesList;
system.debug('updateWarrantiesList:' + updateWarrantiesList);
}
}
}
}
Update code below with Error
(SurgeryTrigger: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a9c8K0000008PvfQAE Class.WarrantyServiceClass.updateWarrantyRecords: line 114, column 1 Class.SurgeryTriggerHandler.bulkAfter: line 46, column 1 Class.TriggerHandlerManager.execute: line 128, column 1 Class.TriggerHandlerManager.createAndExecuteHandler: line 56, column 1 Trigger.SurgeryTrigger: line 2, column 1)
public class WarrantyClass {
public void updateWarrantyRecords(List<Surgery__c> newSurglist, Map<Id,Surgery__c> oldSurgMap){
List<NMD_Warranty__c> updateWarrantiesList = new List<NMD_Warranty__c>();
Map<Id, List<NMD_Warranty__c>> warrantyMap = new Map<Id,List<NMD_Warranty__c>>();
set<Id> processedWarrantiesIds = new set<Id>();
Map<string,string> inactiveReasonMapping = new Map<string,string>{
'Device Damaged'=> 'Damaged',
'Patient Deceased'=>'Deceased' ,
'Device Lost'=> 'Lost',
'Device Stolen'=> 'Stolen',
'Rep Entry Error'=> 'Cancelled Surgery',
'Order Cancelled'=> 'Cancelled Surgery'
};
List<NMD_Warranty__c> relatedWarranties = [Select id, Surgery__c, Inactive_Reason__c, Active__c,
Start_Date__c, End_Date__c from NMD_Warranty__c
where Surgery__c IN :newSurglist];
for(NMD_Warranty__c warranty :relatedWarranties){
if(!warrantyMap.containskey(warranty.Surgery__c)){
warrantyMap.put(warranty.Surgery__c,new list<NMD_Warranty__c>());
warrantyMap.get(warranty.Surgery__c).add(warranty);
}
for(Surgery__c surg :[SELECT Id, Model_Number__r.Warranty_Duration__c,
Procedure_Date__c,Cancellation_Reason__c,Patient__c
FROM Surgery__c
WHERE Id IN :newSurglist]){
if(warrantyMap.containskey(surg.Id)){
List<NMD_Warranty__c> warranties = warrantyMap.get(surg.Id);
for(NMD_Warranty__c war :warranties){
if(!processedWarrantiesIds.contains(war.Id)){
if(surg.Cancellation_Reason__c != oldSurgMap.get(surg.Id).Cancellation_Reason__c && war.Active__c == true) {
string inactiveReason = inactiveReasonMapping.get(surg.Cancellation_Reason__c);
system.debug('inactiveReason in@@ :'+ inactiveReason);
if(inactiveReason!=null){
war.Inactive_Reason__c =inactiveReason;
}
}
if (surg.Procedure_Date__c != oldSurgMap.get(surg.Id)?.Procedure_Date__c && war.Active__c == true) {
System.debug('Procedure Date changed: ' + surg.Procedure_Date__c);
war.Start_Date__c = Date.valueOF(surg.Procedure_Date__c);
System.debug('Start Date updated:' + war.Start_Date__c);
// war.End_Date__c = Date.valueOF(surg.Procedure_Date__c);
//calcualted the end date based on warrnaty duration and start date.
if(surg.Model_Number__r.Warranty_Duration__c!=null){
Map<string,Integer> warDurationMap = new Map<string,Integer>{
'1' => 1,
'2' => 2,
'3' => 3,
'4' => 4,
'5' => 5,
'10' => 10
};
System.debug('In warDurationMap: ' + warDurationMap);
Integer warrantyears = warDurationMap.get(surg.Model_Number__r.Warranty_Duration__c);
war.End_Date__c = Date.valueOF(surg.Procedure_Date__c).addYears(warrantyears);
//war.End_Date__c = Date.valueOF(surg.Procedure_Date__c) + warrantyears;
System.debug('End Date updated: ' + war.End_Date__c);
}
}
if(surg.Patient__c!=oldSurgMap.get(surg.Id).Patient__c){
war.Patient__c = surg.Patient__c;
}
}
updateWarrantiesList.add(war);
processedWarrantiesIds.add(war.id);
}
}
if(!updateWarrantiesList.isEmpty()){
update updateWarrantiesList;
system.debug('updateWarrantiesList:'+updateWarrantiesList);
}
}
}
}
}
Date.addYears()
to a date field is correct (because we get a newDate
back from that method as opposed to the method modifying theDate
it's called on). There's something else in your trigger that's wrong, but we won't be able to say what specifically is wrong unless you show us all of your trigger. Could be that you're inafter update
and forgot to perform DML. Could be that you're inbefore update
but you aren't working on the record instance intrigger.new
/trigger.newMap
. Could be something else entirely.