+ Start a Discussion
Platy ITPlaty IT 

Issues attaching multiple files

I have a Visualforce form used in Sites to submit new Cases.  The form allows people to attach files to the new Case and has been in production working for about 6 months.  In the last couple weeks, suddenly people are receiving errors when attaching more than 2 files, even though this previously worked and the code hasn't been updated.  

 

In my testing, I don't receive an explicit error message but I am finding that if I attach more than 2 files, only the first and last one that's inserted in the code ends up saved on the Case.  So if I select 5 files, only 1 and 5 are saved.  When I review the debug log, it's as if the files in the middle don't even exist, there's no record of the code even trying to insert them, and there's no error messages. 

 

This isn't a view state error, the page correctly gives an error message if any of the files are too big,   I've tried re-writing the actual attachment insert code a few different ways, and the result is always the same- only the first and last files are saved.  Has anyone else experienced similar issues recently?  Since this worked perfectly well before, this is striking me as a bug on the Salesforce side of things, but I'm not sure how to convince support of this so they don't automatically close my help desk case. I'm sadistically hoping others have hit this as well as evidence for my claim.

Platy ITPlaty IT

Here are a couple ways I've tried handling inserting the attachments, both result in the issue I described.

 

Method 1- 

    public Attachment att1 {get;set;}
    public Attachment att2 {get;set;}
    public Attachment att3 {get;set;}
    public Attachment att4 {get;set;}
    public Attachment att5 {get;set;}

    public PageReference SaveCase(){
        //....other code for saving the case and new contacts...
         List<Attachment> newAttaches = new List<Attachment>();
        newAttaches.add(att1);
        newAttaches.add(att2);
        newAttaches.add(att3);
        newAttaches.add(att4);
        newAttaches.add(att5);
        System.debug('ABOUT TO PROCESS ATTACHMENTS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
        for (Attachment newAttach : newAttaches){
            if (newAttach.Name != null){
                try{
                    
                    newAttach.parentId = temp_case.Id;
                    insert newAttach;
                    newAttach.Body = null;
                    newAttach = new Attachment();
                } catch( Exception e ){
                    ApexPages.addMessage(new ApexPages.Message(
                            ApexPages.severity.ERROR,
                            'There was an error uploading this file, please re-attach and try again.'
                        ));
                    Database.rollback( sp );
                    AttSetup();
                          
                    return null;
                } finally {
                    newAttach.body = null; // clears the viewstate
                    newAttach = new Attachment();
                }
            }
        }
        isSubmitted = true;
        return null;
    }

    public void AttSetup(){
        if (att1 != null){
            att1.body = null;
            att2.body = null;
            att3.body = null;
            att4.body = null;
            att5.body = null;
        }
        att1 = new Attachment();
        att2 = new Attachment();
        att3 = new Attachment();
        att4 = new Attachment();
        att5 = new Attachment();
    }

 

Method 2- 

    public transient Blob file1 {get;set;}
    public String filename1 {get;set;}
    public transient Blob file2 {get;set;}
    public String filename2 {get;set;}
    public transient Blob file3 {get;set;}
    public String filename3 {get;set;}
    public transient Blob file4 {get;set;}
    public String filename4 {get;set;}
    public transient Blob file5 {get;set;}
    public String filename5 {get;set;}

    public PageReference SaveCase(){
        //other code to save the Case and new Contacts...
        if (filename1 != null){
        	Attachment tempattach = new Attachment();
        	tempattach.Name = filename1;
        	tempattach.Body = file1;
        	newAttaches.add(tempattach);
        	try{
                    
                tempattach.parentId = temp_case.Id;
                insert tempattach;
                tempattach.Body = null;
            } catch( Exception e ){
                ApexPages.addMessage(new ApexPages.Message(
                        ApexPages.severity.ERROR,
                        'There was an error uploading this file, please re-attach and try again.'
                    ));
                Database.rollback( sp );    
                return null;
            } finally {
                tempattach.body = null; // clears the viewstate
            }
        }
        if (filename2 != null){
        	Attachment tempattach = new Attachment();
        	tempattach.Name = filename2;
        	tempattach.Body = file2;
        	try{
                    
                tempattach.parentId = temp_case.Id;
                insert tempattach;
                tempattach.Body = null;
            } catch( Exception e ){
                ApexPages.addMessage(new ApexPages.Message(
                        ApexPages.severity.ERROR,
                        'There was an error uploading this file, please re-attach and try again.'
                    ));
                Database.rollback( sp );    
                return null;
            } finally {
                tempattach.body = null; // clears the viewstate
            }
        }
        if (filename3 != null){
        	Attachment tempattach = new Attachment();
        	tempattach.Name = filename3;
        	tempattach.Body = file3;
        	try{
                    
                tempattach.parentId = temp_case.Id;
                insert tempattach;
                tempattach.Body = null;
            } catch( Exception e ){
                ApexPages.addMessage(new ApexPages.Message(
                        ApexPages.severity.ERROR,
                        'There was an error uploading this file, please re-attach and try again.'
                    ));
                Database.rollback( sp );    
                return null;
            } finally {
                tempattach.body = null; // clears the viewstate
            }
        }
        if (filename4 != null){
        	Attachment tempattach = new Attachment();
        	tempattach.Name = filename4;
        	tempattach.Body = file4;
        	try{
                    
                tempattach.parentId = temp_case.Id;
                insert tempattach;
                tempattach.Body = null;
            } catch( Exception e ){
                ApexPages.addMessage(new ApexPages.Message(
                        ApexPages.severity.ERROR,
                        'There was an error uploading this file, please re-attach and try again.'
                    ));
                Database.rollback( sp );    
                return null;
            } finally {
                tempattach.body = null; // clears the viewstate
            }
        }
        if (filename5 != null){
        	Attachment tempattach = new Attachment();
        	tempattach.Name = filename5;
        	tempattach.Body = file5;
        	try{
                    
                tempattach.parentId = temp_case.Id;
                insert tempattach;
                tempattach.Body = null;
            } catch( Exception e ){
                ApexPages.addMessage(new ApexPages.Message(
                        ApexPages.severity.ERROR,
                        'There was an error uploading this file, please re-attach and try again.'
                    ));
                Database.rollback( sp );    
                return null;
            } finally {
                tempattach.body = null; // clears the viewstate
            }
        }
        isSubmitted = true;
        return null;
    }

 

I know this second method isn't very efficient, but I was trying to track down exactly where things were going wrong, unsuccessfully.