0

I have written a Batch class that will check for Account records with no child records and delete them. Among the sObjects I need to check are Notes and Attachments. Because I have more than 20 child objects to check I thought I would query NotesAndAttachments together rather than separate. I'm writing my test class now but when I get to testing Notes and Attachments my tests are failing.

My query for Batch class is below but with custom objects removed.

String query ='SELECT PersonContactId'+
                ', (SELECT Id FROM PartnersFrom LIMIT 1) '+
                ', (SELECT Id FROM PartnersTo LIMIT 1) '+
                ', (SELECT Id FROM Assets LIMIT 1) '+
                ', (SELECT Id FROM Cases LIMIT 1) '+
                ', (SELECT Id FROM Events WHERE ActivityDate >= LAST_N_DAYS:1096 LIMIT 1) '+
                ', (SELECT Id FROM NotesAndAttachments LIMIT 1) '+
                ', (SELECT Id FROM Opportunities LIMIT 1) '+
                ', (SELECT Id FROM Tasks WHERE ActivityDate >= LAST_N_DAYS:1096 LIMIT 1) '+
                'FROM Account '+
                'WHERE ...';

The code where I check for child objects is below again with custom objects removed.

Set<Id> candidatesForDeletion = new Set<Id>();
List<Account> toDelete = new List<Account>();
for(Account a : scope)
{
    if( hasNoData(a) )
    {
        candidatesForDeletion.add( a.PersonContactId );
    }
}
...
private Boolean hasNoData(Account a)
{
    return a.PartnersFrom.isEmpty()
        && a.PartnersTo.isEmpty()
        && a.Assets.isEmpty()
        && a.Cases.isEmpty()
        && a.Events.isEmpty()
        && a.NotesAndAttachments.isEmpty()
        && a.Opportunities.isEmpty()
        && a.Tasks.isEmpty()
}

My test method looks like this

@isTest
static void testNoDeleteAccountHasAttachment()
{
    // Implement test code
    Id customRTId= DatabaseUtils.getRecordTypeId('Account', Constants.CUSTOM_RECORD_TYPE);

    Account testAccount = TestingUtils.createBasicAccount();
        testAccount.RecordTypeId = customRTId;
    insert testAccount;

    Attachment attach = new Attachment();
        attach.ParentId = testAccount.Id;
        attach.Name = 'test';
        attach.Body = Blob.valueOf('test blob');
    insert attach;

    Test.startTest();

        Database.executeBatch( new BatchDeleteOldContacts() );

    Test.stopTest();

    testAccount = [SELECT Id, IsDeleted FROM Account WHERE Id = :testAccount.Id LIMIT 1 ALL ROWS];

    System.assertEquals(false, testAccount.IsDeleted,
        'Account was deleted but had a Attachment');
}

@isTest
static void testNoDeleteAccountHasNote()
{
    // Implement test code
    Id customRTId= DatabaseUtils.getRecordTypeId('Account', Constants.CUSTOM_RECORD_TYPE);

    Account testAccount = TestingUtils.createBasicPersonAccount();
        testAccount.RecordTypeId = customRTId;
    insert testAccount;

    Note testNote = new Note();
        testNote.ParentId = testAccount.Id;
        testNote.Title = 'test title';
    insert testNote;

    Test.startTest();

        Database.executeBatch( new BatchDeleteOldContacts() );

    Test.stopTest();

    testAccount = [SELECT Id, IsDeleted FROM Account WHERE Id = :testAccount.Id LIMIT 1 ALL ROWS];

    System.assertEquals(false, testAccount.IsDeleted,
        'Account was deleted but had a Note');
}

In a Sandbox I created and Account, and added one Note and one Attachment to it. Using the Dev Console I queried

SELECT Id, (SELECT Id, ParentId from NotesAndAttachments) FROM Account WHERE Id = '001XXXXXXXXXXX'

and it returned the Account with both the Note and the Attachment.

Am I missing something that is preventing my Note and Attachment from being returned in my test, or am I going to have to bite the bullet and query them seperately?

  • 1
    Do you get the same results if you use the object "CombinedAttachment" ? – PepeFloyd Nov 12 '15 at 23:17
  • is your testmethod class at same version as DC (i.e. V36) – cropredy Nov 13 '15 at 1:26
  • @PepeFloyd If I use "CombinedAttachment" I get both the Note and the Attachment, the only problem is that CombinedAttachment "contains all notes, attachments, Google Docs, documents uploaded to libraries in Salesforce CRM Content, and files added to Chatter that are associated with a record." I'm not worried about Google Docs, Uploads, Chatter files etc.. so it doesn't really help me. – dBeltowski Nov 13 '15 at 14:27
  • I had a similar issue, i needed the body, when I take it out body from the query it loads all the records, but if i leave it only loads 1, this is when i use offset – manza Apr 24 '18 at 1:33
0

I ended up writing a separate query for Notes and one for Attachments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.