2

I keep getting this error when running a Test Class and I just don't know where to go from here. I've tried disabling Parrallel Apex Testing, running the code as a standard user, etc. I must be missing something and I hope that someone can look it over and see where I went wrong. I would really appreciate any help.

For background information, I work at a non-profit so we are using the latest NPSP.

This is the error message:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, npsp.TDTM_Contact: execution of BeforeInsert

caused by: System.AssertException: Assertion Failed: Expected: null, Actual: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Record Type ID: this ID value isn't valid for the user: 012j0000000KvvPAAS: [RecordTypeId]

(npsp)
: []

And here's the Test Class

@isTest
public class Test_FillOtherAddress {
    static testMethod void insertContact() {


    //Add one contact to create household
    Contact con = new Contact(FirstName='TEST_FIRSTNAME', LastName='TEST_LASTNAME');
   test.startTest();
    insert con;

    string addressType = 'Work';
    string street = '123 Wooddale Ave';
    string state = 'DE';
    string postalCode = '1234567';
    string country = 'USA';
    //string householdRecordType='012j0000000AI7Q';

    npsp__Address__c add = new npsp__Address__c(
        //RecordTypeId = householdRecordType,
        npsp__Household_Account__c=con.AccountId,
        npsp__Address_Type__c = addressType,            
        npsp__MailingStreet__c = street,
        npsp__MailingState__c =state,
        npsp__MailingPostalCode__c = postalCode,
        npsp__MailingCountry__c = country
    );

    insert add;

    //Add another contact to test insert event trigger
    Contact con2 = new Contact(FirstName='TEST_FIRSTNAME2', LastName='TEST_LASTNAME',     AccountId=con.AccountId, Other_Address_Lookup__c=add.Id);

    insert con2;   
    test.stopTest();
    Contact insertedContact = [SELECT OtherStreet FROM Contact WHERE Id = :con2.Id];
    System.assertEquals(street, insertedContact.OtherStreet);
}
}

My Trigger works fine, as far as I know:

trigger FillOtherAddress on Contact (before insert, before update) {  

    for(Contact c : Trigger.new){
        if(c.Other_Address_Lookup__c !=null){
            npsp__Address__c otherAddress = 
                [select npsp__Address_Type__c, npsp__Formula_MailingStreetAddress__c, npsp__MailingCity__c,  npsp__MailingState__c, npsp__MailingPostalCode__c, npsp__MailingCountry__c 
                 from npsp__Address__c where ID=:c.Other_Address_Lookup__c LIMIT 1];

            c.npe01__Secondary_Address_Type__c = otherAddress.npsp__Address_Type__c;
            c.OtherStreet = otherAddress.npsp__Formula_MailingStreetAddress__c;
            c.OtherCity = otherAddress.npsp__MailingCity__c;
            c.OtherState = otherAddress.npsp__MailingState__c;
            c.OtherPostalCode = otherAddress.npsp__MailingPostalCode__c;
            c.OtherCountry = otherAddress.npsp__MailingCountry__c;


        }

    }

}
1
  • 1
    BTW, I just noticed your trigger has a query inside a for loop. I would recommend to move it ouside the loop as this will cause issues with mass updates/inserts
    – PepeFloyd
    Jan 3, 2015 at 0:01

3 Answers 3

1

I found on the PowerofUS hub an answer for this. Settings > Account > Record Types. Individual Type was disabled. When I enabled this, the error no longer happens.

0

You created a record type but you haven't assigned to the profile that will insert the record.

Are you the user : 012j0000000KvvPAAS ? If you set this id in the URL will it go to your record ? If yes are you a system administrator? Check if the required record types are viewable by your profile

5
  • No I'm not user 012j0000000KvvPAAS and no one else is. I created a sandbox. When I set the id in the url it displays: "Insufficient Privileges You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary." I am a System Administrator and the Record Types are viewable by my profile.
    – Justin
    Jan 2, 2015 at 15:39
  • Is the id from the variable householdRecordType from the live org or from the dev sandbox? Record type ids are different when you do a refresh. The error has do with something wrong with the id as it says Record Type ID: this ID value isn't valid for the user: 012j0000000KvvPAAS: [RecordTypeId] Jan 2, 2015 at 15:48
  • It's from the dev sandbox. That's good to know tho for future reference!
    – Justin
    Jan 2, 2015 at 15:51
  • Also why don't you have the methods test.StartTest and test.StopTest in your test class? Have a look here developer.salesforce.com/page/… Jan 2, 2015 at 15:57
  • I had thought that it was only necessary if I was worried about testing large datasets. I have updated my code though.
    – Justin
    Jan 2, 2015 at 16:08
0

UPDATE:

This line: npsp__Household_Account__c=con.AccountId,

I don't see where are you setting up an account for that contact. It seems like you forgot to create a test account, is that field required? If not, try commenting that out.

If you want additional fields you need a query for example

ID accid=[Select AccountID from Contact WHERE Id=:con.id].AccountId;

Then you could use it

npsp__Household_Account__c=accid;

Also, you should not be hard-coding the record type ID in the code, that is considered a bad practice as it leads to this type of issues. You should be retrieving the value in the code and store it in a static variable.

5
  • Thanks, I just put in it like that to see if my test case was working correctly. I don't actually need a record type id. When I take it out I get the same issue.
    – Justin
    Jan 2, 2015 at 16:08
  • I have updated my answer
    – PepeFloyd
    Jan 2, 2015 at 16:12
  • In NPSP, a household account is automatically created when a contact is created. The same should be in a test case, right?
    – Justin
    Jan 2, 2015 at 16:15
  • Yes but you still need to query for the account id. You can only retrieve the account id like that if you were setting up manually. I have updated the answer.
    – PepeFloyd
    Jan 2, 2015 at 16:20
  • Still throwing the same error. I'm going to try creating an account first.
    – Justin
    Jan 2, 2015 at 20:03

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.