0

I have an outputlink in visualforce page that uses a custom field as a display name of link instead of Name field. Link successfully displays when Name is used. However, I tried using other custom field named Field1__c, but link does not appear. Why is that?

By the way, Field1__c is a formula text field concatenating the Name standard field and a custom number field. Also, Field1__c has a value when viewed in record detail page.

Visualforce:

<!--portion-->
<apex:pageBlockTable value="{!customList}" var="c" rendered="{!customList != null}">
   <apex:column headerValue="Custom Record">
        <apex:outputLink value="/{!c.Id}">{!c.Field1__c}</apex:outputLink>
   </apex:column>
</apex:pageBlockTable>       

Controller

public class CustomController{

    public List<Contact> contactList{get;set;}
    public List<Custom__c> customList{get;set;}
    public Account accRecord;
    public CustomController(ApexPages.StandardController controller){
        this.accRecord = (Account)controller.getRecord();
        accRecord = [SELECT New_Field__c FROM Account WHERE Id=: accRecord.Id];
        customList = new List<Custom__c>();

        contactList = new List<Contact>();
        if(accRecord.New_Field__c != null){
            contactList = [SELECT Id,Name,Custom__c,Custom__r.Field1__c FROM Contact WHERE New_Field__c =: accRecord.New_Field__c];

            if(contactList.size()==0){
                contactList = null;
            }else{

                for(Contact c : contactList){

                    customList.add(new Custom__c(Id=c.Custom__c, Name = c.Custom__r.Custom_Name__c));

                }

                if(customList.size() == 0){
                    customList = null;
                }
            }
        }     
    }

}

Modified Apex Class:

public class CustomController{

    public List<Contact> contactList{get;set;}
    public List<CustomWrapper> customList {get;set;}
    public Account accRecord;
    public CustomController(ApexPages.StandardController controller){
        this.accRecord = (Account)controller.getRecord();
        accRecord = [SELECT New_Field__c FROM Account WHERE Id=: accRecord.Id];
        customList = new List<CustomWrapper>();

        contactList = new List<Contact>();
        if(accRecord.New_Field__c != null){

            contactList = [SELECT Id,Name,Custom__c,Custom__r.Field1__c, Custom__r.Custom_Name__c FROM Contact WHERE New_Field__c =: accRecord.New_Field__c];

            if(contactList.size()==0){
                contactList = null;
            }else{

                Map<Id, CustomWrapper> customMap = new Map<Id, CustomWrapper>();

                for(Contact c : contactList){
                    Custom__c customObj = new Custom__c(Id=c.Custom__c, Name = c.Custom__r.Custom_Name__c);
                    CustomWrapper wrapper = new CustomWrapper(customObj, c.Custom__r.Field1__c);
                    customMap.put(customObj.Id, wrapper);
                }

                customList = customMap.values();
                if(customList.size() == 0){
                    customList = null;
                }
            }
        }     
    }

    public class CustomWrapper {

        public Custom__c customObj {get; set;}
        public String fieldValue {get; set;}

        public CustomWrapper(Custom__c customObj, String fieldValue) {
            this.customObj = customObj;
            this.fieldValue = fieldValue;
        }
    }
}
9
  • 2
    Your code refers {!c.Field__c} and you want value from Field1__c. Please update your post if that's not what your actual code looks like.
    – jagmohan
    Commented Mar 9, 2018 at 12:22
  • Apologies! I've modified the code just now. But still, link does not appear.
    – curdual
    Commented Mar 9, 2018 at 12:26
  • 1
    Is Field1__c visible in Field-Level Security to the user viewing the page?
    – David Reed
    Commented Mar 9, 2018 at 12:27
  • Yes, I made sure it is visible in field-level security.
    – curdual
    Commented Mar 9, 2018 at 12:28
  • Can you please post your controller code as well?
    – jagmohan
    Commented Mar 9, 2018 at 12:55

1 Answer 1

1

Here is the update controller code. I have added comments to code where you aren't querying fields which is why its Field1__c is not showing up on page. I also added a recommendation to use Map to collect unique records.

VF Page

<apex:page standardController="Account" extensions="CustomController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!customList}" var="c" rendered="{!customList != null}">
                <apex:column headerValue="Custom Record">
                    <apex:outputLink value="/{!c.customObj.Id}">{!c.fieldValue}</apex:outputLink>
                </apex:column>
            </apex:pageBlockTable> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Controller

public class CustomController {

    public List<Contact> contactList{get;set;}
    public List<CustomWrapper> customList {get;set;}
    public Account accRecord;

    public CustomController(ApexPages.StandardController controller){
        this.accRecord = (Account)controller.getRecord();
        accRecord = [SELECT New_Field__c FROM Account WHERE Id=: accRecord.Id];
        customList = new List<CustomWrapper>();

        contactList = new List<Contact>();
        if(accRecord.New_Field__c != null){
            // on this line you were not querying Custom_Name__c field, I have added that
            contactList = [
                SELECT Id, Name, Custom__c, Custom__r.Field1__c, 
                Custom__r.Custom_Number__c, Custom__r.Custom_Name__c 
                FROM Contact 
                WHERE New_Field__c =: accRecord.New_Field__c
            ];

            if(contactList.size()==0){
                contactList = null;
            }else{
                // recommend putting objects in a map to avoid duplicates
                Map<Id, CustomWrapper> customMap = new Map<Id, CustomWrapper>();
                // fixed a typo from Contat to Contact
                for(Contact c : contactList){
                    Custom__c customObj = new Custom__c(Id=c.Custom__c, Name = c.Custom__r.Custom_Name__c);
                    CustomWrapper wrapper = new CustomWrapper(customObj, c.Custom__r.Field1__c);
                    customMap.put(customObj.Id, wrapper);
                }
                // assign objects to list
                customList = customMap.values();
                if(customList.size() == 0){
                    customList = null;
                }
            }
        }     
    }

    public class CustomWrapper {

        public Custom__c customObj {get; set;}
        // use wrapper class property for formula field
        public String fieldValue {get; set;}

        public CustomWrapper(Custom__c customObj, String fieldValue) {
            this.customObj = customObj;
            this.fieldValue = fieldValue;
        }
    }
}

Hope this helps.

8
  • Thanks for your immediate response. However, I've tried this also last time, and since Field1__c is a formula field, saving the controller would result to this error: Field is not writeable.
    – curdual
    Commented Mar 11, 2018 at 12:55
  • I've just used your code as a reference, but upon saving the controller, I am encountering error Variable does not exist: customObj on this line: customMap.put(customObj.Id, wrapper);
    – curdual
    Commented Mar 12, 2018 at 4:19
  • @curdual Can you please update your controller code in the question with the latest one you have in your org?
    – jagmohan
    Commented Mar 12, 2018 at 5:32
  • Modified controller has been added to the post. I just followed the sample you provided.
    – curdual
    Commented Mar 12, 2018 at 9:36
  • @curdual There is typo in the code. This line has extra closing bracket Custom__c customObj = new Custom__c(Id=c.Custom__c, Name = c.Custom__r.Custom_Name__c));. Please change it to Custom__c customObj = new Custom__c(Id=c.Custom__c, Name = c.Custom__r.Custom_Name__c);
    – jagmohan
    Commented Mar 12, 2018 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .