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
Scott WalkerScott Walker 

Serious help needed with some code that fixes an issue!

Hi all I really hope someone can help me here

I should start by saying I am learning to code but nowhere near ready, I have passed my admin and app builder certifications but actual proper coding is out of my reach.  I made my first trigger the other day, which I was proud of!

Anyhow, I have a bit of an issue.  My work (a primary school) paid for a customized system which I inherited and basically it works great apart from one part.  This one part is a process that generates sessions for attendance processes.  The error was known about at the time and I got given a line of code plus a script which I am meant to run that fixes it in within the developer window.  Now the person that wrote it has gone, off the face of the planet.  Now we run this yearly only and last year it worked.  I have the code here.  The issue is that the first line of code works I believe.
Student_Section_Utils.studentSection_AfterInsert([select id, Section__c from Student_Section__c where Section__r.RecordType.Name = 'Schedule Group']);
Its this code that doesn't work as it brings up this error: ​Too many DML rows: 10001.  I have no idea what this means.

User-added image


The code I was given which is not working is here.  
 
List<Section_ReportingPeriod__c> srpList = [select id, Scheduler_ReferenceId__c, Section__r.School__c,  Section__r.Schedule_Group__c, Section__r.Course__c, Time__c from Section_ReportingPeriod__c where Scheduler_ReferenceId__c = null];
Set<String> SchedSecIDs = new Set<String>();
for(Section_ReportingPeriod__c srp:srpList){
		srp.Scheduler_ReferenceId__c = srp.Section__r.School__c+'-'+srp.Section__r.Schedule_Group__c+'-'+srp.Section__r.Course__c+'-'+srp.Time__c;
		SchedSecIDs.add(srp.Scheduler_ReferenceId__c);
}
update srpList;
List<Scheduled_Section__c> schedSecList = [select id, Needs_Publish__c from Scheduled_Section__c where Section_Reference_Id__c IN:SchedSecIDs];
for(Scheduled_Section__c ss:schedSecList){
	ss.Needs_Publish__c = true;
}
update schedSecList;

Any chance someone can help me make this work?  It would be a huge help.  Its just me here at the school and this will fix our entire attendance system.

I can send whoever can help E-beers or E-high fives, whatever you need!
 
Best Answer chosen by Scott Walker
karthikeyan perumalkarthikeyan perumal
Hello 

save the below class 
global class batchUpdate implements Database.Batchable
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        List<Section_ReportingPeriod__c> srpList = [select id, Scheduler_ReferenceId__c, Section__r.School__c,  Section__r.Schedule_Group__c, Section__r.Course__c, Time__c from Section_ReportingPeriod__c where Scheduler_ReferenceId__c = null];
        Set<String> SchedSecIDs = new Set<String>();
        for(Section_ReportingPeriod__c srp:srpList){
		srp.Scheduler_ReferenceId__c = srp.Section__r.School__c+'-'+srp.Section__r.Schedule_Group__c+'-'+srp.Section__r.Course__c+'-'+srp.Time__c;
		SchedSecIDs.add(srp.Scheduler_ReferenceId__c);
		}
        
        String query = 'select id, Needs_Publish__c from Scheduled_Section__c where Section_Reference_Id__c IN:SchedSecIDs';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Scheduled_Section__c> scope)
    {
         for(Scheduled_Section__c ss : scope)
         {   
             
             ss.Needs_Publish__c = true;     
         }
         update schedSecList;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

and open the developer console 

use below code to excute
 
batchUpdate b = new batchUpdate ();
Datbase.executeBatch(b);
Howp this will help you 

Thanks
karthik
 

All Answers

Scott WalkerScott Walker
Just to add to this.  I have been opening up the developer console and then typing the first line of code in the Open Execute Annonymous window.  I then execute that.  Then I run the script via the same method replacing the previous code entered and press execute.
karthikeyan perumalkarthikeyan perumal
Hello, 

As your error indicates, you are only allowed to insert, update, delete, etc 10,000 rows per transaction.  One way to break these records up into smaller, more managable transactions is to createa a batchable class that handles the deletes in batches of up to 2000 records at a time.  For instance, see the following document and the "deleting records"

For Example: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

if you wanna try try to excute below code in Developer console. 

SOQL Select Query allow only 10000 per transaction: 
Student_Section_Utils.studentSection_AfterInsert([select id, Section__c from 
Student_Section__c where Section__r.RecordType.Name = 'Schedule Group' Limit 10000]);
Try to create a Batch apex class and update the same thing 


Hope this will help you. 

Thanks
Karthik

 
Scott WalkerScott Walker
Karthikeyan thanks so much for your help and insight.  I will certainly read up on this, as I mentioned I am at the very very beginning of learning to code so this is all very useful to me.

With regards to the code you have sent me, where do I add this in my original code?


Thanks once again!
Scott WalkerScott Walker
Oh this replaces the first 2 lines of code correct?  If so how do I ensure it runs all the necessary transactions for all that exist in my org, does this only do so many of them and if so how do I run the next load?
Scott WalkerScott Walker
OK so I just read up on the batch class.  I unnderstand what it does but i have no idea how to embed this into my code I'm afraid, is this something I can get help with on here.  It would really help me out
karthikeyan perumalkarthikeyan perumal
Hello,

can you post your full code so that i can create a batch class.. 

Thanks
karthik
 
Scott WalkerScott Walker
Really!!! Thats so kind of you, seriously!

OK well the code is actually in the post above.  This is what I was told to do.

1. Copy paste this short code into the developer console:
Student_Section_Utils.studentSection_AfterInsert([select id, Section__c from Student_Section__c where Section__r.RecordType.Name = 'Schedule Group']);

Then after that I had to copy and paste this script into the developer console:
 
List<Section_ReportingPeriod__c> srpList = [select id, Scheduler_ReferenceId__c, Section__r.School__c,  Section__r.Schedule_Group__c, Section__r.Course__c, Time__c from Section_ReportingPeriod__c where Scheduler_ReferenceId__c = null];
Set<String> SchedSecIDs = new Set<String>();
for(Section_ReportingPeriod__c srp:srpList){
		srp.Scheduler_ReferenceId__c = srp.Section__r.School__c+'-'+srp.Section__r.Schedule_Group__c+'-'+srp.Section__r.Course__c+'-'+srp.Time__c;
		SchedSecIDs.add(srp.Scheduler_ReferenceId__c);
}
update srpList;
List<Scheduled_Section__c> schedSecList = [select id, Needs_Publish__c from Scheduled_Section__c where Section_Reference_Id__c IN:SchedSecIDs];
for(Scheduled_Section__c ss:schedSecList){
	ss.Needs_Publish__c = true;
}
update schedSecList;

If this code doesn't fix the problem, I am meant to run this code below after which also has the same DML error so having this fixed would also be amazing:
 
List<Student_Section__c> studentSections = [select id, Active__c, Enrollment_Start_Date__c,Enrollment_End_Date__c, (select id, Active__c, Current_Record__c, Reference_Id__c, Start_Date__c from Enrollment_Tracking__r) from Student_Section__c where Section__r.Active__c = true];
List<Enrollment_Tracking__c> toUpsert = new List<Enrollment_Tracking__c>();

Integer without=0;
Integer badSetup=0;
for(Student_Section__c ss: studentSections){
	system.debug('testing: '+badSetup);
	if(ss.Enrollment_Start_Date__c == null){
		ss.Enrollment_Start_Date__c = system.today();
	}
	if(ss.Enrollment_Tracking__r.isEmpty()){
		without++;
		if(ss.Active__c){
			toUpsert.add(new Enrollment_Tracking__c(Active__c = true, Current_Record__c = true, Start_Date__c = ss.Enrollment_Start_Date__c, Reference_Id__c=ss.ID+'-'+ss.Enrollment_Start_Date__c.format(),Student_Section__c = ss.ID));
		} else {
			toUpsert.add(new Enrollment_Tracking__c(Active__c = false, Current_Record__c = true, Start_Date__c = ss.Enrollment_Start_Date__c, End_Date__c = ss.Enrollment_End_Date__c, Reference_Id__c=ss.ID+'-'+ss.Enrollment_Start_Date__c.format(), Student_Section__c = ss.ID));
		}
	} else {
		for(Enrollment_Tracking__c et:ss.Enrollment_Tracking__r){
			if(et.Current_Record__c){
				if(et.Active__c != ss.Active__c){
					system.debug(ss.ID);
					badSetup++;
					
					if(et.Start_Date__c == ss.Enrollment_Start_Date__c){
						et.Active__c = ss.Active__c;
						toUpsert.add(et);
					} else {
						et.Current_Record__c = false;
						toUpsert.add(et);
						
						if(ss.Active__c){
							toUpsert.add(new Enrollment_Tracking__c(Active__c = true, Current_Record__c = true, Start_Date__c = ss.Enrollment_Start_Date__c, Reference_Id__c=ss.ID+'-'+ss.Enrollment_Start_Date__c.format(),Student_Section__c = ss.ID));
						} else {
							toUpsert.add(new Enrollment_Tracking__c(Active__c = false, Current_Record__c = true, Start_Date__c = ss.Enrollment_Start_Date__c, End_Date__c = ss.Enrollment_End_Date__c, Reference_Id__c=ss.ID+'-'+ss.Enrollment_Start_Date__c.format(),Student_Section__c = ss.ID));
						}
					}
				}
			}
		}
	}
}
system.debug('Without: '+without);
system.debug('badSetup: '+badSetup);
system.debug(toUpsert.size());
system.debug(toUpsert);
//upsert toUpsert Reference_Id__c;

Thats all the code!  Thanks so much for your help, its really appreciated!!!
 
Scott WalkerScott Walker
Hi karthikeyan.  Did you have any luck with this at all?  I appreciate your help!
Scott WalkerScott Walker
Oh really!  I'm sorry its tricky, thanks for working on this.  
karthikeyan perumalkarthikeyan perumal
Hello 

save the below class 
global class batchUpdate implements Database.Batchable
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        List<Section_ReportingPeriod__c> srpList = [select id, Scheduler_ReferenceId__c, Section__r.School__c,  Section__r.Schedule_Group__c, Section__r.Course__c, Time__c from Section_ReportingPeriod__c where Scheduler_ReferenceId__c = null];
        Set<String> SchedSecIDs = new Set<String>();
        for(Section_ReportingPeriod__c srp:srpList){
		srp.Scheduler_ReferenceId__c = srp.Section__r.School__c+'-'+srp.Section__r.Schedule_Group__c+'-'+srp.Section__r.Course__c+'-'+srp.Time__c;
		SchedSecIDs.add(srp.Scheduler_ReferenceId__c);
		}
        
        String query = 'select id, Needs_Publish__c from Scheduled_Section__c where Section_Reference_Id__c IN:SchedSecIDs';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Scheduled_Section__c> scope)
    {
         for(Scheduled_Section__c ss : scope)
         {   
             
             ss.Needs_Publish__c = true;     
         }
         update schedSecList;
    }   
    global void finish(Database.BatchableContext BC)
    {
    }
}

and open the developer console 

use below code to excute
 
batchUpdate b = new batchUpdate ();
Datbase.executeBatch(b);
Howp this will help you 

Thanks
karthik
 
This was selected as the best answer
Scott WalkerScott Walker
Thak you so so so much for this!!!  I will run this over the weekend and report back to you!


Amazing you did this