1

I'm at a loss. I don't understand why my test class works fine in the sandbox (100% coverage), yet when I run a deployment validation in production, I get the following error for each test method:

System.QueryException: List has no rows for assignment to SObject Stack Trace: Class.contentDocumentLinkTriggerTest.internalResolutionNote: line 53, column 1

(This is the first SOQL query in each test method attempting to return the relevant ContentNote inserted in Test Setup.)

@IsTest
public class contentDocumentLinkTriggerTest {

@TestSetup static void setupData() {

    Case internalCase = new Case(Subject = 'TestContentDocumentTriggerInternalCase',
                                 Description = 'test description',
                                 RecordTypeId = [Select Id
                                                 From RecordType
                                                 Where DeveloperName = 'Internal_Case'
                                                 Limit 1].Id,
                                 Type = 'Bug',
                                 Status = 'Backlog',
                                 Origin = 'Admin',
                                 Priority = 'P3 - Minor',
                                 HasResolutionNotes__c = False);

    Case customerCase = new Case(Subject = 'TestContentDocumentTriggerCustomerCase',
                                 Description = 'test description',
                                 RecordTypeId = [Select Id
                                                 From RecordType
                                                 Where DeveloperName = 'Customer'
                                                 Limit 1].Id,
                                 Type = 'Bug',
                                 Status = 'Open',
                                 Origin = 'Email',
                                 Priority = 'Medium',
                                 HasResolutionNotes__c = False);

    ContentNote cnResolution = new ContentNote(Title = 'Resolution Notes TriggerTest',
                                               OwnerId = [Select Id
                                                          From User
                                                          Where IsActive = True
                                                          And CompanyName Like '%[our company name]%'
                                                          Limit 1].Id);

    ContentNote cnNoResolution = new ContentNote(Title = 'Some Different Notes TriggerTest',
                                                 OwnerId = [Select Id
                                                            From User
                                                            Where IsActive = True
                                                            And CompanyName Like '%[our company name]%'
                                                            Limit 1].Id);

    insert internalCase;
    insert customerCase;
    insert cnResolution;
    insert cnNoResolution;

}

@IsTest
static void internalResolutionNote() {

    ContentNote cnResolution = [Select Id
                                From ContentNote
                                Where Title = 'Resolution Notes TriggerTest'
                                Limit 1];

    Case internalCase = [Select Id
                         From Case
                         Where Subject = 'TestContentDocumentTriggerInternalCase'
                         Limit 1];

    ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cnResolution.Id,
                                                      LinkedEntityId = internalCase.Id,
                                                      ShareType = 'I');

    Test.startTest();

        insert cdl;

    Test.stopTest();

    Case checkCase = [Select Id, HasResolutionNotes__c
                      From Case
                      Where Subject = 'TestContentDocumentTriggerInternalCase'];

    System.assert(checkCase.HasResolutionNotes__c);

}

@IsTest
static void customerResolutionNote() {

    ContentNote cnResolution = [Select Id
                                From ContentNote
                                Where Title = 'Resolution Notes TriggerTest'
                                Limit 1];

    Case customerCase = [Select Id
                         From Case
                         Where Subject = 'TestContentDocumentTriggerCustomerCase'
                         Limit 1];

    ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cnResolution.Id,
                                                      LinkedEntityId = customerCase.Id,
                                                      ShareType = 'I');

    Test.startTest();

        insert cdl;

    Test.stopTest();

    Case checkCase = [Select Id, HasResolutionNotes__c
                      From Case
                      Where Subject = 'TestContentDocumentTriggerCustomerCase'];

    System.assert(!checkCase.HasResolutionNotes__c);

}

@IsTest
static void internalNoResolutionNote() {

    ContentNote cnNoResolution = [Select Id
                                  From ContentNote
                                  Where Title = 'Some Different Notes TriggerTest'
                                  Limit 1];

    Case internalCase = [Select Id
                         From Case
                         Where Subject = 'TestContentDocumentTriggerInternalCase'
                         Limit 1];

    ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cnNoResolution.Id,
                                                      LinkedEntityId = internalCase.Id,
                                                      ShareType = 'I');

    Test.startTest();

        insert cdl;

    Test.stopTest();

    Case checkCase = [Select Id, HasResolutionNotes__c
                      From Case
                      Where Subject = 'TestContentDocumentTriggerInternalCase'];

    System.assert(!checkCase.HasResolutionNotes__c);

}

@IsTest
static void customerNoResolutionNote() {

    ContentNote cnNoResolution = [Select Id
                                  From ContentNote
                                  Where Title = 'Some Different Notes TriggerTest'
                                  Limit 1];

    Case customerCase = [Select Id
                         From Case
                         Where Subject = 'TestContentDocumentTriggerCustomerCase'
                         Limit 1];

    ContentDocumentLink cdl = new ContentDocumentLink(ContentDocumentId = cnNoResolution.Id,
                                                      LinkedEntityId = customerCase.Id,
                                                      ShareType = 'I');

    Test.startTest();

        insert cdl;

    Test.stopTest();

    Case checkCase = [Select Id, HasResolutionNotes__c
                      From Case
                      Where Subject = 'TestContentDocumentTriggerCustomerCase'];

    System.assert(!checkCase.HasResolutionNotes__c);

}
}
  • That error always suggests sharing rules to me. No personal experience with ContentNote but as you are doing something explicit with the owner FYI success.salesforce.com/ideaView?id=0873A000000E2a6QAC. – Keith C Jun 27 '17 at 20:27
  • Thanks for sharing that link - I had no idea that was the case! (I'm addressing the substance of your comment as a reply below since it's very similar) – Mike Jun 27 '17 at 20:45
2

When you are setting up your ContentNote test data, you are selecting from User to set OwnerId. Make sure to insert test users before selecting from them.

  • 1
    That was the underlying issue. I didn't feel like inserting a user and for my purposes, it shouldn't matter who the Note owner is in theory in order to trigger the class... But I didn't realize the sharing implications. After reading through this, I realized there's no need to explicitly assign an owner, so I just removed that from the note setup and it all worked fine. Thanks! – Mike Jun 27 '17 at 20:48

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.