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
deepu.sandeepdeepu.sandeep 

Attachment size Restriction

Hi,

 

I'm trying to insert a record in to cases object with attachment. Here ineed to put one condition i.e. 

if attachment size is greater thhan 10 mb then record should not insert and a message has to display like"attachment size exceeded limit is 10 mb". I'm trying with the below code its not working can anyone help mein this issue.

 

public with sharing class LibraryAskUsRequestController {

/*
* @description Property section
*/
public String librarycontent { get; set; }
public Case request{get; set;}
public Attachment att {get; set;}
public boolean FacultyResearchRequest {get; set;}
public Transient Blob filebody { get; set; }
public Transient Blob attbody { get; set; }
public String contentType { get; set; }
public Boolean incorrect_Captcha {get;set;}
public boolean attsize_exceeded { get; set; }
public String filename{ get; set; }
public integer attsize {get; set; }

/*
* @description Constructor
*/
public LibraryAskUsRequestController() {

request = new Case();
Attachment att = new Attachment();
librarycontent = TextManagerUtils.getAppText(CoreConstants.ApplicationId.core.name(), CRMCoreConstants.TM_LIBRABY_ASKUS_CONTENT);

}
/*
* @description this method is used to insert new record on clicking Submit button
*/
public PageReference Save() {

try{
if(Recaptcha.response!='') {
/* verifying recapthca */

if(Recaptcha.getVerify())
{
if(attbody!=null)
{
If(attbody.size()>10485760 ){
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'This file exceeds the maximum size limit of 10MB.'));
return null;
}
}
else{

request.Request_Type__c='Ask Us';
request.Origin='Web';
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
dmo.EmailHeader.triggerAutoResponseEmail = true;
request.setOptions(dmo);

if(attbody!=null){

att = new Attachment();
att.body = attbody;
att.Name=filename;
att.BodyLength=10485760;
att.ContentType=contentType;
att.ParentId = request.Id;
insert att;

attbody=null;
att.Body=null;
}
}

insert request;
PageReference confirmation = new PageReference('http://www.google.com');
return confirmation;


}

else{
incorrect_Captcha=true;
return null;
}
}
else{
incorrect_Captcha=true;
return null;
}
}

catch(Exception e) {
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.WARNING, e.getMessage()));
}
finally
{
att = new Attachment();
}

return null;
}

}

qraishqraish

It's essentially same thing but may be we can try something like 

 

ApexPages.Message errormsg = new ApexPages.Message(ApexPages.Severity.WARNING,'This file exceeds the maximum size limit of 10MB.');
ApexPages.addmessage(errormsg);

 

What exactly is the error you're receiving??

 

 

gautam_singhgautam_singh

Hi,

 

I believe you are not inserting the Case and trying to insert the attachment. Insert the Request Case first and then adding the attachment will be the sure shot solution for it.

Have a look over this piece of Code:-

if(attbody!=null)
               {
                   if(attbody.size()>10485760 )
                       {
                                ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'This file exceeds the maximum size limit of 10MB.'));
                               return null;
                       }else{
                            request.Origin='Web';
                            Database.DMLOptions dmo = new Database.DMLOptions();
                            dmo.assignmentRuleHeader.useDefaultRule= true;
                            dmo.EmailHeader.triggerAutoResponseEmail = true;
                            request.setOptions(dmo);
                            insert request;
                            
                            att = new Attachment();
                            att.body = attbody;
                            att.Name = filename;
                            att.ContentType=contentType;
                            att.ParentId = request.Id;
                            insert att;
                            
                            PageReference confirmation = new PageReference('http://www.google.com');
                            return confirmation;
                            
                 }
                 }

 

Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

deepu.sandeepdeepu.sandeep
Hi qraish,

 

Thanks its working fine, but i'm unable to cover the test coverage for this class.

I'm able to get only 45% with the following test class

 

@isTest(SeeAllData=true)
private class TestLibraryAskUsRequestController {

static testMethod void RequestCreation() {
Case req = new Case();
req.SuppliedEmail='gsb@stanford.edu';
req.Requestor_affiliation__c='Stanford Student';
req.Request__c='Library Request';
req.Request_Type__c='Ask Us';
req.Origin='Web';
req.Faculty_Member__c='Stanford';
req.Deadline__c=date.today();
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
dmo.EmailHeader.triggerAutoResponseEmail = true;
req.setOptions(dmo);
insert req;

LibraryAskUsRequestController lib = new LibraryAskUsRequestController();
system.assertEquals(true, Recaptcha.getVerify());
system.assert(true, lib.attsize>10485760);
lib.Save();


blob filebody1=Blob.valueOf('Attachment body');
Attachment att1 = new Attachment();
att1.Name='Library Request';
att1.ContentType='content type';
att1.Body=filebody1;
att1.ParentId=req.id;
insert att1;
lib.insertatt();
}

 

Im able to cover only if blocks

Could you please help me in this issue.

 

Thanks 

 

 

 

deepu.sandeepdeepu.sandeep

Hi gautam,

 

what you said is correct,

 

but here attachment is namdatory, if boty is null then case has to be inserted and redirect to conformation page.

 

how should i get it

gautam_singhgautam_singh

Hi ,


I guess the requirement says that if the attachment size is 10 MB ++ then the Case should also not be inserted. Right ? If it is less then the WARNING should be displayed.


This code concept will work in this scenario



 public PageReference Save()
    {
        
        if(attbody!=null)
               {
                   if(attbody.size()>10485760 )
                       {
                               ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'This file exceeds the maximum size limit of 10MB.'));
                               return null;
                       }else{
                            request.Origin='Web';
                            Database.DMLOptions dmo = new Database.DMLOptions();
                            dmo.assignmentRuleHeader.useDefaultRule= true;
                            dmo.EmailHeader.triggerAutoResponseEmail = true;
                            request.setOptions(dmo);
                            insert request;
                            
                            att = new Attachment();
                            att.body = attbody;
                            att.Name = filename;
                            att.ContentType=contentType;
                            att.ParentId = request.Id;
                            insert att;
                            
                            PageReference confirmation = new PageReference('http://www.google.com');
                            return confirmation;
                            
                 }
                 }
       
          }



And in the test class for the same att.BodyLength=10485760; will ease the condition and it will definately make the condition to true.



Important :

Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

deepu.sandeepdeepu.sandeep

Hi gautam,

 

The requirement is 

 

If the attachment is 10 mb++ record shouldn't be inserted and awarning hasto be displayed on page

 

if it's less than 10 mb case record  will insert with attachment. an redirects 

 

here aattachment is not mandatory if file is not attached then only case record will have to insert and redirect 

 

Also can you edit my test class with correct code and post it please.

 

Thanks,

 

gautam_singhgautam_singh

Hi ,

 

You can use the following logic to successfully implement the requirement.

 

public pagereference Save(){

              if(attach.body == null || attach.size()< 10 MB) {
             insert request;
}else
{
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING, 'This file exceeds the maximum size limit of 10MB.'));
                               return null;
 
}catch (Exceptions e)
{

}

}



Also , In the test class you need to insert 2 cases one with an attachment and one with attachment of size more than 10 MB. and execute the Save Buttton. This solves your problem.

 

 

Important :

Hit Kudos{Stars Aside my Pic} if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You

 

 

deepu.sandeepdeepu.sandeep

Hi gautam,

 

Thanks for the logic it works fine , but i'm facing the problem with test class.

 

If u dont mind please can u write a test class for this class and post it.

 

Thanks in Advance,

Sandeep

deepu.sandeepdeepu.sandeep

hi gautam,

 

att.body.size() is not cluclating the size of attached file, so its not coming in to loop.

 

Is there any alternative to caluclate attached file size

 

 

gautam_singhgautam_singh
BodyLength works correct.