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
viv1viv1 

testmethod for attachment ??

i am writing test for controller

having attachment functionality !!

 


this is my code..

 

Blob b = Blob.valueOf('Test Data');  
      
           Attachment attachment = new Attachment();  
        attachment.ParentId =doc.Id;  
          attachment.Name = 'Test Attachment for Parent';  
          attachment.Body = b;  
      
           insert attachment;

 

but is giving

error : System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Body]: [Body]

bob_buzzardbob_buzzard

That code should work - I've just tried it on my dev org.

 

What type is 'doc' that is the parent of the attachment?

Srinivasa Chary TaduriSrinivasa Chary Taduri

Attachment vAttachment        = new Attachment();
  

   vAttachment.Name              = 'File';
   vAttachment.ParentId          = vLstOppty[i].Id;
   vAttachment.ContentType       = '.txt';
   vAttachment.Body              = Blob.valueOf('Test Data');
 

insert vAttachment;

 

I thinks you have missed ContentType field. Please mark it, if it helps you.