function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
caterina ferrigno 8caterina ferrigno 8 

Cross object

Hello, 
I have a custom object 'fattura__c' that has a look up relationship with a standard object 'quote' ;
'quote' has a master detail relationship with 'opportunity' and opportunity has a look up relationship with 'account'.
These are the steps: I select an opportunity, then in the opportunity's related list I select a quote, and in the quote's related list i select a fattura.
I have a visualforce page on 'fattura__c' and I want some of fattura's fields must be automatically populated, taking them directly from the account (fattura__c doesn't have any kind of link with Account).
How can i write this in an apex class?

Thanks a lot!
Best Answer chosen by caterina ferrigno 8
Apoorv Saxena 4Apoorv Saxena 4
Hi Caterina,

You could query and get all the fields from Account that you want and then use these fields on your visualforce page.
You need to query something like this : 

Select id,name,quote__r.Opportunity__r.account.name from fattura__c

Here '__r' represent relationship, so if your lookup field api name is 'quote__c' then use 'quote__r' to access any field of Quote, similarly if quote has a lookup field whose API name is 'Opportunity__c', then you can access any field on Opportunity like 'quote__r.Opportunity__r' and similarly to access any 'Account' field you could traverse like : quote__r.Opportunity__r.account.anyfieldApiName.

Please let me know if this helps you!

Thanks,
Apoorv

All Answers

Apoorv Saxena 4Apoorv Saxena 4
Hi Caterina,

You could query and get all the fields from Account that you want and then use these fields on your visualforce page.
You need to query something like this : 

Select id,name,quote__r.Opportunity__r.account.name from fattura__c

Here '__r' represent relationship, so if your lookup field api name is 'quote__c' then use 'quote__r' to access any field of Quote, similarly if quote has a lookup field whose API name is 'Opportunity__c', then you can access any field on Opportunity like 'quote__r.Opportunity__r' and similarly to access any 'Account' field you could traverse like : quote__r.Opportunity__r.account.anyfieldApiName.

Please let me know if this helps you!

Thanks,
Apoorv
This was selected as the best answer
caterina ferrigno 8caterina ferrigno 8
Hi Apoorv,
I have written this query in my apex class:

List<fattura__c> fattlist = [SELECT Preventivo__r.Opportunity.Account.Percentuale_IVA_def__c, Preventivo__r.Opportunity.Account.Codice_IVA__c, Preventivo__r.Opportunity.Account.Note_esenzione_IVA__c FROM Fattura__c];

How can I write this in the visualforce page?
This is my section:

<apex:pageBlockSection title="Informazioni pagamento e IVA" collapsible="FALSE" >   
                <apex:inputField value="{!Fattura__c.Modalit_di_pagamento__c}"/>
                <apex:inputField value="{!Fattura__c.Annotazioni__c}"/>
                <apex:inputField value="{!Fattura__c.IVA__c }" required="true"/>  
   </apex:pageblockSection> 

I want that custom fields 'Annotazioni__c' and 'IVA__c' are equal to account's fields 'Note_esenzione_IVA__c' and 'Percentuale_IVA_def__c'.

Thanks!
Apoorv Saxena 4Apoorv Saxena 4
Hi Caterina,

You will need to declare this 'fattlist' as an {get;set;} auto-property like:

public List<fattura__c> fattlist{get;set;}

then query the records and use this list on visualforce page through pageblockTable.

Below is the link that shows how to display data on visuaforce page using pageBlockTable:

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_iteration_components.htm

Hope this helps!
caterina ferrigno 8caterina ferrigno 8
Thank you very much Apoorv!