0

I wrote this trigger

trigger UpdateScholarShareDate on Contact (before update){  
    for( Contact con : Trigger.New){
        Contact oldCon = Trigger.oldMap.get(con.Id);
        if( oldCon.Scholar_Share_Email__c != con.Scholar_Share_Email__c ){
            con.Scholar_Share_Email_Date__c = System.today();             
        }
    }
}

here is the test code i have written but it will not let me insert any objects to test.

@isTest
private class  TestUpdateScholarShareDate {
    @isTest 
    static void testUpdateScholarShareDate(){
        //Account acc =  new Account(Name='acc1', RecordType= new RecordType(Name ='Household'));
        // insert acc;
        // Contact c = new Contact(LastName = 'testUpdateScholarShareDate1', Account = acc );
        Contact c = new Contact(LastName = 'testUpdateScholarShareDate1');
        insert c;
        Test.startTest();
        Contact con = [Select Id, FirstName, LastName, Scholar_Share_Email_Date__c,Scholar_Share_Email__c FROM Contact Where LastName = 'testUpdateScholarShareDate1'];
        con.Scholar_Share_Email__c = false;
        update con;
        Contact con1 = [Select Id, FirstName, LastName, Scholar_Share_Email_Date__c,Scholar_Share_Email__c FROM Contact Where LastName = 'testUpdateScholarShareDate1'];
        con1.Scholar_Share_Email__c = true;
        update con1;
        Contact con2 = [Select Id, FirstName, LastName,Scholar_Share_Email_Date__c,Scholar_Share_Email__c FROM Contact Where LastName = 'testUpdateScholarShareDate1'];
        Test.stopTest();
        System.assertEquals(con2.Scholar_Share_Email_Date__c , System.today());
        delete acc;
        delete c;
    }
}

I either get get this error

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: Class.npsp.ACCT_IndividualAccounts_TDTM.insertContactAccount: line 568, column 1 Class.npsp.ACCT_IndividualAccounts_TDTM.run: line 288, column 1 Class.npsp.TDTM_TriggerHandler.runClass: line 169, column 1 Class.npsp.TDTM_TriggerHandler.run: line 101, column 1 Trigger.npsp.TDTM_Contact: line 34, column 1: 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]

Class.npsp.TDTM_TriggerHandler.run: line 139, column 1 Trigger.npsp.TDTM_Contact: line 34, column 1: []

As you can see i added an account object previously and it didn't let me insert anything.

Any help would be greatly appreciated.

Thanks

Paul

1

Consider enhancing your test code by inserting a user record at test data setup and assigning it a user profile that is allowed to use the npsp record types, per your org config.

Then, enclose the testing runtime code inside a runAs context. See https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm for details on this.

2
  • Thanks for the reply! that sis not fix the issue though. – user24818 Aug 23 '16 at 23:08
  • Invalid cross reference error tells us that the user profile of running user does not have access to the correct record types. If you have implemented a system.runas user then worth checking that users profile permissions – CloudHugger Aug 24 '16 at 2:08

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.