+ Start a Discussion
anjaanja 

need help with trigger test- error: Invalid id vale for this SObject type

Hello, I am new to writing tests - working off of the template in how to write good unit tests. This is my effort but I get the error Invalid id value for this SObject type:

 

public class ActiveAffiliationsTriggerTest

static testMethod void ActiveAffiliationsTest() 

{     
    // Set up the Contact record.   

Contact c = new Contact(FirstName='Test Contact', LastName ='Test Last');   

insert c;


   // Verify that the initial state is as expected.   

c = [SELECT DVA_Affiliation_MDA_and_Active__c          

FROM Contact          

WHERE Id = :c.Id];   

System.assertEquals(null, c.DVA_Affiliation_MDA_and_Active__c);


   // Set up the Affiliation record.   

String affiliationName = 'Test Affiliation';   

Affiliation__c a = new Affiliation__c(Id=c.Id,                                     

Active__c=TRUE);


   // Insert Affiliation.   

insert a;    


   // Verify that the results are as expected.   

c = [SELECT FirstName, LastName, DVA_Affiliation_MDA_and_Active__c          

FROM Contact        

 WHERE Id = :c.Id];     

}

}

 

contact is contact

Affliliation__c is a related object where Contact is the master - contact can have more than one Affiliation record

DVA_Affiliation_MDA_and_Active__c is a number field the trigger fills in calulating on the Contact record how many affiliations the contact has

 

mngmng

Affiliation__c a = new Affiliation__c(Id=c.Id, Active__c=TRUE);



You're passing the id of a contact as the id of another object. Perhaps you meant to set the contact lookup field instead, whatever that is?

rcravenrcraven

agree with nmg.  in addition, you keep updating your var "c" for Contact when you requery.   But when you requery your not including the Id.   I'm not 100% sure the second reference "Id = :c.Id" would work?

 

   // Insert Affiliation.   

insert a;    


   // Verify that the results are as expected.   

c = [SELECT FirstName, LastName, DVA_Affiliation_MDA_and_Active__c          

FROM Contact        

 WHERE Id = :c.Id]; 

Ankit AroraAnkit Arora

Still not clear with what you want, but the problem is here :

 

Affiliation__c a = new Affiliation__c(Id=c.Id,                                     
Active__c=TRUE);

 Here you are entering the contactId in Affiliation Object. I think you have a relationship lookup of contact on Affiliation then you can do something like this :

 

Affiliation__c a = new Affiliation__c(Contact__c=c.Id,                                     
Active__c=TRUE);

I am assuming  "Contact__c" is the lookup API name. You can replace it with the actual one. Let me know if this is what you actually want.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page