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

Calculating attachment size in tigger



i am trying to limit the attachment size to 2 MB through a seems like not calculating correct size. i want to limit the users so that they only able to upload files less than 2 MB in size. 


i am trying with following code


trigger trgCheckFileSize on Attachment (after insert) {

list<Attachment> att = new List<Attachment>();   

for (Attachment a : {         att.add(a);        }   


integer sizecheck = att.get(0).body.size();   

if (sizecheck  > 2097152){   

att.get(0).addError('max size 1 Mb: you are trying file size ' + String.Valueof(sizecheck));        }    



any idea why its not calculating the correct size.

ps: for file size 8kb, i am getting body size 8098.




ok guys, i have figured out what was wrong.


instead of using (after insert)  i used (before insert)   and it worked.


well done Me...