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
Charlie CoxCharlie Cox 

DML Error uploading attachments from VF Page

Hello All!

I am getting a DML Error attaching an attachment to a custom object in production. It works fine in sandbox just not produciton. Both production and the sandbox use a site

public PageReference uploadDoc() {
      // if no file has been selected, throw error message
      if (attachment.name == null && application.AppRoughDraft__c == null) {
            attachment.body = null;
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'You must upload a document or a rough draft'));
            return null;
        }
        
        // select the related applicant ID and the uploaded evaluation checkbox from evaluation record
        // External_Update__c eval = [SELECT Id, Applicant__c, EvalUploaded__c FROM External_Update__c WHERE Id =: ID];
        
        // set basic information (pulled from visualforce page)
        attachment.OwnerId = '005G0000003tiMx'; // since this is an external file, there is no owner. This ID is currently lucey's
        Event_Registration__c e1 = [SELECT id, name FROM Event_Registration__c WHERE Pertains_To__c =: 'CallForP' AND LastName__c =: c.lastName AND First_Name__c =: c.firstName];
        if(e.size() == 0) {
            attachment.ParentId = application.id; // record ID
        }
        else {
            list<Attachment> oldAtt = [SELECT id, name FROM attachment WHERE parentId =: e.get(0).id LIMIT 1];
            if(oldAtt.size() > 0) {
                delete oldAtt; // delete old attachment when you upload a new one
            }
            attachment.ParentId = e.get(0).id;
        }
        attachment.IsPrivate = False;
        string originalName = attachment.Name;
        
        string extension;
        // extract extension from file name
        if(attachment.name != null) {
            extension = originalName.substring(originalName.length() - 4, originalName.length()).toLowerCase(); // get the .doc, .pdf, etc.
        
        // if the file is not a .pdf document, throw error message and do not attach
        if (extension != '.pdf' && extension != '.doc' && extension != 'docx') {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Only .pdf, .doc, or .docx documents are accepted. Please convert your document.'));
            attachment.body = null;
            return null;
            
        } else { // otherwise, change file name to TeacherEvaluation.pdf
            attachment.name = c.firstName + '-' + c.lastName + 'Submission' + extension;
        }
        
        list<Attachment> allAttacts = [SELECT id, Name FROM Attachment WHERE parentId =: application.id];
        // if an evaluation has already been uploaded, throw an error message and do not upload
        if (allAttacts.size() > 0) {
            attachment.body = null;
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Please delete previous version before uploading.'));
            return null;
        } else { // begin uploading
            try {
                insert attachment;
                //eval.EvalUploaded__c = True; // change uploaded eval checkbox to True
                //upsert eval; // save changes
            } catch (DMLException e) { // if an error occurs, throw error message
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO, 'Error uploading attachment'));
                attachment.body = null;
                return null;
            } finally {
                attachment = new Attachment();
            } 
        } 
    }
        pageReference reRend = new PageReference('/apex/NUBSThankYou');
        reRend.setRedirect(true);
        return reRend;
        }    
        
Here is the method used to upload the attachment. I keep getting a 'Error uploading attachment' error meaning I am catching the DML exception e. Does anyone have any solutions? 

Thanks!