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
Jagadeesh AdaveniJagadeesh Adaveni 

Why i am getting following Error while i am updating Record?

Hi All,

May i know that Why i am getting following Error while i am updating Record?
But in Debug logs i am getting values that what i want to update. Could you please change my trigger.


Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger editGenerateLogbooks caused an unexpected exception, contact your administrator: editGenerateLogbooks: execution of AfterUpdate caused by: System.ListException: Duplicate id in list: a04f0000005HAXyAAO: Trigger.editGenerateLogbooks: line 68, column 1
 
trigger editGenerateLogbooks on Log_Book__c (after update) {
    
    Set<id> set1 = new Set<id>();
    Set<String> weeks = new Set<String>(); 
    Set<String> subjects = new Set<String>();
    Set<String> students = new Set<String>();   
    List<Weeks__c> weekList = Weeks__c.getAll().values();
    List<Log_Book__c> lb = new List<Log_Book__c>();
    List<Log_Book__c> lbDelList = new List<Log_Book__c>();
    List<Log_Book__c> lstToUpdate = new List<Log_Book__c>();
    Map<Integer,weeks__c> weekMap = new Map<Integer,weeks__c>();
   
    
    for(weeks__c week : weekList)
    {
        weekMap.put((Integer)week.Order__c,week);
         
    }
    for(Log_Book__c lbook : Trigger.old)
    {
        set1.add(lbook.Id);
    }
    lb=[select Abacus__c, Absent_Time_Stamp__c, Absent__c, Attended__c, Class_Scheduled_Date__c, Collected_Book__c, Comments__c, Do_Not_Promote__c, English__c, Level__c, Order__c, Reason__c, Id, Student__c, Subject__c, Subjects_Registered__c, Week__c, Week_Day__c, gk__c, math__c from log_book__c where id in: set1];
    for(Log_Book__c lbdelete : lb)
    {
        if(lbdelete.Class_Scheduled_Date__c >= System.today())
        {
            lbDelList.add(lbdelete);
           
        }
    }
    for(Log_Book__c lbUpdate : Trigger.new)
    {
        
    
            weeks.add(lbUpdate.week__c);
            subjects.add(lbUpdate.Subject__c);
            students.add(lbUpdate.Student__c); 
   
    }
    
      for(Log_Book__c lbup : [select Abacus__c, Absent_Time_Stamp__c, Absent__c, Attended__c, Class_Scheduled_Date__c, Collected_Book__c, Comments__c, Do_Not_Promote__c, English__c, Level__c, Order__c, Reason__c, Id, Student__c, Subject__c, Subjects_Registered__c, Week__c, Week_Day__c, gk__c, math__c from log_book__c where subject__c IN: subjects AND Student__c IN: students AND week__c IN: weeks AND Class_Scheduled_Date__c >=: system.today() order by order__c])
      {
      
          String[] weekLevels = lbup.Week__c.split('-');
          Integer weekLevel = Integer.valueof(weekLevels[0]);
   
       if(lbup.week__c != Trigger.oldMap.get(lbup.id).week__c || lbup.Class_Scheduled_Date__c >= System.Today())
       {
           for(Integer j=weekLevel;j<27;j++)
           {
            system.debug('++hiiiiiiiiiii+');
            lbup.Class_Scheduled_Date__c = lbup.Class_Scheduled_Date__c.addDays(7);
            lbup.week__c = weekMap.get(weekLevel).name;
            system.debug('*****Jagadeesh********'+lbup.Class_Scheduled_Date__c + lbup.Level__c + lbup.Week__c);
            lstToUpdate.add(lbup);
            
            weekLevel++;
            
            }
            weekLevel = 1;
        }
      }
    
   
    
    delete lbDelList;
    update lstToUpdate;
}

User-added image
Philip NelsonPhilip Nelson
I'm not sure what you're doing specifically here, but but it appears you are adding the lbup to the lstToUpdate many times.

Try moving your call to add the lbup to lstToUpdate outside that final for loop, like this:

        if(lbup.week__c != Trigger.oldMap.get(lbup.id).week__c || lbup.Class_Scheduled_Date__c >= System.Today()) {
            for(Integer j=weekLevel;j<27;j++) {
                lbup.Class_Scheduled_Date__c = lbup.Class_Scheduled_Date__c.addDays(7);
                lbup.week__c = weekMap.get(weekLevel).name;

                weekLevel++;

            }

            // move this outside the for loop above
            lstToUpdate.add(lbup);

            weekLevel = 1;
        }
Jagadeesh AdaveniJagadeesh Adaveni
if i add that outside for loop i won't accept bcz "lbup" is local variable
Philip NelsonPhilip Nelson
lbup is not local to your to your for loop on line 50, it is local to your for loop on line 42.

Did you try moving it as suggested?
Jagadeesh AdaveniJagadeesh Adaveni
If i do like that i'm getting following error

ENTITY_IS_DELETED, entity is deleted: [] Trigger.editGenerateLogbooks: line 74, column 1: []: Trigger.editGenerateLogbooks: line 74, column 1
Vishal NegandhiVishal Negandhi
The same record must have been added in the list "lbDelList". 

You need to differentiate the records added in delete list and the ones added in update list. 
Maybe you can use a boolean;

... some logic... 
lbDelList.add(object);
// update a variable in the same logic
addedToDeleteList = true;

... some logic...
if(!addedToDeleteList)
    lstToUpdate.add(object);

Hope this clears your doubt.