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
DahveedDahveed 

Customer Portal Custom Controller List has no rows for assignment

I every few weeks get the following in my customer portal and it's when I go to a contact in my salesforce database and login to portal as user.

 

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

Class.portalHomePageController.<init>: line 12, column 1

 

It is referencing this controller which is my portal home page.

Line 12 is oContact=[select Id,Name,AccountId,Account.Name,Account.Protocol__c, Account.Banks__c,Account.Plateforme_ASP__c from Contact where id=:oUser.ContactId];

 

This would mean that there is no contact returned for which I just logged in as portal user. Is this just a bug that will happen if the SOQL query errors? Is there something I can do to prevent this? Should I put it into a list and check it and if it's size()=0 then requery it?  There is no reason that the SOQL query would ever return null because it would mean there is no portal user.

 

Thanks for any help,

 

Dahveed

 

public class portalHomePageController {
    
    public List <AlertManagement__c> listFlux {get;set;}
    public List <Portal_Marketing__c> listFluxMK {get;set;}
    public Contact oContact {get;set;}
    public Account oAccount {get;set;}
    public User oUser {get;set;}
    
    public portalHomePageController(){
                
        oUser=[SELECT id, ContactId, LanguageLocaleKey FROM User WHERE id=:userinfo.getuserid()];
        oContact=[select Id,Name,AccountId,Account.Name,Account.Protocol__c, Account.Banks__c,Account.Plateforme_ASP__c from Contact where id=:oUser.ContactId];
        oAccount=[select id, Name, Plateforme_ASP__c, ID_base_production__c, Banks__c, Protocol__c from Account where Id=:oContact.AccountId];  

 

........rest of logic

RockDeveloperRockDeveloper

Write in constructor oUser =new User();  and oContact = new Contact();

it may solve your problem

DahveedDahveed

This is a good suggestion. I'll put it in and see if this alleviates the issue. Thanks RD