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
mat_tone_84mat_tone_84 

limit file size of attachment with a trigger

Hello!

I have to do a trigger which send an email when the attachment's size is > of X kb.

I see that I can't create trigger into sandbox, because there isn't the object "attachment".

first question: Do have I to create the trigger only by eclipse ???

second question: Using the body.size() Will I solve my problem ?

thanks

Best Answer chosen by Admin (Salesforce Developers) 
mat_tone_84mat_tone_84

I create this code, but doesn't work.

If I open an opportunity for upload attachment with size about 2 mb , I haven't any error!

why??

 

 

trigger size_allegati on Attachment (before insert) {
	list<Attachment> att = new List<Attachment>();
    for (Attachment a : Trigger.new) { 
        att.add(a);    
    }
    integer sizecheck = att.get(0).body.size();
    if (sizecheck  > 1000){
    	att.get(0).addError('max size 1 Mb');
    }
}

 

I think that is easy what I'm asking, but it isn't!! 

 

This works properly, before I had inactive trigger.

All Answers

SurekaSureka

Hi,

 

To answer your first question, Yes. You need to create the trigger for attachment in Eclipse.

 

For your second question, You have a parameter "filesize" in attachment.You can use filesize>10000 in the query, where 10000 is 10 MB.

 

Hope this helps.

 

Thanks

SurekaSureka

Hi,

 

In brief, you can make use of "filesize" tag of "inputFile" VF tag. ie.

 

<apex:inputFile value="{!document.body}" filename="{!document.name}" filesize="{!filesize}"/>

mat_tone_84mat_tone_84

I create this code, but doesn't work.

If I open an opportunity for upload attachment with size about 2 mb , I haven't any error!

why??

 

 

trigger size_allegati on Attachment (before insert) {
	list<Attachment> att = new List<Attachment>();
    for (Attachment a : Trigger.new) { 
        att.add(a);    
    }
    integer sizecheck = att.get(0).body.size();
    if (sizecheck  > 1000){
    	att.get(0).addError('max size 1 Mb');
    }
}

 

I think that is easy what I'm asking, but it isn't!! 

 

This works properly, before I had inactive trigger.

This was selected as the best answer
Amol Bhalerao 7Amol Bhalerao 7
If you are using content document objects nowadays, you need to write the trigger on ContentDocumentLink object instead of Attachment.
Here is the solution,
http://theapexhub.com/restrict-file-count-and-file-size-of-content-document-for-any-salesforce-object/