2

I'm trying to create interface for loading images and here is my code:

Visualforce Page:

<apex:page controller="LogoManager" contentType="text/html" readonly="true">
    <head>
        <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" />
    </head>
    <body>
        <script type="text/javascript">            
            function FileSize() {
                var fileInput = $('#idFileUpload');
                var maxSize = fileInput.data('max-size');
                if(fileInput.get(0).files.length)
                {
                    var fileSize = fileInput.get(0).files[0].size; // in bytes
                    if(fileSize>maxSize)
                    {
                        alert('file size is more than 2MB');
                        return false;
                    }
                    else
                    {                       
                        return true;
                    }
                }
                else
                {
                    alert('Please select the file to upload');
                    return false;
                }
            }
        </script>
        <apex:form id="upload-form">
            <apex:pageMessages />
            <br/>
            <apex:outputPanel rendered="{!accountID != ''}">
                <h2>Account Name: <label>{!accountName}</label></h2>
                <br/><br/>
                Please select image: 

<apex:inputFile value="{!doc.body}" filename="{!doc.name}" id="file"/>
                <br/><br/>
                <apex:commandButton value="Upload" onClick="if (FileSize() === false) { return false; }" action="{!SaveAttachment}"/>   
            </apex:outputPanel>
            <apex:outputPanel rendered="{!accountID == ''}">
                No valid account selected
            </apex:outputPanel>
        </apex:form>
    </body>
</apex:page>

Controller code:

public with sharing class LogoManager{    
    public String accountID { get; set; }
    public String accountName { get; set; }
    public String attBody { get; set; }
    public LogoManager()
    {
        accountID = ApexPages.currentPage().getParameters().get('ID');
        if (accountID == null)
        {
            accountID = '';
        }
        if (accountID != '')
        {
            Account a = [select Name from Account where id = :accountID];
            if (a == null)
            {
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please upload a jpg file'));

            }
            else
            {
                accountName = a.Name;
            }
        }
    }

    public Pagereference SaveAttachment()
    {
        doc.AuthorId = UserInfo.getUserId();
        doc.FolderId = UserInfo.getUserId(); //update this folder to Account Logo Folder        
        try
        {
            insert doc;
        }
        catch (DMLException e)
        {
            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading file'));
            return null;
        }
        finally
        {
            doc.body = null; // clears the viewstate
            doc = new Document();
        }       
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
        return null;
    }

    public Document doc
    {
        get 
        {
            if (doc == null)
            {
                doc = new Document();
                doc.Name = accountName;
                doc.description = '';
                doc.keywords = '';
            }
            return doc;
        }
        set;
    }
}

I get following error:

Too many DML statements: 1 Error is in expression '{!SaveAttachment}' in component in page logomanager: Class.LogoManager.SaveAttachment: line 46, column 1

Not sure what is causing this error and how do I go about fixing it. Any recommendations will be appreciated.

3

Too many DML statements: 1 Error

means your apex:page readonly attribute false

Setting Read-Only Mode for an Entire Page

Giving the attribute readonly will give that element the readonly status. It doesn't matter what value you put after it or if you put any value after it, it will still see it as readonly. Putting readonly="false" won't work.

Issue related to your VF page is readonly="true" means you can't perfom DML in controller

Just change this to readonly="false"

or you can remove this attribute. readonly attribute default value is false

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.