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
S.Mac-IntoshS.Mac-Intosh 

Batch Insert

Hello,

Atm i'm getting the error: Too many query rows: 50001
This because the functionality will insert up to 5000 records.
The functionality works fine but Salesforce gives me this error because he thinks its wrong.
How can i prevent this error? Some sources told me that batch apex should work but i didn't succeed in implementing this.

Code snippit:
01 public void CreateSOLocations(){
02            
03 
04            
05            Fill_Item_Ids();
06            
07            
08            
09            Check_Amount_Items();       
10            
11            
12            String VGWO_ID = Get_WO();
13            String Customer_Account = Get_Main_Acc();
14            
15            for(AggregateResult a: GroupBy_Project_Locations){
16                             
17                PBSI__PBSI_Sales_Order__c NewRecord = new PBSI__PBSI_Sales_Order__c();
18                id myid = string.valueof(a.get('Account_Location_ID__c'));
19                /// system.debug('Blabla  ' + myid);
20                NewRecord.Account_Location__c = myid;
21                NewRecord.PBSI__Customer__c = Customer_Account;
22                NewRecord.VG_Work_Order__c = VGWO_ID;
23                NewRecord.Project__c = ProjectID;                       
24 
25                NewSOs.add(NewRecord);
26                
27                }
28                Insert NewSOs;
29                
30                integer i = 0;
31                
32                for(AggregateResult a: GroupBy_Project_Locations){
33                
34                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item1')));
35                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item2')));
36                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item3')));
37                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item4')));
38                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item5')));
39                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item6')));
40                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item7')));
41                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item8')));
42                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item9')));
43                Amount_ProjectItem.add(integer.valueof(a.get('Totaal_Item10')));
44                 
45                for(Integer c = 0; c < Amount_DifferentItems ;c++ ){
46                           
47                    PBSI__PBSI_Sales_Order_Line__c NewSOLine = new PBSI__PBSI_Sales_Order_Line__c();
48                    NewSOLine.PBSI__Sales_Order__c = NewSOs[i].id;
49                    NewSOLine.PBSI__Item__c = Id_ProjectItem[c];
50                    NewSOLine.PBSI__Quantity_Needed__c = Amount_ProjectItem[c];
51                    
52                    if(NewSOLine.PBSI__Quantity_Needed__c != NULL){
53                        NewSOLines.add(NewSOLine);
54                    }
55                    
56                    
57                    
58                    }
59            i++;
60            Amount_ProjectItem.clear();
61                       
62                       
63            }
64            
65            Insert NewSOLines;     
66      
67 
68         }


Can someone help me out with this?
S.Mac-IntoshS.Mac-Intosh
Anyone who can help?
Cloud_forceCloud_force
You are getting this error because you are trying to query records more than 50k, an SQL query should always return less than 50k records[this is a govergers limit imposed by salesforce]. Optimise your query to return less number of records. You can use filters or if you cannot put any filters then atleast make sure you use limit clause
ex: select id from account limit 20000.

for processing huge number of records you should use batch apex.
see this for batch batch apex:http://www.forcexplore.com/2014/04/what-is-batch-apex-class.html

T
hanks,
http://www.forcexplore.com
S.Mac-IntoshS.Mac-Intosh
Hi,

Thanks for your answer.
I've tried to use the batch apex but i wont work at me.
Can you help me with this or do you have a working example for me?

Thanks,

Sergio