You need to sign in to do that
Don't have an account?
Donald Hursey
Using field set from related object to display field values on visualforce page
I have a request to create a visualforce page to be used as a sub tab to display field values from a related object. I am using the code below but received the error "System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Equipment__c.Leasing_Company__c"
What am I missing in my extension or vf page? I'm not sure if this is the best way to accomplish what I'm trying to do so any suggestions would be helpful.
What am I missing in my extension or vf page? I'm not sure if this is the best way to accomplish what I'm trying to do so any suggestions would be helpful.
<apex:page standardController="Equipment__c" extensions="AgreementExtV2" lightningStylesheets="true"> <apex:pageBlock id="agrDetails" title="Agreement Details"> <apex:repeat value="{!$ObjectType.Agreements__c.FieldSets.Lease_Agreement}" var="f"> <option value="{!$ObjectType.Agreements__c.Fields[f].localname}">{!$ObjectType.Agreements__c.Fields[f].label}</option> <apex:outputField value="{!Equipment__c[f]}"/> </apex:repeat> </apex:pageBlock> </apex:page> public class AgreementExtV2 { public List<Agreements__c> agreement{get; set;} public List<Equipment__c> currentRecord{get; set;} public AgreementExtV2(ApexPages.StandardController controller) { currentRecord = [SELECT Id FROM Equipment__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')]; agreement = [SELECT Id, Leasing_Company__c, Lease_ID__c, Lease_Exp_Date__c, Lease_Type__c, Leased_Amount__c, Lease_Rate__c, Lease_Term__c, Lease_Payment__c, Equipment__c FROM Agreements__c Where Equipment__c = :currentRecord]; } }
if you are using master-detail or lookup you need to use '__r'
You may change the query by adding the field:
agreement = [SELECT Id, Leasing_Company__c, Lease_ID__c, Lease_Exp_Date__c, Lease_Type__c, Leased_Amount__c, Lease_Rate__c, Lease_Term__c, Lease_Payment__c, Equipment__c,Lease_Company__c FROM Agreements__c Where Equipment__c = :currentRecord];
__r should be used in query. Hope this explanation helps you
__r represents a custom relationship. There are two uses for __r. We use it when we query a custom relationship from child to parent, or from parent to child.
For example, if you have two custom objects, called Service__c and Service_Line__c, where the Service Line has a field that references a Service as its parent, you can query from child to parent, or parent to child.
The child to parent relationship query looks like this:
SELECT Id, Service__c, Service__r.Name FROM Service_Line__c
While the parent to child relationship looks like this:
SELECT Id, Name, (SELECT Id, Name FROM Service_Lines__r) FROM Service__c
To access parent and children records in Apex Code, you'd use the same syntax:
Service_Line__c line = [select ... from service_line__c where ...]; if(line.service__r.name == 'Master Service') { // Do something } Service__c service = [select ... from service__c where ...]; for(Service_Line__c line:service.Service_Lines__r) { // Do something }
Each mechanism has a specific purpose depending on your intent.