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
Gopal ChatGopal Chat 

Trigger is not working in Update,Error(duplicate id in list which i am updating)

trigger SetAppointmentsSequenceAndSerial on Appointment__c (after Update){
    
    if(CheckRecursive.runOnce() && TriggerManager.getState(string.valueOf(this))){
        Set<Id> doctorIdSet = new Set<Id>();
        Set<Id> patientIdSet = new Set<Id>();
        Set<Date> operationDateSet = new Set<Date>();
        Set<Id> HealthcardSet = new Set<Id>();
        Set<String> operationTimeSet = new Set<String>();
        
        Map<string,List<Appointment__c>> docAppointmentMap = new Map<String,List<Appointment__c>>();
        Map<string,List<Appointment__c>> patientAppointmentMap = new Map<string,List<Appointment__c>>();
        
        for(Appointment__c apt : trigger.New)
            if(ChiroConstants.operationStatusToSimulateDeleteSet.contains(apt.Status__c) 
               && apt.Status__c != trigger.oldMap.get(apt.Id).Status__c){
                   system.debug('===>Raghav');
                   doctorIdSet.add(apt.Doctor_Name__c);
                   operationDateSet.add(apt.Appointment_Date__c);
                   operationTimeSet.add(apt.Appointment_Time__c);
                   patientIdSet.add(apt.Patient__c);
                   HealthcardSet.add(apt.Health_Card__c);
                   string key = apt.Doctor_Name__c+'@@'+apt.Appointment_Date__c+'@@'+apt.Appointment_Time__c;
                   system.debug('==>key'+key);
                   string patientKey = apt.Patient__c+'@@'+apt.Doctor_Name__c+'@@'+apt.Clinic_Name__c+'@@'+apt.Health_Card__c;
                   system.debug('==>patientKey'+patientKey);
                   patientAppointmentMap.put(patientKey,new List<Appointment__c>());
                   docAppointmentMap.put(key,new List<Appointment__c>());
                   system.debug('==>docAppointmentMap'+docAppointmentMap);               }
        
        for(Appointment__c apt : [SELECT Id,Doctor_Name__c,Appointment_Date__c,Appointment_Time__c,Priority_Sequence__c
                                  FROM Appointment__c 
                                  WHERE Doctor_Name__c IN :doctorIdSet
                                  AND status__c NOT IN :ChiroConstants.operationStatusToSimulateDeleteSet
                                  AND Appointment_Date__c IN :operationDateSet 
                                  AND Appointment_time__c IN :operationTimeSet
                                  ORDER BY Doctor_Name__c,Appointment_Date__c,Appointment_Time__c,Priority_Sequence__c]){
                                      string key = apt.Doctor_Name__c+'@@'+apt.Appointment_Date__c+'@@'+apt.Appointment_Time__c;
                                      If(docAppointmentMap.containsKey(key)){
                                          system.debug('===>Dockey'+key);
                                          List<Appointment__c> tempList = docAppointmentMap.get(key);
                                          tempList.add(apt);
                                          docAppointmentMap.put(key, tempList);
                                      }
                                  }
        
        for(Appointment__c apt : [SELECT Id,Patient__c,Health_Card__c,Doctor_Name__c,Clinic_Name__c,Appointment_Date__c,Appointment_Time__c,Serial_No__c
                                  FROM Appointment__c 
                                  WHERE Patient__c IN :patientIdSet 
                                  AND Doctor_Name__c IN :doctorIdSet
                                  AND Health_Card__c IN :HealthcardSet
                                  AND status__c NOT IN :ChiroConstants.operationStatusToSimulateDeleteSet
                                  ORDER BY Patient__c,Serial_No__c]){
                                      string key = apt.Patient__c+'@@'+apt.Doctor_Name__c+'@@'+apt.Clinic_Name__c+'@@'+apt.Health_Card__c;
                                      system.debug('===>Patkey'+key);
                                      If(patientAppointmentMap.containsKey(key)){
                                          List<Appointment__c> tempList = patientAppointmentMap.get(key);
                                          tempList.add(apt);
                                          patientAppointmentMap.put(key, tempList);
                                      }
                                  }
        
        List<Appointment__c> updateAppointmentList = new List<Appointment__c>();
        for(List<Appointment__c> apptList : docAppointmentMap.values()){
            integer sequence = 1;
            for(Appointment__c appt : apptList){
                appt.Priority_Sequence__c = sequence++;
                updateAppointmentList.add(appt);
                system.debug('===>updateAppointmentList1'+updateAppointmentList);
            }
        }
        for(List<Appointment__c> apptList : patientAppointmentMap.values()){
            integer sequence = 1;
            for(Appointment__c appt : apptList){
                appt.Serial_No__c = sequence++;
                updateAppointmentList.add(appt);
                system.debug('===>updateAppointmentList2'+updateAppointmentList);
            }
        }
        system.debug('===>updateAppointmentListSize----->'+updateAppointmentList.size());
        system.debug('===>updateAppointmentList--->'+updateAppointmentList);
        
        if(updateAppointmentList.size()>0)
            update updateAppointmentList;
        system.debug('===>updateAppointmentList3'+updateAppointmentList);
    }
}