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
SolidLucasSolidLucas 

List has no rows for assignment to SObject VisualForce

Hello guys, i'm trying to create a visualforce that returns the account and contacts, of the logged user but its returning this error List has no rows for assignment to SObject.

this is my controller

public with sharing class AnalyticsRevendaController {
	public Account contas   {get;set;} 
    public Contact contato  {get;set;}
    public User	   usuario  {get;set;}
    public String  userId 	{get;set;}
    
  
    // Detecta os valores do usuário ativo
	 public User activeUser = [SELECT AccountId,ContactId FROM User WHERE Id = :userId LIMIT 1];
     public String userAccount = activeUser.AccountId;
   	 public String userContact = activeUser.ContactId;
    
    public AnalyticsRevendaController(){
    //Retorna os dados do usuário Logado
    contas = new Account();
    userId = UserInfo.getUserId(); 
    if(userId != null && ''.equals(userId))
    	getAccount();
    } 
    
    //retorna os valores da conta do usuário ativo.
    public Account getAccount(){
			//Set<Id> setConta = new Set<Id>();
		List<Account> listContas = [SELECT Id,Name,OwnerId FROM Account WHERE Id = :userAccount];
			if(listContas.size()> 0)
				contas = listContas[0];
			return contas;
    }
    
    //retorna os valores do contato	ativo.
    public Contact getContact(){
    		 contato = [Select c.LastName, c.FirstName, c.AccountId,c.Email From Contact c
    				    WHERE Id=:userContact];
    		return contato;		   
    	}	
	}
 

my visualforce

 

<apex:page controller="AnalyticsRevendaController">
	
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection  title="Conta">
                <apex:outputtext value ="{!contas.Name}" />
                <apex:outputtext value ="{!contas.OwnerId}" />
            </apex:pageBlockSection>
              <apex:pageBlockSection  title="Contato">
                <apex:outputtext value ="{!Contact.FirstName}" />
                <apex:outputtext value ="{!Contact.LastName}" />
                <apex:outputtext value ="{!Contact.Email}" />
            </apex:pageBlockSection>
            <apex:pageBlockSection  title="Usuario">
                <apex:outputtext value ="{!$User.FirstName}" />
                <apex:outputtext value ="{!$User.LastName}" />
                <apex:outputtext value ="{!$User.Email}" />
                <apex:outputtext value ="{!userId}" />  
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 

 

Best Answer chosen by SolidLucas
John PipkinJohn Pipkin
No problem.

AccountId and ContactId are only for partner users. If you are testing, I would hard-code a partner user id just to test with since we know that the real query is at least working. Once you're done testing, you can change it back to id =:UserInfo.getUserId()

All Answers

John PipkinJohn Pipkin
The "userAccount" variable has not been set by the time you call the method in the constructor. Try this:
 
public with sharing class AnalyticsRevendaController {
	public Account contas   {get;set;} 
    public Contact contato  {get;set;}
    public User	   usuario  {get;set;}
    public String  userId 	{get;set;}
    
  
    // Detecta os valores do usuário ativo
	 public final User activeUser;
     public final String userAccount;
   	 public final String userContact;
    
    public AnalyticsRevendaController(){
    //Retorna os dados do usuário Logado
    activeUser = [Select Id,AccountId,ContactId from User where Id = :UserInfo.getUserId()];  
    userId = activeUser.Id;
    userAccount = activeUser.AccountId;
    userContact = activeUser.ContactId;
    if(userId != null && userId != '')
    	getAccount();
    } 
    
    //retorna os valores da conta do usuário ativo.
    public Account getAccount(){
			//Set<Id> setConta = new Set<Id>();
		List<Account> listContas = [SELECT Id,Name,OwnerId FROM Account WHERE Id = :userAccount];
			if(listContas.size()> 0)
				contas = listContas[0];
			return contas;
    }
    
    //retorna os valores do contato	ativo.
    public Contact getContact(){
    		 contato = [Select c.LastName, c.FirstName, c.AccountId,c.Email From Contact c
    				    WHERE Id=:userContact];
    		return contato;		   
    	}	
	}

 
SolidLucasSolidLucas

Hello john i've did a debug and keeps give me the same error 

11:14:25.852 (852499303)|FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

John PipkinJohn Pipkin
Do you have an Account and Contact connected to your SF user? Also, I just noticed that the IDs are being declared as String data types. Try declaring as ID type
SolidLucasSolidLucas

i've notice that my user doesn't have the account id, i believe beacuse of that it is give me error, you know how can i create an Accountid to my user. And thanks to be helping me i really appreciate.

John PipkinJohn Pipkin
No problem.

AccountId and ContactId are only for partner users. If you are testing, I would hard-code a partner user id just to test with since we know that the real query is at least working. Once you're done testing, you can change it back to id =:UserInfo.getUserId()
This was selected as the best answer
SolidLucasSolidLucas
Hey John, i've did the hard code that you talk about it, and work! thank you very much :)
John PipkinJohn Pipkin
No problem!