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
Prince VenkatPrince Venkat 

Field Refrence

how to reference contact field from Account object in apex class??


Any Help
Thanks in advance

Best Answer chosen by Prince Venkat
Suraj Tripathi 47Suraj Tripathi 47
Hi Prince,

Here is the solution:
Apex Class:
public class Contactreference {
    public static void Contactreference(){
        List<Account> accountlist = new List<account>();
        accountlist= [select id,Name, (select ID,LastName from contacts) from account];
        
        for (Account acc:accountlist){
            for(Contact con :acc.contacts){
                system.debug(con.LastName);
            }
        }
    }
}

Thanks

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi Prince,

Here is the solution:
Apex Class:
public class Contactreference {
    public static void Contactreference(){
        List<Account> accountlist = new List<account>();
        accountlist= [select id,Name, (select ID,LastName from contacts) from account];
        
        for (Account acc:accountlist){
            for(Contact con :acc.contacts){
                system.debug(con.LastName);
            }
        }
    }
}

Thanks
This was selected as the best answer
Prince VenkatPrince Venkat
Thanks Suraj