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
Iceman123Iceman123 

Case trigger not finding contactId created as part of the testmethod (during deployment)?

I think I am overlooking something simple here, any help is appreciated.  Here is an isolated example (as you can see, the trigger doesn't actually do anything, it's trimmed down to just show the problem).   The testmethod runs fine with 100% coverage when testing against the sandbox environment.  

 

During deployment validation (when attempting to deploy to production), when the testmethod gets to the insert statement for the case, it goes into the trigger as expected (after insert), but it gives a "list has no rows" error in the trigger (when it attempts to query for the contact associated to the case).  But the contact is created at the beginning of the testMethod and associated to the case before the insert statement so I would expect that the validation process would find this contact because it is created and associated to the case as part of the testmethod.  Can anyone tell me why it cannot locate the contact during the deployment process?

 

thanks!

Pi

 

---------------

trigger testTrigger on Case (after insert) {

    for (Case c:Trigger.new)
    {
        Contact contact1 = [select AccountId from Contact where Id=:c.ContactId]; //caused by: System.QueryException: List has no rows for assignment to SObject
    }

}

 

 

    static testMethod void myUnitTest() {

        Test.startTest();
        Account a = new Account();
        a.Name='Test Account';
        a.Industry = 'Automotive';
        insert a;
        
        Contact contact1 = new Contact();
        contact1.FirstName = 'First';
        contact1.LastName = 'Last';
        contact1.AccountId = a.Id;
        insert contact1;        

         Case case1 = new Case();
         case1.ContactId=contact1.Id;
         case1.Subject='test Subject';
        insert case1;         
        
        system.assertEquals(a.Id,[select AccountId from Contact where Id=:case1.ContactId].AccountId);
        Test.stopTest();
    }

Best Answer chosen by Admin (Salesforce Developers) 
Iceman123Iceman123

nvm just realized that it is error-ing out on a different testmethod from a different class, this use case works fine :)

All Answers

kritinkritin

change your lines from

Contact contact1 = [select AccountId from Contact where Id=:c.ContactId];

 

to

 

Contact[] contact1 = [select AccountId from Contact where Id=:c.ContactId];

system.Debug(contact1.size());

Iceman123Iceman123

thank you. Using an array of contacts didn't fail with an error, however, the array is empty which suggests that the trigger is not aware of the Contact object that is created as part of the testMethod.  

 

TRIGGER:

 

trigger TestT on Case (after insert) {
    for (Case c:Trigger.new)
    {
        Contact[] contact = [select AccountId from Contact where Id=:c.ContactId];

        system.debug('testing to get the first object in the array:' + contact[0];
    }
}

 

 

TEST:

 

public with sharing class TestTrig {

    static testMethod void myUnitTest() {

        Test.startTest();
        Account a = new Account();
        a.Name='Test Account';
        a.Industry = 'Automotive';
        insert a;
        
        Contact contact1 = new Contact();
        contact1.FirstName = 'First';
        contact1.LastName = 'Last';
        contact1.AccountId = a.Id;
        insert contact1;        

          Case case1 = new Case();
          case1.ContactId=contact1.Id;
          case1.Subject='test Subject';
        insert case1;         
        
        system.assertEquals(a.Id,[select AccountId from Contact where Id=:case1.ContactId].AccountId);
        Test.stopTest();
    }

}

 

Anyone know why the production deployment is failing while the Sandbox test succeeds (given that account, contact and case of interest are all created as part of the test case)?

 

thanks

Iceman123Iceman123

nvm just realized that it is error-ing out on a different testmethod from a different class, this use case works fine :)

This was selected as the best answer