You need to sign in to do that
Don't have an account?
SolidLucas
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>
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
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
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.
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()