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
sfdc18sfdc18 

Unknown_Exception, BlobField : Body got null error

Hi,

I am getting below error when I try to attach files in attachment object.
But problem is this error is not occuring continuously.
Sometimes I successfully attach files in attachment object. Sometimes I get this error on insert statement :  insert l2AttList;

User-added image\
Apex Class :
public with sharing class CopyAttachmentsController {

    public Set<string> supportCaseIds = new Set<string>();
    ID l2CaseId; 
    public List<AttachmentWrapper> attList {get; set;}
    public Boolean allChecked { get; set; }
    
    public CopyAttachmentsController(ApexPages.StandardController controller)
    {
        supportCaseIds.add(ApexPages.CurrentPage().getParameters().get('parentId'));
        system.debug('-----------supportCaseIds------------'+supportCaseIds);

        l2CaseId = ApexPages.CurrentPage().getParameters().get('Id');
        system.debug('-----------l2CaseId------------'+l2CaseId);
        
        allChecked = false;
    }
    
    public PageReference CheckAll(){
        for(AttachmentWrapper at : attList){
            at.selected = allChecked;
        }
        return null;
    }

    public List<AttachmentWrapper> getAttachments() {
        if(attList == null) {
            attList = new List<AttachmentWrapper>();
            for(Attachment a: [Select Id, Name, Body, ContentType, ParentId from Attachment Where ParentId in : supportCaseIds]) {
                attList.add(new AttachmentWrapper(a));
            }
        }
        return attList;
    }
 
    public PageReference SelectAttachments() {

        List<Attachment> selectedAttachments = new List<Attachment>();

        for(AttachmentWrapper attach : getAttachments()) {
            if(attach.selected == true) {
                selectedAttachments.add(attach.att);
            }
        }
        List<Attachment> l2AttList = new List<Attachment>();
        for(Attachment selectedAtt : selectedAttachments)
        {
            Attachment attForL2Case = New Attachment();
            IF(selectedAtt != null && selectedAtt.body != null)
            {
            attForL2Case.Name = selectedAtt.Name;
            attForL2Case.Body = selectedAtt.Body;
            attForL2Case.contentType = selectedAtt.contentType;
            attForL2Case.ParentId = ApexPages.CurrentPage().getParameters().get('Id');
            }
            l2AttList.add(attForL2Case);
        }
        /*
        if (Limits.getHeapSize() > Limits.getLimitHeapSize()) { 
            ApexPages.Message errMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Attachments limit exceeded : Please deselect few attachments and try again');
            ApexPages.addMessage(errMsg); 
            return null;
        }
        */
        //try 
        //{
        If(l2AttList.size() > 0)
        {
            insert l2AttList;
        }
        //}
        //catch(Exception e)
        //{
            //system.debug('--------exception--------'+e);
        //}

        attList=null;

        PageReference pageReference = new PageReference('/'+l2CaseId);
        pageReference.setRedirect(true);
        return pageReference;
    }

    public class AttachmentWrapper {
        public Attachment att {get; set;}
        public Boolean selected {get; set;}

        public AttachmentWrapper(Attachment a) {
            att = a;
            selected = false;
        }
    }
}
Thanks.
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
public with sharing class CopyAttachmentsController {

    public Set<string> supportCaseIds = new Set<string>();
    ID l2CaseId; 
    public List<AttachmentWrapper> attList {get; set;}
    public Boolean allChecked { get; set; }
    
    public CopyAttachmentsController(ApexPages.StandardController controller)
    {
        supportCaseIds.add(ApexPages.CurrentPage().getParameters().get('parentId'));
        system.debug('-----------supportCaseIds------------'+supportCaseIds);

        l2CaseId = ApexPages.CurrentPage().getParameters().get('Id');
        system.debug('-----------l2CaseId------------'+l2CaseId);
        
        allChecked = false;
    }
    
    public PageReference CheckAll(){
        for(AttachmentWrapper at : attList){
            at.selected = allChecked;
        }
        return null;
    }

    public List<AttachmentWrapper> getAttachments() {
        if(attList == null) {
            attList = new List<AttachmentWrapper>();
            for(Attachment a: [Select Id, Name, Body, ContentType, ParentId from Attachment Where ParentId in : supportCaseIds]) {
                attList.add(new AttachmentWrapper(a));
            }
        }
        return attList;
    }
 
    public PageReference SelectAttachments() {

        List<Attachment> selectedAttachments = new List<Attachment>();

        for(AttachmentWrapper attach : getAttachments()) {
            if(attach.selected == true) {
                selectedAttachments.add(attach.att);
            }
        }
        List<Attachment> l2AttList = new List<Attachment>();
        for(Attachment selectedAtt : selectedAttachments)
        {
            Attachment attForL2Case = New Attachment();
            IF(selectedAtt != null && selectedAtt.body != null)
            {
				attForL2Case.Name = selectedAtt.Name;
				attForL2Case.Body = selectedAtt.Body;
				attForL2Case.contentType = selectedAtt.contentType;
				attForL2Case.ParentId = ApexPages.CurrentPage().getParameters().get('Id');
	            l2AttList.add(attForL2Case);

            }
        }
        /*
        if (Limits.getHeapSize() > Limits.getLimitHeapSize()) { 
            ApexPages.Message errMsg = new  ApexPages.Message(ApexPages.Severity.ERROR,'Attachments limit exceeded : Please deselect few attachments and try again');
            ApexPages.addMessage(errMsg); 
            return null;
        }
        */
        //try 
        //{
        If(l2AttList.size() > 0)
        {
            insert l2AttList;
        }
        //}
        //catch(Exception e)
        //{
            //system.debug('--------exception--------'+e);
        //}

        attList=null;

        PageReference pageReference = new PageReference('/'+l2CaseId);
        pageReference.setRedirect(true);
        return pageReference;
    }

    public class AttachmentWrapper {
        public Attachment att {get; set;}
        public Boolean selected {get; set;}

        public AttachmentWrapper(Attachment a) {
            att = a;
            selected = false;
        }
    }
}
Please let us know if this will help you

Thanks,
Amit Chaudhary