3

I have the following test class:

@isTest
private class PMTools_AppliedTechExtensionTest {

    @TestSetup
    static void SetupTestData(){

        Account testAccount = new Account(
                Name='Test Account 00',
                Status__c = 'Green',
                Billing_Terms_Days__c = 30
        );
        insert testAccount; 

        cpProject__c testProject = new cpProject__c(
            Name = 'Test Project 00',
            Client_Facing_Project_Name__c = 'Client Test Project',
            Sub_Type__c = 'Project',
            Account__c = testAccount.Id,
            Status__c = 'Pending',
            Type__c = 'Client',
            Start_Date__c = Date.today(),
            End_Date__c = Date.today().addDays(30)
        );
        insert testProject;
    }

    @isTest
    static void TestConstructor(){
        cpProject__c testProject = [SELECT Id FROM cpProject__c WHERE Name = 'Test Project 00' LIMIT 1];

        //Other Stuff
    }
}

When the test runs, I get the 'List has no rows for assignment to SObject' error on the project query. When I try to debug an unfiltered project query right after the insertion or in TestConstructor(), nothing at all prints in the debug log.

I reached out to a coworker and he asked what sharing model the cpProject__c object has. It turns out to be private, but I thought that wouldn't matter in Apex.

To cover our bases, he suggested creating and querying the record as a particular user, so I modified the code to the following:

@isTest
private class PMTools_AppliedTechExtensionTest {

    @TestSetup
    static void SetupTestData(){

        Account testAccount = new Account(
                Name='Test Account 00',
                Status__c = 'Green',
                Billing_Terms_Days__c = 30
        );
        insert testAccount; 

        //Create and insert a user

        System.runAs(u) {
            cpProject__c testProject = new cpProject__c(
                Name = 'Test Project 00',
                Client_Facing_Project_Name__c = 'Client Test Project',
                Sub_Type__c = 'Project',
                Account__c = testAccount.Id,
                Status__c = 'Pending',
                Type__c = 'Client',
                Start_Date__c = Date.today(),
                End_Date__c = Date.today().addDays(30)
            );
            insert testProject;
        }         
    }

    @isTest
    static void TestConstructor(){
        User u = [SELECT Id FROM User];
        System.runAs(u) {
            cpProject__c testProject = [SELECT Id FROM cpProject__c WHERE Name = 'Test Project 00' LIMIT 1];

            //Other Stuff
        }
    }
}

Now I'm getting the same error, but this time when querying for the the new user. I'm getting no other errors, so I ruled out that the insertions are failing in the first place.

  • 1
    Do you have anything modifying the name of the project on insert? Remove the where condition and debug the name from the result. If you do not get an error then the name was changed – Eric Apr 24 at 4:23
  • I tried System.debug([SELECT Id,Name FROM cpProject__c]) both right after the insert and in the test method and nothing at all prints in the debug log. – Gabriel Rivera Apr 24 at 13:03
  • Something has to print if you make it to that line. It may be [] or {} but something has to be there if your log levels are right and you make it to that line – Eric Apr 24 at 14:18
  • You're first comment ending up being correct. In TestConstructor() I added a query for each type of record created in the test setup. They all came back and project name was changed. I don't know why I didn't see any debug statements before. – Gabriel Rivera Apr 24 at 15:13
  • Check your workflow rules. I will add as an answer shortly – Eric Apr 25 at 13:20
0

Do you have anything modifying the name of the project on insert? Many times an org will have a workflow rule that standardizes the name. If this is the case your query will not find the record you created.

To validate:

  1. Remove the where condition and debug the name from the result.
  2. If you do not get an error then the name was changed
  3. You can debug the record to see what the name was changed to
1

Users are not subject to database isolation; your query is effectively querying every user in your org. You should be getting "more than one row for assignment" errors instead. Make sure you're querying for just a single row. In the case of the user, consider:

User u = [SELECT Id FROM User ORDER BY CreatedDate DESC LIMIT 1];

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.