Monday, July 28, 2008

SOQL within a Visualforce Page



I'm trying to figure out how I can query a related object in Visualforce, without hammering javascript into my VF page, or using a custom controller.

That's the VF page (above), here's the code that generates it.

It's easy to access the data from the standard Case controller, and it's to traverse one level up to the Account parent of this Case record. But I've been staring at the code trying to figure out how to get the Name field from the related Switch__c object.

I've tried hacking some Select queries into the VF page, but that didn't work out so well. I don't want to java script something so trivial.

It seems like I should be able to reference "{!Case.Switch__c.Name}" in the same way that I reference {!Case.Account.Name}, but traversing parent-child doesn't seem behave the same for related records.

Back to the books ... or maybe a plea for help on the dev forums.

2 comments:

  1. I think you need to do 2 things.

    1) Go to the Case object and edit its lookup field to the Switch object. Enter a Relationship Name (e.g. Switch). It is probably blank right now.

    2) The field reference should be {!Case.Switch__r.Name}

    This is how it works in SOQL anyway, so I figure it's the same thing in VF. I didn't test it.

    ReplyDelete
  2. I think your issue has a simple solution, but first I need more info:

    1. Have you created the relationship between the object?
    2. What kind of relationship have you created?
    3. Which object is the parent, and which is the child(syntax varies depending on the direction of access)?

    If you can send me some code I'd be happy to help.

    Cheers,
    Wes

    ReplyDelete