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
Suresh RaghuramSuresh Raghuram 

writing a class to populate the fields in a custom object from the contact object

when a new record is created in the contacts object a trigger will fire to invoke the CreatePhantom class

 

trigger and class code is as follows.

but i am facing this error

Error: Compile Error: Initial term of field expression must be a concrete SObject: LIST<Contact> at line 10 column 27

 

trigger CreateAffiliation1 on Contact (after Insert, after Update) {

createPhantom pnt1 = new createPhantom();
for(Contact con : Trigger.new){
if(con.RecordTypeId == '012U00000009ALt'){
Pnt1.Phantom();
}
}
}

 

 

public class createphantom
{
contact cn = new contact();
List<contact> cnt =[select Name, ReportsToId, AccountId from Contact];

phantom__c pnt = new phantom__c();
public void phantom()
{
pnt.Name = 'patient';
pnt.External_Key__c = cnt.Id;
pnt.Last_Name__c = cnt.Name;
pnt.Reports_To__c = cnt.ReportsToId;
pnt.Account_Name__c = cnt.AccountId;

//Insert pnt;
Database.UpsertResult rslt = Database.Upsert(pnt,Phantom__c.External_key__c);
}


}

Best Answer chosen by Admin (Salesforce Developers) 
Abhinav GuptaAbhinav Gupta

You declared "cnt" as a list and then trying to use it as a single object, that is the problem. 

 

List<contact> cnt =[select Name, ReportsToId, AccountId from Contact];

 

// This shouldn't be cnt.Id, you should either iterate on all contacts in list "cnt" or use cnt[0].id. Same applies for other field references in "cnt"
pnt.External_Key__c = cnt.Id;
pnt.Last_Name__c = cnt.Name;
pnt.Reports_To__c = cnt.ReportsToId;
pnt.Account_Name__c = cnt.AccountId;