You need to sign in to do that
Don't have an account?

I'm getting a error while executing the code ,can anyone help me below issue?
Apex:
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public id accRecId;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
accRecId = [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')].accountid;
if(accRecId != null)
{
accRec = [select id,name,accountnumber,annualrevenue from account where id =:accRecId];
}
}
}
Visualforce:
<apex:page standardController="contact" extensions="inlinecontroller">
<apex:form >
<apex:pageBlock title="My Inline Visualforce page">
Account Name <apex:outputField value="{!accRec.name}"/><br/>
Account Number <apex:outputField value="{!accRec.accountnumber}"/><br/>
Annual Revenue <apex:outputField value="{!accRec.annualrevenue}"/><br/>
</apex:pageBlock>
</apex:form>
</apex:page>
Error:
Visualforce Error
Help for this Page
System.QueryException: List has no rows for assignment to SObject
Class.inlinecontroller.<init>: line 6, column 1
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public id accRecId;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
accRecId = [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')].accountid;
if(accRecId != null)
{
accRec = [select id,name,accountnumber,annualrevenue from account where id =:accRecId];
}
}
}
Visualforce:
<apex:page standardController="contact" extensions="inlinecontroller">
<apex:form >
<apex:pageBlock title="My Inline Visualforce page">
Account Name <apex:outputField value="{!accRec.name}"/><br/>
Account Number <apex:outputField value="{!accRec.accountnumber}"/><br/>
Annual Revenue <apex:outputField value="{!accRec.annualrevenue}"/><br/>
</apex:pageBlock>
</apex:form>
</apex:page>
Error:
Visualforce Error
Help for this Page
System.QueryException: List has no rows for assignment to SObject
Class.inlinecontroller.<init>: line 6, column 1
Your query is returning 0 records. that is why it is not getting any accountId to assign into accRecId as a result you are getting System.QueryException: List has no rows for assignment to SObject.
Check your query result in workbench whether it is returning any records or not.
Best practice - always fetch all records then check for its size then assign to variable.
List<Contact> contacts = [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')];
if(contacts != null && contacts.size() > 0) {
accRecId = contacts[0].accountId;
accRec = [select id,name,accountnumber,annualrevenue from account where id =:accRecId];
}
Also, try to use relationship query to improve performance :) :)
Hope it will help you.
All Answers
Thanks,
N.J
i'm getting error as below
"inlinecontroller Compile Error: Illegal assignment from Id to LIST<Account> at line 7 column 5"
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public List<Account> accRecLst;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
accRecLst = new List<Account>() ;
accRecLst = [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')].accountid;
if(accRecLst.size() > 0)
{
accRec = [select id,name,accountnumber,annualrevenue from account where id =:accRecLst[0].Id];
}
}
}
Your query is returning 0 records. that is why it is not getting any accountId to assign into accRecId as a result you are getting System.QueryException: List has no rows for assignment to SObject.
Check your query result in workbench whether it is returning any records or not.
Best practice - always fetch all records then check for its size then assign to variable.
List<Contact> contacts = [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')];
if(contacts != null && contacts.size() > 0) {
accRecId = contacts[0].accountId;
accRec = [select id,name,accountnumber,annualrevenue from account where id =:accRecId];
}
Also, try to use relationship query to improve performance :) :)
Hope it will help you.
Please find correct code posted by ankit as there was small mistake in the code.
I'm getting below error
inlinecontroller Compile Error: Variable does not exist: accRecLst at line 6 column 6
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public List<Contact> conRecLst;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
accRecLst = new List<Account>() ;
conRecLst= [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')];
if(conRecLst!=null && conRecLst.size() > 0)
{
accRecLst = [select id,name,accountnumber,annualrevenue from account where id =:conRecLst[0].accountId];
if(accRecLst.size() > 0) {
accRec = accRecLst[0];
}
}
}
}
I'm getting below error
inlinecontroller Compile Error: Incompatible types since an instance of SObject is never an instance of contact at line 6 column 14
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public id accRecId;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
conRec = (Contact)controller.getRecord();
accRecId = [select id, accountId from contact where id= :conRec.Id].accountId;
//accRecId = [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')].accountid;
if(accRecId != null)
{
accRec = [select id,name,accountnumber,annualrevenue from account where id =:accRecId];
}
}
}
I'm getting error as inlinecontroller Compile Error: unexpected token: 'List' at line 6 column 5
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public List<Contact> conRecLst;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
List<Account>() accRecLst = new List<Account> ();
conRecLst= [select id,accountid from contact where id=:ApexPages.currentPage().getParameters().get('id')];
if(conRecLst!=null && conRecLst.size() > 0)
{
accRecLst = [select id,name,accountnumber,annualrevenue from account where id =:conRecLst[0].accountId];
if(accRecLst.size() > 0) {
accRec = accRecLst[0];
}
}
}
}
Check now :) If this helps you then mark it as best answer.
Thanks
Michael
Error:inlinecontroller Compile Error: Illegal assignment from LIST<Contact> to LIST<contact> at line 8 column 1
Apex:
public with sharing class inlinecontroller {
Public contact conRec{get;set;}
Public List<Contact> conRecLst;
public List<Account> accRecLst;
Public account accRec{get;set;}
public inlinecontroller(ApexPages.StandardController controller) {
accRecLst = new List<Account>() ;
conRecLst= [select id, accountid from contact where id='00390000016KbWo'];
if(conRecLst!=null && conRecLst.size() > 0)
{
accRecLst = [select id,name,accountnumber,annualrevenue from account where id =:conRecLst[0].accountId];
if(accRecLst.size() > 0) {
accRec = accRecLst[0];
}
}
}
}
VF:
<apex:page standardController="contact" extensions="inlinecontroller">
<apex:form >
<apex:pageBlock title="My Inline Visualforce page">
Account Name <apex:outputField value="{!accRec.name}"/><br/>
Account Number <apex:outputField value="{!accRec.accountnumber}"/><br/>
Annual Revenue <apex:outputField value="{!accRec.annualrevenue}"/><br/>
</apex:pageBlock>
</apex:form>
</apex:page>
It is working fine at my end. Please check again if you missed any thing. Find above snapshot of output fields.
If it helps you then mark it as best answer. :)
Thanks
Michael
error
Make C capital of contact in Query.
Thanks
Michael