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
QuickDEVQuickDEV 

accessing contact field value from a custom object

Hi all,

 

I am Facing issues  accessing contact.Email field value from a custom object.

 

Details:

 

 Custom object (obj__C) has a custom lookup field to "Account".

 

I thought, because Account and contact have a relationship between them, I could use the following.

 

When I try to use {!obj__c.Account__r.Contact__r.Email}  merge field in a VF page, the error is:

 

  Invalid field Contact__r for SObject obj__c.

 

Any help is greatly appreciated.

 

Thank you.

mpannmpann

Remember that Accounts can have many contacts. You can only traverse object relations "upward". Accessing child object's fields will therefore not work.

 

You would have to create a lookup the the actual contact you want to reference.

QuickDEVQuickDEV

Thanks for your quick reply.

 

But the problem is,  I am trying to pull some fields from Account and contact. If I create the lookup on the custom object for the contact directly, how will I ensure that the fields are pulled from related "Account & contact"  objects?

mpannmpann

If you only had the contact lookup, you should be able to access the account fields in much the same way you had tried initially. Just "the other way around". Best practice, of course is for every contact to be associated to an account.

 

E.g:  Contact__r.Account.Name 

 

QuickDEVQuickDEV

Thank You !

 

Account & Custom object are related and in use for a while. Lot of custom coding has already been done around that.

 

Only for the contact.Email field, we need contact.

 

Not sure if I have to change all the existing stuff just for this. Thinking of trying some workaround in the controller of VF

 

page.

mpannmpann

No Problem. I'm still not sure how you will decide which contact is the one with the right email address, based just on the account, but let us know how it went.

 

QuickDEVQuickDEV
we have a custom field check box "primary" to indicate the primary contact and we need Email of that contact.