+ Start a Discussion
Vepsun VarunVepsun Varun 

in soql retrive data id,name from account and frist name,last name from contact where account name starts with hp

salesforcelearner1salesforcelearner1
Hi Varun,

Please use the below query

Select Id,Name, (Select FirstName,LastName from Contacts) from Account WHERE Name LIKE 'hp%'

Please mark it as best answer... 

Thanks,
SalesforceLearner
Deepak GerianiDeepak Geriani

Hello  Please find the Below Query 

As you requested for query which will return the list of account so you should store it in the list of account varibale.
List<Account> accountList = [select Id,Name, (select FirstName,LastName from Contacts) from Account WHERE Name LIKE 'hp%'];

if it helps you 

Please mark it as best answer... 

Thanks

Ajay K DubediAjay K Dubedi
Hi Varun,

* Well you need to use nested query if you want to get data of two sObject.

* The SOQL query to retrieve data will be like this:
  
List<Account> Account_List = [select Id,Name,(select FirstName,LastName from Contacts) from Account WHERE Name LIKE 'hp%'];

* Now contact is also connected to account object and you can retrieve data of contact like:
 
for(Account ac: Account_List)
     {
        String str = ac.contacts.FirstName;
        String str1 = ac.contacts.LastName
     }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
Deepali KulshresthaDeepali Kulshrestha
Hi Vepsun,

Use the following SOQL query.

List<Account> account_List = [SELECT Id, Name,(SELECT FirstName,LastName FROM Contacts) FROM Account WHERE Name LIKE 'hp%'];

'%' sign means that any word can come after 'hp'.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com