3

I'm writing a test for a Contact trigger that references a class. I get the error: "Invalid id value for this SObject type". Not exactly sure how to reference this.

Thanks for any help.

Kevin

When a Contact record is Inserted, Updated or Deleted, a trigger fires and updates a field in the Account record. The full error:

Class: TestDardenCountAlumni
Method Name: updateAccount
Pass/Fail: Fail
Error Message :System.TypeException: Invalid id value for this SObject type: 000000000000001AAA
Stack Trace: Class.TestDardenCountAlumni.updateAccount: line 6, column 1

Here is my test:

@isTest
public class TestDardenCountAlumni {
    static testMethod void updateAccount(){

        Account accountToInsert = new Account();
        accountToInsert.Id = '000000000000001AAA';
        accountToInsert.Name = 'Test Account';
        accountToInsert.Number_of_Alumni__c = 0;
        insert accountToInsert;

        Contact contactToInsert = new Contact();
        contactToInsert.FirstName = 'Test Contact';
        contactToInsert.Darden_Degree__c = 'MBA';
        contactToInsert.Darden_Preferred_Year__c = '2014';
        contactToInsert.AccountId = '000000000000001AAA';
        insert contactToInsert;

    }
}
  • 1
    Kevin never hard code any Id's be it test/ main program. You may run the test successfully in your sandbox and when you move to another sandbox/ prod you may face issues since the hardcoded Id (the record you are referencing) may or may not exists. Always create a new record and assign the Id as SalesForceGirl pointed below :) – Rao Jan 30 '14 at 18:06
10

you cant set the id of the account(or any object on insert), it gets set on its own. (this is why you see the error) Once the account is inserted you can change the contact create to:

    Account accountToInsert = new Account();
    accountToInsert.Name = 'Test Account';
    accountToInsert.Number_of_Alumni__c = 0;
    insert accountToInsert;

    Contact contactToInsert = new Contact();
    contactToInsert.FirstName = 'Test Contact';
    contactToInsert.Darden_Degree__c = 'MBA';
    contactToInsert.Darden_Preferred_Year__c = '2014';
    contactToInsert.AccountId = accountToInsert.Id;
    insert contactToInsert;

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.