You need to sign in to do that
Don't have an account?

List of index out of bounds : 1
I'm getting this error from a class referenced by an Email Service. From what I understand, the error is kicked off when a reference is made to a list which exceeds the number of items in the list.
In this case, the class is referencing the attachments included in an email. Ultimately, it intends to re-save the attachment as a file in a SF record. Here are the lines which are causing the error. I will also include the entire class later in this post :
//Set file Name fileName = fileStartName+pCR.Contact__r.Name +' )'; version = Integer.valueOf(attachment.Name.split('V1.')[1].removeEnd('.csv').isNumeric() ? attachment.Name.split('V1.')[1].removeEnd('.csv') : '0'); fileName += '.csv';
In the debug logs the line beginning with 'version' seems to be the line causing the error.
I am confused by the code so would really appreciate any input which you can provide on why this error is happening. I do know that the code will work some times. Thank you very much.
I have included the whole class here :
global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email,Messaging.InboundEnvelope envelop) { Messaging.InboundEmailResult result = new Messaging.InboundEmailResult(); //String to hold message String message = ''; String fileStartName = ''; String fileEndName = ''; String fileName = ''; Integer version = 0; //List to hold Error log records List<Error_Log__c> errorLogs = new List<Error_Log__c>(); //Get From Address String fromAddress = email.FromAddress; //Get Contact Contact contactRecord; List<Contact> contacts = [SELECT Id , Name , AccountId FROM Contact WHERE Email =: fromAddress ORDER BY Name DESC LIMIT 1]; if(contacts.size() > 0) contactRecord = contacts[0]; if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) { //Map to hold the Funder Contact Role Details with Funder Map<Id, Program_Contact_Role__c> mapPCRsWithProgram = new Map<Id, Program_Contact_Role__c>(); //List to hold Content versions List<ContentVersion> contentVersions = new List<ContentVersion>(); for(integer i =0, s = email.binaryattachments.size(); i < s; i++) { if(email.binaryAttachments[i].mimeTypeSubType.equals('application/vnd.ms-excel') || email.binaryAttachments[i].mimeTypeSubType.equals('text/csv') ||email.binaryAttachments[i].filename.contains('.csv')) { try { System.debug('Attachment matched a criteria'); //Get Attachment details List<String> attachmentSplitsByRows = new List<String>(); //Insatnce of attachemnts Attachment attachment = new Attachment(); // Get attachment name and body attachment.Name = email.binaryAttachments[0].filename; attachment.Body = email.binaryAttachments[0].body; System.debug('Attachment Body '+(attachment.Body).toString()); //Convert to string String attachmentBody = (attachment.Body).toString(); attachmentBody = attachmentBody.unescapeCsv(); //Split the data by new line attachmentSplitsByRows = attachmentBody.split('\n'); //Set to hold the Onbording Ids Set<Id> setOnbordingIds = new Set<Id>(); //Map to hold the Onbording Record Map<Id, Program_Onboarding__c> mapProgramOnboardings = new Map<Id, Program_Onboarding__c>(); //Set to hold the Program Onbording ids to check Funder is already approved or not Set<Id> setOfProgOnboardingIds = new Set<Id>(); //List of Milestone to create a new Milestone List<Milestone__c> milestones = new List<Milestone__c>(); Boolean firstRow = true; //Loop over the Attachments for(String csvRow : attachmentSplitsByRows) { //Skip header row if(!firstRow){ //Get the onbording details List<String> attachmentSplitByColumns = csvRow.split(','); //Add Onbording Id in set setOnbordingIds.add(attachmentSplitByColumns[0]); } firstRow = false; } firstRow = true; //Check onbordings if(setOnbordingIds.size() > 0) { //Get Onboardings mapProgramOnboardings = new Map<Id, Program_Onboarding__c>([Select Id, Supplier__c, Supplier__r.Supplier__c, Funder__c, Program__c From Program_Onboarding__c Where Id IN : setOnbordingIds]); //Get Milestones for(Milestone__c ml : [Select Id , Onboarding__c FROM Milestone__c WHERE Onboarding__c IN: setOnbordingIds AND Milestone_Type__c =: Constants.PROGRAM_MILESTONE_MILESTONE_TYPE_APPROVED_BY_FUNDER]) setOfProgOnboardingIds.add(ml.Onboarding__c); //Set to hold the Program Details Set<Id> setProgramIds = new Set<Id>(); //Loop over the Onboardings for(Program_Onboarding__c pO : mapProgramOnboardings.values()) setProgramIds.add(pO.Program__c); //Set file Name fileStartName = 'Response - '+attachment.Name.split('.csv')[0].trim()+' - ( '; //Get Funder Program Contact Role for(Program_Contact_Role__c pCR : [Select Id, Program_Name__c, Funder__c , Contact__c , Contact__r.Name From Program_Contact_Role__c Where Program_Name__c IN : setProgramIds AND Contact__r.Email =: fromAddress AND RecordTypeId =: Constants.PROGCONTACTROLE_RT_ID_FUNDER]){ mapPCRsWithProgram.put(pCR.Program_Name__c, pCR); //Set file Name fileName = fileStartName+pCR.Contact__r.Name +' )'; version = Integer.valueOf(attachment.Name.split('V1.')[1].removeEnd('.csv').isNumeric() ? attachment.Name.split('V1.')[1].removeEnd('.csv') : '0'); fileName += '.csv'; //Insert File under Program // convert from string to blob Blob tempBlob = attachment.Body; // convert base64 encode from the blob string csvFile = EncodingUtil.base64Encode(tempBlob); //Decoding base64Data String base64Data = EncodingUtil.urlDecode(csvFile, 'UTF-8'); ContentVersion contentVersionData = new ContentVersion(); contentVersionData.Title = fileName; contentVersionData.PathOnClient = '/' + fileName; contentVersionData.FirstPublishLocationId = pCR.Program_Name__c; contentVersionData.VersionData = EncodingUtil.base64Decode(base64Data); contentVersionData.IsMajorVersion = true; //Add content version contentVersions.add(contentVersionData); } } System.debug('attachmentSplitsByRows '+attachmentSplitsByRows[0].split(',').size()); if(attachmentSplitsByRows.size()>0 && attachmentSplitsByRows[0].split(',').size() > 8){ //Loop over the Onbording details for(String csvRow : attachmentSplitsByRows){ //Skip header row if(!firstRow){ //Get the onbording details List<String> attachmentSplitByColumns = csvRow.split(','); //Get last column Integer lastColumn = attachmentSplitByColumns.size(); if(attachmentSplitByColumns.size() > 8){ //Check Approved by funder is Yes or True then update program onboarding status to 'Trading Set Up - Pending Supplier' if(attachmentSplitByColumns.size() > 8 && String.isNotBlank(attachmentSplitByColumns[0]) && !setOfProgOnboardingIds.contains(attachmentSplitByColumns[0]) && (attachmentSplitByColumns[lastColumn-1].trim().equalsIgnoreCase('True') || attachmentSplitByColumns[lastColumn-1].trim().equalsIgnoreCase('Yes'))){ //Update onbording mapProgramOnboardings.get(attachmentSplitByColumns[0]).Status__c = Constants.ONBOARDING_STATUS_PENDING_SUPPLIER; //Add a new Milestone milestones.add(milestone(mapProgramOnboardings.get(attachmentSplitByColumns[0]), mapPCRsWithProgram, Constants.PROGRAM_MILESTONE_MILESTONE_TYPE_APPROVED_BY_FUNDER)); } else if(attachmentSplitByColumns.size() > 8 && String.isNotBlank(attachmentSplitByColumns[0]) && !setOfProgOnboardingIds.contains(attachmentSplitByColumns[0]) && (attachmentSplitByColumns[lastColumn-1].trim().equalsIgnoreCase('False') || attachmentSplitByColumns[lastColumn-1].trim().equalsIgnoreCase('No'))){ //Update onbording mapProgramOnboardings.get(attachmentSplitByColumns[0]).Status__c = Constants.ONBOARDING_STATUS_BUYER_REVIEW; //Add a new Milestone milestones.add(milestone(mapProgramOnboardings.get(attachmentSplitByColumns[0]), mapPCRsWithProgram, Constants.PROGRAM_MILESTONE_MILESTONE_TYPE_BUYER_REVIEW_REQUESTED)); }else if(setOfProgOnboardingIds.contains(attachmentSplitByColumns[0])){ message = 'Funder File is already processed.'; if(mapProgramOnboardings.containsKey(attachmentSplitByColumns[0])){ //Add error Log errorLogs.add(errorLog(mapProgramOnboardings.get(attachmentSplitByColumns[0]) , message , contactRecord)); } }else if(attachmentSplitByColumns.size() > 8 && (!attachmentSplitByColumns[lastColumn-1].trim().equalsIgnoreCase('Yes') || !attachmentSplitByColumns[lastColumn-1].trim().equalsIgnoreCase('No'))){ message = 'Funder replies back with '+attachmentSplitByColumns[lastColumn-1].trim()+' instead of Yes or No.'; //Add error Log errorLogs.add(errorLog(mapProgramOnboardings.get(attachmentSplitByColumns[0]) , message , contactRecord)); } }else{ message = 'Funder replies back with blank value instead of Yes or No.'; //Add error Log errorLogs.add(errorLog(mapProgramOnboardings.get(attachmentSplitByColumns[0]) , message , contactRecord)); } } firstRow = false; } }else{ message = 'Funder file formate is not correct , please check funder file.'; //Add Error log errorLogs.add(errorLog(null , message , contactRecord)); } //check size if(mapProgramOnboardings.size() > 0) update mapProgramOnboardings.values(); //Check milestone list if(milestones.size() > 0) insert milestones; //Check content versions if(contentVersions.size() >0) insert contentVersions; }catch(Exception ex){
Thank you.
Hope your query is answered below.
https://salesforce.stackexchange.com/questions/322008/list-of-index-out-of-bounds-1
Thanks,