You need to sign in to do that
Don't have an account?
Developer.mikie.Apex.Student
Please Help with Apex Exception - CSV upload controller - Any Tips would be much appreciated
I have created a tool which allows me to upload a CSV, it will then upload what it can and then those that it cant (if they fall under two criteria), will be added to an errorlist, which will be downloaded. It works pretty well except for a few things: It seems to have a random limit on the ampunt of records I can upload at a time. Sometimes its 4 sometimes 5....but, it is designed to allow tens at a time. This is the email exception i get when it fails:
caused by: System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.
It also seems to be randomly changing one field in another object call AccountStatus.
Please, anyone with knowledge of Apex, please look at my code and give me any tips that they can. Thank you in advance for your help
caused by: System.TypeException: Cannot have more than 10 chunks in a single operation. Please rearrange the data to reduce chunking.
It also seems to be randomly changing one field in another object call AccountStatus.
Please, anyone with knowledge of Apex, please look at my code and give me any tips that they can. Thank you in advance for your help
public class FileUploader_TraEziDebit { public string nameFile{get;set;} public Blob contentFile{get;set;} String[] filelines = new String[]{}; public List<Transaction__c> EziDebitUpload; public List<Transaction__c> EziDebitError; public ID DocId {get; set;} //Introduce string which will link the transactions to the correct Account Set<String> AccLinks = new Set<String>(); Set<String> SerLinks = new Set<String>(); List<Account> existAccounts = new List<Account>(); List<Service__c> existServices = new List<Service__c>(); List<String> EziDebitList = new List<String>(); Map<String, ID> AccMap = new Map<String, ID>(); // Define a new Account map Map<String, ID> OffMap = new Map<String, ID>(); // Define a new Office map Map<String, ID> SerMap = new Map<String, ID>(); // Define a new Service map Datetime mydate = System.Now(); String S = mydate.format('dd/MM/yyyy'); String EziDebitId; String TransactionError; //Error Message Integer UploadCount; Integer ErrorCount; public FileUploader_TraEziDebit(Transaction_Query controller){ filelines = new String[]{}; EziDebitUpload = New List<Transaction__c>(); EziDebitError = New List<Transaction__c>(); UploadCount = 0; ErrorCount = 0; } public Pagereference ReadFile() { List<Account> existAccounts = [SELECT Id, Office__c, Ezidebit_Payer_ID__c FROM Account where Ezidebit_Payer_ID__c != null]; if(existAccounts != null && existAccounts.size() > 0 ){ for(Account Acc : existAccounts){ AccLinks.add(Acc.Ezidebit_Payer_ID__c); AccMap.put(Acc.Ezidebit_Payer_ID__c, Acc.Id); OffMap.put(Acc.Ezidebit_Payer_ID__c, Acc.Office__c); } } List<Service__c> existServices = [SELECT Id, Payment_Choice__c, UpperCaseId__c , Amount_remaining__c FROM Service__c]; if(existServices != null && existServices.size() > 0 ){ for(Service__c Ser : existServices){ String SerID = ser.id; // String SerID15 = SerID.substring(0, 15); //Changed from substring of 15 SerLinks.add(SerID); SerMap.put(Ser.UpperCaseId__c,SerID); } } nameFile=contentFile.toString(); filelines = nameFile.split('\n'); EziDebitUpload = new List<Transaction__c>(); for (Integer i=1;i<filelines.size();i++) { String[] inputvalues = new String[]{}; inputvalues = filelines[i].split(','); String SerRef = string.valueof(inputvalues[2]); String SerMatch = SerRef.substring(0, 18); String SerKey = SerMap.get(SerMatch); if( AccLinks.contains(inputvalues[1]) && SerLinks.contains(SerKey) ){ Transaction__c Tra = new Transaction__c() ; Tra.date_of_payment__c = date.parse(inputvalues[0]); EziDebitId = inputvalues[1]; Tra.Ezidebit_Payer_ID__c = inputvalues[1]; Tra.Account__c = AccMap.get(EziDebitId); Tra.Office__c = OffMap.get(EziDebitId); Tra.Destiny_Service_No__c = SerKey; Tra.Amount__c = decimal.valueof(inputvalues[3]); Tra.Method__c = 'EZIDEBIT'; Tra.Transaction_Type__c = 'Part Payment'; EziDebitUpload.add(Tra); UploadCount = UploadCount+1; }else{ //Creating error records //! at the front will negate the statement Transaction__c Tra = new Transaction__c(); TransactionError = 'Error: '; Tra.date_of_payment__c = date.parse(inputvalues[0]); EziDebitId = inputvalues[1]; Tra.Ezidebit_Payer_ID__c = inputvalues[1]; if(AccLinks.contains(inputvalues[1])){ TransactionError = TransactionError+'EziDebit Player ID was found, '; }else if(!AccLinks.contains(inputvalues[1])){ TransactionError = TransactionError+'EziDebit Player ID not found on Account, '; } if(SerLinks.contains(SerKey)){ TransactionError = TransactionError+'Service ID was found in the org.'; }else if(!SerLinks.contains(SerKey)){ TransactionError = TransactionError+'Service ID not found.'; } Tra.Account__c = AccMap.get(EziDebitId); Tra.Office__c = OffMap.get(EziDebitId); Tra.Destiny_Service_No__c = SerMap.get(SerMatch); Tra.Amount__c = decimal.valueof(inputvalues[3]); Tra.Method__c = 'EZIDEBIT'; Tra.Transaction_Type__c = 'Part Payment'; tra.EziDebit_Error__c = TransactionError; EziDebitError.add(Tra); ErrorCount = ErrorCount+1; } } string header = 'Transaction Date,Ezidebit Payer ID,Client Contract Ref, Schedule Collected , Actual Collected , Transaction Fee , Credit Card Fee ,Transaction_Type, Error Message\n'; string finalstr = header ; //Error CSV Header if(EziDebitError != null && EziDebitError.size() > 0 ){ for(Transaction__c tran: EziDebitError) { string recordString = '"'+Tran.date_of_payment__c+'","'+Tran.Ezidebit_Payer_ID__c+'","'+Tran.Destiny_Service_No__c+'","'+Tran.Amount__c+'","'+''+'","'+''+'","'+''+'","'+Tran.Transaction_Type__c+'","'+Tran.EziDebit_Error__c+'"\n'; finalstr = finalstr +recordString; } Blob csvBlob = Blob.valueof(finalstr); Document tDoc = new Document(); tDoc.Name = 'Error_EziDebit #'+S; tDoc.Type = 'csv'; tDoc.body = csvBlob; tDoc.FolderId = '00l90000001aggJ'; /* -00lN0000000M9Pm- id of whatever Salesforce document folder you want to use*/ insert tDoc; DocId = tDoc.id; /*after this, you can use the ID from your newly inserted document as a URL parameter for the download link*/ ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'There is an error with '+ErrorCount+' of the EziDebit Payer Ids, please click the link to download the errors'); ApexPages.addMessage(errormsg); } try{ if(EziDebitUpload != null && EziDebitUpload.size() > 0 ){ insert EziDebitUpload; ApexPages.Message Successmsg = new ApexPages.Message(ApexPages.severity.confirm, UploadCount+' EziDebit Transactions have been uploaded'); ApexPages.addMessage(Successmsg); } } catch (Exception e) { ApexPages.Message errormsg = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured. Please check the template or try again later'); ApexPages.addMessage(errormsg); } return null; } }
if(EziDebitUpload != null && EziDebitUpload.size() > 0 ){
insert EziDebitUpload;
}
before insert statement use EziDebitUpload.sort(); method to sort instance of sobject with their label.
for more detail refer link.
https://bartoszborowiec.wordpress.com/2014/06/15/execution-failed-system-typeexception-cannot-have-more-than-10-chunks-in-a-single-operation-please-rearrange-the-data-to-reduce-chunking/
All Answers
if(EziDebitUpload != null && EziDebitUpload.size() > 0 ){
insert EziDebitUpload;
}
before insert statement use EziDebitUpload.sort(); method to sort instance of sobject with their label.
for more detail refer link.
https://bartoszborowiec.wordpress.com/2014/06/15/execution-failed-system-typeexception-cannot-have-more-than-10-chunks-in-a-single-operation-please-rearrange-the-data-to-reduce-chunking/
Hi Mate,
I attempted your solution and it still failed, but it led me to read the apex exception. This was for a managed packagae, I visited the Github and Bam, solution i merely needed to update.
P.S. I am pretty sure there solution was the same as yours. so thank you for your help.