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
Robert RobinsonRobert Robinson 

Error: Compile Error: Invalid field

I am creating a test class for an Apex class. My code:

@isTest
public class LsUnitListTest
{
    static testMethod void testLsUnitListPage()
    {
    
        Lease__c objLease = new Lease__c(Name = 'Test Lease');
        insert objLease;
       
        List<Lease_Unit_Association__c> lstUnit = new List<Lease_Unit_Association__c>{  new Unit__c(    Unit__c = 'Test Unit1', Unit__c = objLease.ID,  Property_Unit__r.Unit_Status_Code__c = 'N'),


I am encountering the following error:
Error: Compile Error: Invalid field Unit__c for SObject Unit__c at line 10 column 115

I am not quite sure what it is telling me. Do I have an invalid field type in the test? I get the feeling it is something obvious. Any help would be appreciated. Thanks.
Chris Gary CloudPerformerChris Gary CloudPerformer
In your line 'List<Lease_Unit_Association__c> lstUnit = new List<Lease_Unit_Association__c>{  new Unit__c(    Unit__c = 'Test Unit1', Unit__c = objLease.ID,  Property_Unit__r.Unit_Status_Code__c = 'N')' You are listing the API Field Name 'Unit__c' twice, and you are assigning two different values to it.  Is this Unit__c fiels on the Unit__c object a String or a Lookup?
Robert RobinsonRobert Robinson
Worse. It is a formula.
Robert RobinsonRobert Robinson
And it is not the Unit__c object; it is the Lease_Unit_Association__c object.
Chris Gary CloudPerformerChris Gary CloudPerformer
Then there is your problem.  You are trying to define a Unit__c sObject in a List of Lease_Unit_Association__c objects when you should be defining a new Lease_Unit_Association__c() object.  It should look like the following:
List<Lease_Unit_Association__c> lstUnit = new List<Lease_Unit_Association__c>{  new Lease_Unit_Association__c(    Unit__c = 'Test Unit1', Unit__c = objLease.ID,  Property_Unit__r.Unit_Status_Code__c = 'N')};

Also - yeah - you can't set a formula field.  You still have something funky going on with  this "Unit__c = 'Test Unit1', Unit__c = objLease.ID" piece. 
Robert RobinsonRobert Robinson
Thank you. Will give this a whirl. Robert L Robinson Manager, Salesforce Application [cid:image002.jpg@01D0FB5B.BD9097F0] Welltower Inc. 4500 Dorr Street Toledo, Ohio 43615-4040 419.247.2860 (o) 419.699.2539 (m) rrobinson@welltower.com Please note my new welltower.com email address.
Chris Gary CloudPerformerChris Gary CloudPerformer
No problem Robert! If my answer helps, please remember to mark as 'Best Answer' so that this discussion can be closed and marked as solved to help others! Keep the community alive!
Chris Gary CloudPerformerChris Gary CloudPerformer
Robert, did you ever resolve this issue?  If you did, please let me know how you resolved it.  We would like to have that answer recorded and the question marked as solved to help others in the future.  Thanks!