You need to sign in to do that
Don't have an account?

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,
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
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
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);
}
}
Namo, ok you mean
Contact con = Account_name.Contacts;
system.debug(LoggingLevel.INFO,con.FirstName);
Yes this works thanks
Best
pooja,Yes this works, thanks
Let me add
system.debug(LoggingLevel.INFO,lstContact.get(0).FirstName);
Both answers are considered a solution,
Thank you guys
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
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
OK, i managed to get it working,
Thank you guys,
Best
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
Ok i fixed this,
Thanks