1

In apex you can make a query like

SELECT id , name , **account__r.name** FROM schemaname.contact 

where account__r.name is grabbing the related accounts name.

Is it possible to do this in java with a postgres database? or to do something similar. Without specifically querying each table individually

2
  • What do you mean exactly by related accounts? Is it something related to your business? is it another table/relation in database? Commented Jan 9, 2018 at 22:33
  • so in SFDC, you have a lookup field on an object, so for example. My contact has a lookup to account. so in your query you can use account__r.name which will grab the account's name field in postgres, the field as held as just an id, so for example the field name is accountid is there any way to grab the accounts name when querying from contact? Commented Jan 9, 2018 at 22:43

1 Answer 1

1

Based on your comment, it seems that this ** enclosed fields are being treated specially by SFDC. So you can't access that data as easy as you do in your Apex code. When you're developing using Apex it handles many such underlying stuff for you automatically. In Java and PostgreSQL, you have much more flexibility (due to working in a lower layer and less abstraction) but of course you will lose such fancy features. It's a trade-off. If the data you're talking about is being stored in postgresql, then they're definitely tables and there is no way other than querying them directly by joining multiple tables. My guess is that in its simplest form, it would be something like this (it's just an example):

SELECT c.id, c.name, ac.name 
FROM contact c 
    JOIN account ac ON c.account_id = ac.id;

The problem is that your code would be definitely coupled with the way your data is being model in SFDC on the database layer. So if they change something via other tools, then your code stops working. I'm not familiar with this SFDC and Apex thing, if the data model design is being done by you, then it worth thinking about doing it this way, otherwise it's more future-proof to stick to a single solution/framework/tool and let it handle all underlying stuff.

EDIT: Considering your comment regarding the store --> account --> contact relationship, then your query would be something like this:

SELECT c.id, c.name, ac.name, st.city
FROM store st join account ac on ac.store_id = st.id
    JOIN contact c on c.accountid = ac.id
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is exactly what I'm looking for but the initial scenario I posted is way simpler than what my business needs are, How would I go an object deeper? if maybe a store held the account, and i want to get the store's city so store --> account --> contact but i want to query the contact is it like select c.id, c.name, ac.name , st.city from contact c join account ac on c.accountid = ac.id join store st on ac.id = st.id could you please tell me if I understand correctly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.