+ Start a Discussion

test class creation - Custom object name and related object with same field name issues

Have a custom object called "Merchant__c". Have added a lookup field to the Merchant__c object within a Case and originally named the field Merchant (which produced an api name of Merchant__c for the reference field within the Case object).

Creating a test method for APEX code that will insert a Case and a fragment of the code for that is:

// Retrieve key from inserted test merchant record

List<Merchant__c> mrc = new List<Merchant__c>();

mrc = [select Id from Merchant__c where Name = 'testmerchant##'];

// Insert case records for test

List<Case> cse = new List<Case>();

cse.add(new Case(Origin = 'Outbound Phone Call',

Status = 'New',

merchant__c = mrc.Id,

Case_Subject__c = 'Installation Reschedule#3'));

The line - "merchant_name__c = mrc.Id" displays an error in Eclipse or when manually inserted into the APEX IDE

Compilation error: Initial term of field expression must be a concrete SObject: LIST:SOBJECT:Merchant__c

Thinking that perhaps the same field name as an object might be a point of confusion, I changed the field name in the Case object to "merchant name" and ensured the API name was reflected as "merchant_name__c". I then refreshed the schema in Eclipse and the code now appears as before but with the line "merchant_name__c = mrc.Id" - yet the error persists.

Any insights as to what might be incorrect here? I have other test methods written that load-up the ID of another object into a reference field without encountering this,

the mrc variable is a list of merchents, not a single one, you probably want mrc[0].id
Thanks Simon.  Before your reply I caught the errors of my dereferencing ways.