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
Sebastien PlanteSebastien Plante 

Trying to display the lasted entry of a related list... but getting "Content cannot be displayed: List has no rows for assignment to SObject"

I'm trying to display the last entry of a custom related list (BPG) from Account.

I'm using this
 
public class BGP_ControllerExtension {

    public String currentRecordId {get;set;}
    
    public BGP_ControllerExtension(ApexPages.StandardController controller) {
        currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
    }

    public Business_Generator_Profile__c      BGPList {
        get {
           if ( BGPList == null ) {
          
                Business_Generator_Profile__c[] BGPList =
                [   SELECT  Name, Dealer__c, Account__r.Id
                    FROM    Business_Generator_Profile__c
                    WHERE Account__r.Id = :currentRecordId
                    ORDER BY CreatedDate DESC
                    LIMIT 1
                ];
            }          
            
           return BGPList;
           
        }
        private set;
    }

}

and the "sample" VF page (I will complete it when fixed! :P)
<apex:page standardController="Account"  extensions="BGP_ControllerExtension" showHeader="false">
  
  <apex:pageBlock>
    <apex:PageBlockSection title="Business Generator Profile ({!BGPList.Name})">
        <apex:outputField value="{!BGPList.Dealer__c}"/>
    </apex:PageBlockSection>
  </apex:pageBlock>
    
    
</apex:page>
and it's working fine, except for one thing... When there's nothing on the related list, It display "Content cannot be displayed: List has no rows for assignment to SObject". I wish to change that to a custom message, simply "No BGP entered or something like this".

I've been trying many things found on the internet... but I can't seems to find the best way for doing it.

Using a list seems to be the "best idea", but I never manged to get anything on the VF page... I'm lost! :'(
 
Best Answer chosen by Sebastien Plante
Raj VakatiRaj Vakati
Try this code
 
public class BGP_ControllerExtension {

    public String currentRecordId {get;set;}
    
    public BGP_ControllerExtension(ApexPages.StandardController controller) {
        currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
    }

    public Business_Generator_Profile__c      BGPList {
        get {
			try{
           if ( BGPList == null ) {
          
                Business_Generator_Profile__c[] BGPList =
                [   SELECT  Name, Dealer__c, Account__r.Id
                    FROM    Business_Generator_Profile__c
                    WHERE Account__r.Id = :currentRecordId
                    ORDER BY CreatedDate DESC
                    LIMIT 1
                ];
            }          
            
           return BGPList;
			}catch(Exception e){
			 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No BGP entered or something like this'));
    return null;
			}
        }
        private set;
    }

}
 
<apex:page standardController="Account"  extensions="BGP_ControllerExtension" showHeader="false">
  <apex:pageMessages ></apex:pageMessages>

  <apex:pageBlock>
    <apex:PageBlockSection title="Business Generator Profile ({!BGPList.Name})">
        <apex:outputField value="{!BGPList.Dealer__c}"/>
    </apex:PageBlockSection>
  </apex:pageBlock>
    
    
</apex:page>

 

All Answers

Sebastien PlanteSebastien Plante
Sorry, this is the code working, the one I've posted was  my lastest test... not working.
 
public class BGP_ControllerExtension {

    public String currentRecordId {get;set;}
    
    public BGP_ControllerExtension(ApexPages.StandardController controller) {
        currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
    }

    public Business_Generator_Profile__c      BGPList {
        get {
            if ( BGPList == null ) {
                BGPList =
                [   SELECT  Name, Dealer__c, Account__r.Id
                    FROM    Business_Generator_Profile__c
                    WHERE Account__r.Id = :currentRecordId
                    ORDER BY CreatedDate DESC
                    LIMIT 1
                ];
            }          
            
           return BGPList;
           
        }
        private set;
    }

}

 
Raj VakatiRaj Vakati
Try this code
 
public class BGP_ControllerExtension {

    public String currentRecordId {get;set;}
    
    public BGP_ControllerExtension(ApexPages.StandardController controller) {
        currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
    }

    public Business_Generator_Profile__c      BGPList {
        get {
			try{
           if ( BGPList == null ) {
          
                Business_Generator_Profile__c[] BGPList =
                [   SELECT  Name, Dealer__c, Account__r.Id
                    FROM    Business_Generator_Profile__c
                    WHERE Account__r.Id = :currentRecordId
                    ORDER BY CreatedDate DESC
                    LIMIT 1
                ];
            }          
            
           return BGPList;
			}catch(Exception e){
			 ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No BGP entered or something like this'));
    return null;
			}
        }
        private set;
    }

}
 
<apex:page standardController="Account"  extensions="BGP_ControllerExtension" showHeader="false">
  <apex:pageMessages ></apex:pageMessages>

  <apex:pageBlock>
    <apex:PageBlockSection title="Business Generator Profile ({!BGPList.Name})">
        <apex:outputField value="{!BGPList.Dealer__c}"/>
    </apex:PageBlockSection>
  </apex:pageBlock>
    
    
</apex:page>

 
This was selected as the best answer
Sebastien PlanteSebastien Plante

Woohoo ! Working. Thanks a lot !

 

The "apex:pageMessages" need to have a field else it won't display (dosent call the controller?), but since I use the rendered, it's working fine :D

 

<apex:page standardController="Account"  extensions="BGP_ControllerExtension" showHeader="false">

   <apex:pageMessages rendered="{!IF(BGPList.Name=='',True,False)}" />

  <apex:pageBlock rendered="{!IF(BGPList.Name=='',False,True)}">
    <apex:PageBlockSection title="Business Generator Profile ({!BGPList.Name})">
        <apex:outputField value="{!BGPList.Dealer__c}"/>
    </apex:PageBlockSection>
  </apex:pageBlock>
    
    
</apex:page>