0

I'm struggling to find this answer due to the ambiguity surrounding the many scenarios the keyword Reference can fit in to.

I have multiple Reference types in my custom object. They are lookup fields to other objects. I need to know how to display them within Visualforce. At the moment I have a wrapper in my controller but don't know what type to cast it to, or more likely, how to extract and display the actual Reference type.

Sample code:

Apex:

Inner wrapper class.

public class DisplayObject
{
    private SObject obj;

    public DisplayRAIDObject(SObject item) 
    {
        this.obj = item;
    }

    // Properties for use in the Visualforce view
    public Id ID
    {
        get { return obj.Id; }
    }
    public String Name
    {
        get { return (String)obj.get('Name'); }
    }
    public String Type
    {
        get { return (String)obj.get('Risk_Type__c'); }
    }
    public String Originator
    {
        get { return (String)obj.get('Originator__c'); }
    }
}

VF:

At the moment my visualforce is only displaying the MEMORY ADDRESS VALUE of the Originator__c field type casted in to public String Originator above. What is the correct type to use?

Please let me know your thoughts!

  • Basic idea would be to use getDescribe to get sObjectName, then get field name, and after that construct the same object, but with defined type, and use apex:inputField for that. But I would recommend to use custom lookup window -- blog.jeffdouglas.com/2011/08/12/…. – kurunve Feb 4 '16 at 9:41
  • Missed that you dont need an input, only output. Idea is the same -- use getDescribe to find to which record that lookup is, then query related record by id, and display name as link to the record (Name + Id are standard fields so you can use dynamic SOQL easily) – kurunve Feb 4 '16 at 9:43
  • Hi Kurunve, thanks for following up mate. Could you give me an example scenario of how you would use getDescribe to retrieve the related record? I've never come across that before. – MattGarnett Feb 4 '16 at 9:56
  • there are info about here - developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… You can do it dynamically like getDescribe for all fields for concrete object. As soon as you get concrete result for field, you use getReferenceTo() method to get sObject type, and do dynamic query like "select id, Name FROM ' + sObjName' WHERE Id = \'' + field_val + '\' LIMIT 1', and output that values from received sObject where needed and as needed. Some generic example is here cloudforce4u.com/2013/10/get-all-fields-of-object-in-apex.html – kurunve Feb 4 '16 at 10:34
  • I might be missing something here, but it looks like the question is simply how to display a value from a related record in Visualforce. Is this correct? If so it's quite straightforward: Originator__c will display the ID of the Originator record, Originator__r.Name will display the Name of the record, Originator__r.MyCustomField__c will display the custom field, etc. You may need to add those fields (in the same format) to any SELECT statements as well. – Alex McDonald Feb 5 '16 at 0:41

Your Answer

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

Browse other questions tagged or ask your own question.