0

Already Gone through many different links related to this Error, but still in problem. Getting this error when I am uploading large size file. If I am using the Transient keyword then my i am unable to update my Document, Please suggest me what to do!!!

Vf page

<apex:page docType="HTML-5.0" controller="AdminPannelController" id="pgId">
<apex:form id="frmId" >
        <apex:pageBlock title="Live Agent Configure " id="pgBlockId">
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockSection id="pbsection" columns="2">
<apex:outputLabel value="LOGO: ">
                     <apex:outputText value="{!callfunc}" escape="false"/>
                    <apex:inputFile value="{!file.body}" fileName="{!file.Name}" fileSize="{!file.BodyLength}"></apex:inputFile>
                </apex:outputLabel>

<apex:pageBlockButtons location="Bottom" id="pbButton">
                <apex:commandButton value="Save" action="{!Save}"/>
                <apex:commandButton value="Reset" action="{!reset}"/>
            </apex:pageBlockButtons> 
        </apex:pageBlock>

    </apex:form>

</apex:page>

My controller

    public with sharing class AdminPannelController{
       transient public Document file{get;set;}
     public String textUrl{get;set;}

    transient public List<brand__mdt> BrandTypes = new List<brand__mdt>();

     public AdminPannelController(){
         file = new Document();
         flag = false;
     }

  public void save(){


     List<brand__mdt> productNameFromMetaData = [Select DeveloperName from brand__mdt where id =: metaData limit 1];
     List<Folder> existingFolder = [Select Id,Name from Folder where Name = 'Product Logo Folder' limit 1];
     List<Folder> existingTextFolder = [Select Id,Name from Folder where Name = 'Product Text Folder' limit 1];     


    Document Doc = new Document();
         try{
             List<AdminPanelBranding__c> existingColorCode = [Select Background_Color__c from AdminPanelBranding__c where Name =: productNameFromMetaData[0].DeveloperName ];
             if(colorCode == ''){
                 system.debug('in if loop of colorCode');
                 if(existingColorCode.size() > 0){
                     colorCode = existingColorCode[0].Background_Color__c;
                 }
             }
             List<Document> existingDoc = [Select Name,Id,Body from Document where Description =: metadata AND FolderId =: existingFolder[0].id limit 1];
             System.debug('existing doc' +existingDoc);
             System.debug(file);

             if(file.Name == null){
                 System.debug('in null loop should insert');
                 if(existingDoc.size() > 0){
                     Doc.id = existingDoc[0].id;
                     Doc.name=file.Name;
                     Doc.Body=file.Body;
                     Doc.Description = metadata;
                     Doc.Folderid = existingFolder[0].id;
                     System.debug(file.BodyLength);
                     update Doc;
                 }
             }
             else{
                 if(existingDoc.size() > 0){
                        System.debug('file'+file);
                        system.debug('size loop');
                        Doc.id = existingDoc[0].id;
                        Doc.name=file.Name;
                        Doc.Body=file.Body;
                        Doc.Description = metadata;
                        Doc.Folderid = existingFolder[0].id;
                        System.debug(file.BodyLength);
                        if(file.BodyLength <= 135000 ){
                            System.debug('in loop of body');
                            update Doc;
                            flag = false;
                        }
                        else{
                            flag = true;
                            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Text Document , Please upload a Text/Plain Document with less than 135kb size');

                        }
                    }
                     else{
                         System.debug('not null and insert file');
                         Doc.name=file.Name;
                         Doc.Body=file.Body;
                         Doc.Description = metadata;
                         Doc.Folderid = existingFolder[0].id;

                         if(file.BodyLength <= 135000 ){
                             system.debug('inside body length');
                             insert Doc;
                             flag = false;
                         }
                         else{
                             flag = true;
                             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error: Invalid Text Document , Please upload a Text/Plain Document with less than 135kb size');
                             ApexPages.addMessage(myMsg);
                         }
                     }
                }
         }Catch(Exception e){
             e.getMessage();
         }
    }
  • what is your method name? – Tushar Sharma Apr 21 '17 at 7:40
  • Sorry @TusharSharma mistakenly it was removed. I have save method in the controller. – Aarya Tiwary Apr 21 '17 at 7:43
2

If you're going with the method you're going with (using a Document), just clear out the Body when you're done in the Save method:

     } Catch(Exception e){
         e.getMessage();
     } finally {
         file.Body = null;
     }

This way, you can remove the transient keyword from the variable so that you can retain all of the other data.

Personally, however, I'd go with an alternative approach of using individual variables so you have greater control over your view state:

public Integer fileSize { get; set; }
public String fileName { get; set; }
public transient Blob fileBody { get; set; }
0

Make you method return type as PageReference and then after successful upload return it to save page or any other method.

public PageReference Save(){
    // your code here
   return new PageReference('/apex/YourPageName');

}

It will solve your problem.

  • Didn't work still not able to save the file. It's still showing the file value null when I am inserting and updating. – Aarya Tiwary Apr 21 '17 at 7:54
  • @Aarya remove the transient variable if not already removed. – Tushar Sharma Apr 21 '17 at 8:16

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.