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
SREENIVASULU.CSREENIVASULU.C 

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
Best Answer chosen by SREENIVASULU.C
Michael DsozaMichael Dsoza
Hi sreenu c 3,

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

Nilesh Jagtap (NJ)Nilesh Jagtap (NJ)
Replace line no 6 with following code:
conRec = (Contact)controller.getRecord();
accRecId = [select id, accountId from contact where id= :conRec.Id].accountId;



Thanks,
N.J
Ankit AroraAnkit Arora
Or you can go with this (correct me if am wrong)
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];
         }
    }
}


SREENIVASULU.CSREENIVASULU.C
Hi Ankit ,

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];
         }
    }
}
Michael DsozaMichael Dsoza
Hi sreenu c 3,

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. 
This was selected as the best answer
SREENIVASULU.CSREENIVASULU.C
Thank you ...Michael Dsoza
Michael DsozaMichael Dsoza
Hi sreenu c 3,

Please find correct code posted by ankit as there was small mistake in the code.
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];
         }
         }
    }
}


SREENIVASULU.CSREENIVASULU.C
Hi Michael Dsoza, 

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];
         }
         }
    }
}
SREENIVASULU.CSREENIVASULU.C
Hi NJ,

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];
         }
    }
}
SREENIVASULU.CSREENIVASULU.C
Hi Michael Dsoza,

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];
         }
         }
    }
}
Michael DsozaMichael Dsoza
Code is working fine at my end...

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];
         }
         }
    }
}

Check now :) If this helps you then mark it as best answer.

Thanks
Michael
SREENIVASULU.CSREENIVASULU.C
Hi Michael Dsoza,

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>
Michael DsozaMichael Dsoza
Working Fine at my end

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
SREENIVASULU.CSREENIVASULU.C
Hi Michael Dsoza,

error
 
   User-added image
Michael DsozaMichael Dsoza
Hi sreenu c 3,

Make C capital of contact in Query.

Thanks
Michael