3

I'm not sure if this is allowed but I wanted to clarify something. I have a lookup table field. For the sake of an example, let's call it AccountId. I want the specific name from a specific AccountId. Normally, I would do it like this:

List<Asset> AssetList = [SELECT AccountID From Asset];
List<Account> AccountList = [Select Name FROM Account where ID IN: AssetList];

My question is basically can I avoid doing two Select Statements in order to get the Name from a specific ID? If I have the AccountId already, is there any way of getting just the Name from that ID without doing a select statement?

3

Yes, you can do it like this:

List AssetList = [ Select Account.Name From Asset ];

Instead of using AccountID use Account. Generally when you see a [Object]ID on standard Salesforce objects you can drop the ID and use as a relationship field, just like custom fields where you see __r.

  • That's exactly what I was looking for. Thanks for the explanation too! Helped clear it up for me. – SSadat Jul 5 '13 at 15:21
  • No problem, glad to help. – Phil Hawthorn Jul 5 '13 at 15:26

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.