You need to sign in to do that
Don't have an account?
Jagadeesh Adaveni
Unable to Delete Records using APEX????
Hi All,
I have written one Apex controller in that i wrote logic for delete and update. Problem here is am unable to delete records.
Thanks,
A.Jagadeesh
I have written one Apex controller in that i wrote logic for delete and update. Problem here is am unable to delete records.
public class logView3Controller { public Boolean searched { get; set; } public List<Weeks__c> weekList = Weeks__c.getAll().values(); public Map<Integer, Weeks__c> weekMap = new Map<Integer, Weeks__c>(); public Map<String, Integer> weekMapGetIntWithWeek = new Map<String, Integer>(); public Map<String, Subjects_and_Levels__c> subjectLevelMap = Subjects_and_Levels__c.getAll(); public List<Subjects_and_Levels__c> subjectLevelList = subjectLevelMap.values(); public Map<String, Subjects_and_Levels__c> levelMap = new Map<String, Subjects_and_Levels__c>(); public Date StartDate {get;set;} public Date EndDate {get;set;} public String selectedLocation {get;set;} public String selectedDay {get;set;} public List<Log_Book__c> logBookEntries {get;set;} public List<studentsWithWeeks> studentsWithWeeksList {get;set;} public studentsWithWeeks newSWW {get;set;} public String domain {get;set;} public List<integer> indexNums {get;set;} public List<String> weekTableHeader {get;set;} public List<String> subjectTableHeader {get;set;} public List<Log_Book__c> relatedLogs{get;set;} //Jagadeesh public String selectedTeacher{get;set;} public String selectedDays{get;set;} public Id recordToUpdateId {get;set;} public Id recordToUpdateIdStudent {get;set;} public log_Book__c logObj {get;set;} public Student__c studentObj {get;set;} public boolean displayPopup {get; set;} public boolean displayPopupStudent {get; set;} public String exportToExcelURL {get;set;} public log_Book__c newLevel{get;set;} public log_Book__c newWeek{get;set;} List<Log_Book__c> lbjump{get;set;} // 2. a,b,c,d - a,j,k,l,m - changing the complete series // Written by Jagadeesh(New code for skip records) public List<Log_Book__c> jump{get;set;} public Log_Book__c jump1{get;set;} public List<Log_Book__c> lblist{get;set;} List<Log_Book__c> updateLogs = new List<Log_Book__c>(); public logView3Controller () { domain = ApexPages.currentPage().getHeaders().get('Host'); //exportToExcelURL = '/apex/logView3ExportToExcel?startDate='+startDate+'&endDate='+endDate+'&selectedLocation='+selectedLocation; exportToExcelURL = '/apex/logView3ExportToExcel?startDate='+startDate+'&endDate='+endDate+'&selectedLocation='+selectedLocation+'&selectedTeacher='+selectedTeacher; weekTableHeader = new List<String>(); relatedLogs = new List<Log_book__c>(); searched = false; } public String getEnglishColor() { return Colr; } public PageReference doUpdateLogObj2() { String lv = logObj.Week__c.split('-')[0]; if(logObj.Subject__c == 'Math' && logObj.Level__c == 'Level 5B' && Integer.valueOf(lv) > 17){ logObj.Week__c.addError('For Math Level 5B week should not exceed 17'); return null; } system.debug('hii'); relatedLogs = [SELECT Id, Name, Color_Code__c, Comment_on_LogBook__c, Student__c, Student__r.Id, Student__r.Name, Student__r.Last_Name__c, Subject__c, Level__c, Week__c, Week_Day__c, Class_Scheduled_Date__c, Student__r.Location__r.Name FROM Log_Book__c WHERE Student__r.Id =: logObj.Student__r.Id AND Subject__c =: logObj.Subject__c AND Class_Scheduled_Date__c >=: logObj.Class_Scheduled_Date__c and isSkipped__c=false]; System.debug(' relatedLogs.Size()'+ relatedLogs.Size()); Log_Book__c lb = new Log_Book__c(); Integer levelOrder = Integer.valueof(subjectLevelMap.get(logObj.Subject__c +':'+logObj.Level__c).Order__c); Integer weekOrder; System.Debug('levelOrder :' + levelOrder); System.Debug('weekList :' + weekList); for(Weeks__c week : weekList) { weekMap.put((Integer)week.Order__c, week); weekMapGetIntWithWeek.put(week.Name, (Integer)week.Order__c); } System.Debug('weekMap :' + weekMap); System.Debug('weekMapGetIntWithWeek :'+ weekMapGetIntWithWeek); for(Subjects_and_Levels__c level: subjectLevelList) { levelMap.put(level.subject__c+Integer.valueof(level.Order__c), level); } System.Debug('levelMap :' + levelMap); Integer totalLevels = 0; if(logObj.Subject__c == 'English') totalLevels = 16; else if(logObj.Subject__c == 'Abacus') totalLevels = 11; else if(logObj.Subject__c == 'Math') totalLevels = 14; else if(logObj.Subject__c == 'G.K') totalLevels = 3; System.Debug('Total Logs :'+ relatedLogs.Size()); lb = logObj.clone(false, true, false, false); updateLogs.add(lb); for(Integer i=1; i<relatedLogs.Size(); i++) { lb = new Log_Book__c(); System.Debug('lblblblblblblb :' + lb); lb = updateLogs[i-1].clone(false, true, false, false); weekOrder = Integer.ValueOf(weekMapGetIntWithWeek.get(lb.Week__c)); System.Debug('weekOrder : ' + weekOrder); lb.Class_Scheduled_Date__c = lb.Class_Scheduled_Date__c.addDays(7); System.Debug('weekMapGetIntWithWeek.get(lb.Week__c) :'+ weekMapGetIntWithWeek.get(lb.Week__c)); System.Debug('weekMapGetIntWithWeek.get(lb.Week__c)+i :'+ Integer.ValueOf(weekMapGetIntWithWeek.get(lb.Week__c))+i); if( weekOrder < 26 ) { weekOrder = weekOrder+1; lb.Week__c = weekMap.get(weekOrder).Name; System.Debug('@@@@@@ lb.Week__c = ' + lb.Week__c); } else { if(lb.week__c == '26-Z') { if(levelOrder < totalLevels) { levelOrder = levelOrder +1; System.Debug('lb.Subject__c+levelOrder : '+ lb.Subject__c+levelOrder); lb.level__c = levelMap.get(lb.Subject__c+levelOrder).Level__c; lb.Week__c = '1-A'; } } else if(lb.Subject__c == 'Math' && lb.Level__c == 'Level 5B' && lb.Week__c == '17-Q') { if(levelOrder != null && levelOrder < totalLevels){ levelOrder = levelOrder +1; lb.level__c = levelMap.get(lb.Subject__c+levelOrder).Level__c; lb.Week__c = '1-A'; } } else { i = relatedLogs.Size(); } } System.Debug('lblblblblblblb :' + lb); updateLogs.add(lb); System.Debug('updateLogs : '+ updateLogs); } delete relatedLogs; update updateLogs; //insertLogs(); System.debug('@@@@@updated logs@@@'+updateLogs.size()+'----------->'+updateLogs[0]); displayPopup = false; return null; } }
Thanks,
A.Jagadeesh
records are not getting deleted but updating
relatedLogs = [SELECT Id FROM Log_Book__c WHEREStudent__r.Id =: logObj.Student__r.Id AND Subject__c =: logObj.Subject__c ANDClass_Scheduled_Date__c >=: logObj.Class_Scheduled_Date__c and isSkipped__c=false];
System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []
Error is in expression '{!doUpdateLogObj2}' in component <apex:commandButton> in page logview3: Class.logView3Controller.doUpdateLogObj2: line 177, column 1
Class.logView3Controller.doUpdateLogObj2: line 177, column 1
Please look into the code "updateLogs" is not really a update call. I think it contains the new cloned records. Please change it to either insert orelse upsert.
Regards,
Mahesh
highlited records should delete.
its working
Check your Query First........................
yeah Query is alright Rahul,
Nothing to change query
Am not getting any error if i use upsert.
old records are not getting deleted
System.debug('--->related logs are--->'+relatedLogs);
delete relatedLogs;
System.debug('--->related logs are after delete--->'+relatedLogs);
those statements are not getting executed.
Then we have to review the whole code by adding the debug msgs appropriately.
If possible send us the debug log so that I can analyse it and give you a better answer.
Regards,
Mahesh
I hope trigger is stoping the delete statement,
I have after update trigger on that object, if i remove that it is working.
Could you give solution for this.
Then we have to see the update trigger and analyse why it is giving the error.
Regards,
Mahesh
After adding after update trigger i am unable to delete my old records.
Could you please see the code
Test it and see the result.
could you please give any suggestions.
Do we have any triggers on that object, if yes then provide those here and also try inactivating them and naildown which trigger is causing the issue.
Regards,
Mahesh