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
ssousanssousan 

Understanding Relationship Names-Unable to call Account name from Contact info

I am trying to call the Account name of a contact as follows:

 

public with sharing class mainSub {

public PageReference sendpassfail() {
Account_name = SELECT Account.Name, (SELECT Contact.FirstName FROM Contact Where Contact.LastName = 'Steve') FROM Account

}
}

 

But this gives me the following error:

 

unexpected token: 'SELECT' at line ----

 

Can someone tell me why?

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
pooja@magichorse.compooja@magichorse.com

Try this,

 

Try This...

Account Account_name = [SELECT Account.Name, (SELECT FirstName FROM Contacts Where Contact.LastName = 'Steve') FROM Account Limit 1]; //I used limit 1 because you have not mentioned any condition so we can assign only one record to Account object "Account_name"

 

List<Contact> lstContact = new List<Contact>();//We are getting List for contact

if(Account_name.contacts.size()>0){

    for(Contact con:Account_name.contacts){

        lstContact.add(con);

    }

}

 

 

All Answers

Team WorksTeam Works

Try This...

Account Account_name = [SELECT Account.Name, (SELECT FirstName FROM Contacts Where Contact.LastName = 'Steve') FROM Account];

Contact con = Account.Contacts;

system.debug(con.FirstName);

Hit Kudo's if this helps

pooja@magichorse.compooja@magichorse.com

Try this,

 

Try This...

Account Account_name = [SELECT Account.Name, (SELECT FirstName FROM Contacts Where Contact.LastName = 'Steve') FROM Account Limit 1]; //I used limit 1 because you have not mentioned any condition so we can assign only one record to Account object "Account_name"

 

List<Contact> lstContact = new List<Contact>();//We are getting List for contact

if(Account_name.contacts.size()>0){

    for(Contact con:Account_name.contacts){

        lstContact.add(con);

    }

}

 

 

This was selected as the best answer
ssousanssousan

Best

ssousanssousan

pooja,Yes this works, thanks
Let me add
system.debug(LoggingLevel.INFO,lstContact.get(0).FirstName);

ssousanssousan

Both answers are considered a solution,

 

Thank you guys

ssousanssousan

Could you guys possibly tell me how to add another name based on the same Account,

 

So in other words how to programtically link a Contact to a specific Account,

 

Thanks for the help

 

pooja@magichorse.compooja@magichorse.com
Contact object has lookup field for Account (AccountId) means Account is parent object and Contact is child object. Above SOQL query returns list of related contacts with each account.
ssousanssousan

Yes, this is how its done manually, correct,

but how to save a new contact and relate the contact to a specific account using APEX code?

 

Thanks

ssousanssousan

OK, i managed to get it working,

 

Thank you guys,

 

Best

ssousanssousan

Can one of you guys possibally tell me why this line of code gives me an error:

 

  Contact Contact_email = [Select Contact.Email,(SELECT Host_Name__c From Contracts Where Contract.Physical_Address__c=:record.Physical_Address__c) FROM Contact];

 

AS:

 

Didn't understand relationship 'Contracts' in FROM part of query call.
If you are attempting to use a custom relationship, be sure to append
the '__r' after the custom relationship name. Please reference your WSDL
or the describe call for the appropriate names. at line ...

 

 

Im just trying to retrieve the contact email, based on the contract IP Address,

 

Thanks

ssousanssousan

Ok i fixed this,

 

Thanks