Test Attachments records in Apex Test Class

@isTest(SeeAllData=true)
private class TestAttachment{
    static testmethod void testattach(){
   
        Account a = new Account(Name='newAcct');
        Test.startTest();
        insert a;
        Test.stopTest();
        System.debug('Account created: ' + a.Id);
       
        Attachment attach=new Attachment();
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=a.Id;
        attach.ContentType = 'application/msword';
        attach.IsPrivate = false;
        attach.Description = 'Test';
        insert attach;
       
        System.debug('Inserted: '+ attach.Id);
       
        List<Account> acct = [SELECT id, Name,
        (SELECT Id FROM CombinedAttachments)
        FROM Account where id = :a.Id];
       
        System.debug('Account Query : '+acct);
       
        for(Account a1 : acct){
            System.debug('Id: '+a1.Id + ' Name: '+a1.Name + ' CombAttach: '+ a1.CombinedAttachments);   
            System.assert(a1.CombinedAttachments.size()>0);       
        }       
    }
}

cheers! 

3 comments:

  1. Very helpful blog for us. Thanks for sharing. Please checkSalesforce Training
    For Administrators & Developers Salesforce Developer Certification

    ReplyDelete
  2. JavaScript training helps learners build the skills needed to create interactive and dynamic websites. A well-structured training program covers the fundamentals of JavaScript, including variables, data types, operators, loops, functions, arrays, objects, and events. As learners progress, they gain hands-on experience with DOM manipulation, form validation, asynchronous programming, and modern ES6+ features, enabling them to develop responsive and user-friendly web applications.

    ReplyDelete