I have one contact in one of my account. I have the account id of that account. I am trying to get the email of the contact in my account with a soql query but it is returned blank :

contact em = [select name,email from contact where Accountid =:add];

To retrieve the email I then do em.mail. But that is empty.

add is my account id that I know

enter image description here

  • There are many problems with this. If more than record is retrieved it will fail. You are also saying em.mail when the results would be em.email. It is very possible that there could be multiple contacts with emails for one account – Ohmicron Jul 30 '19 at 14:34
  • So what would be the best way to contact the specific email from the contact I have for that account ? – Tiburce Jul 30 '19 at 14:36
  • Is Person Account enabled in your org ? – Vijay Ganji Jul 30 '19 at 14:42

email may work in apex but in Lightning it will fail as its wrong API name. Correct API is Email.

Change query to below (does not hurt to leave as is but you will get confused when implementing in lightning)

contact em = [SELECT Id, Name, Email FROM Contact where AccountId =:add];

Now wherever you are referring to email in lightning component, change it to Email (case sensitive)

|improve this answer|||||

You are not being specific enough so you have two options. If you are looking for a specific record then

 contact em = [select name, email from contact where accountdId=:add and name ='sdasadsda' limit 1];

would have the information

list<contact> emlist = [select name ,email from contact where accountId=:add];
for(contact c : emlist) {
  system.debug('Name: ' + c.name + ' : Email: ' + c.email);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.