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
pq100pq100 

Display Data from parent record on child VF layout

Hi

 

I'm using an extension to display data from the parent record (Site__c) on my VF page, this works correctly when i click on an existing record (Investment__c) in the related list, however, when i add a new Investment__c i get the following error:

 

System.QueryException: List has no rows for assignment to SObject

 

I'm guessing this is becausethe Id isn't available at this point ofr the new record. So does anyoneknow how i can get round this?

 

Code excerpt  below, Thanks for any suggestions!

Paul

 

	//get data from Parent Object
        Investment__c inv = (Investment__c)stdController.getRecord();
        
    	inv = [Select ID, Site__r.Address__c, Site__r.Postcode__c, Site__r.Town__c from Investment__c
    			where id=:stdController.getRecord().Id limit 1];
    			
    	SiteObj = inv.Site__r;
    //////

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfcksfck

Ah, that's good, because it means it already knows who the parent is. You should therefore be able to display the parent info on the page regardless of the fact that the investment record is not yet in the database. Try this:

 

//get data from Parent Object
        Investment__c inv = (Investment__c)stdController.getRecord();
        SiteObj = [select Address__c, Postcode__c, Town__c from Site__c where id = :inv.site__c];
    //////

All Answers

sfcksfck

What is the output of system.debug if you put it in here?

 

//get data from Parent Object
        Investment__c inv = (Investment__c)stdController.getRecord();
        system.debug(inv);
pq100pq100

Hi

 

Output is:

 

USER_DEBUG|[26]|DEBUG|Investment__c:{Site__c=a0iA0000003dAva}

 

Thanks

Paul

sfcksfck

Ah, that's good, because it means it already knows who the parent is. You should therefore be able to display the parent info on the page regardless of the fact that the investment record is not yet in the database. Try this:

 

//get data from Parent Object
        Investment__c inv = (Investment__c)stdController.getRecord();
        SiteObj = [select Address__c, Postcode__c, Town__c from Site__c where id = :inv.site__c];
    //////
This was selected as the best answer
pq100pq100

Perfect - thanks a lot!