2

enter image description hereI want to populate Account Number when the user click Account lookup field,but i got this error System.QueryException: List has no rows for assignment to SObject Error is in expression '{!AccountPopulated}' in page lookup_populate Class.Lookup_populate.AccountPopulated: line 8, column 1.

    public class Lookup_populate{

    public String AccNo{get;set;}
    public Id AccountId;
 public Account cont {get{cont = new Account();}set;} 
public pagereference AccountPopulated()
{
    cont=[select AccountNumber from Account where id=:cont.ParentId limit 1];
    AccNo= cont.AccountNumber;
    return null;
}
}
<apex:page controller="Lookup_populate" >
    <apex:form >
  <apex:pageBlock >
    <apex:pageBlockSection id="accinfo" title="Account Information">
    <apex:actionRegion >
      <apex:inputField value="{!cont.ParentId}">
            <apex:actionSupport event="onchange" action="{!AccountPopulated}" rerender="accinfo"/> 
         </apex:inputField>
         </apex:actionRegion>
         <apex:inputText value="{!AccNo}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>  
        </apex:form>
</apex:page>
3

You have getter setter on Contact and hence follwoing change in query should solve your problem

public class Lookup_populate{

public String AccNo{get;set;}
public Id AccountId;
public Account cont {get;set;} 

public Lookup_populate(){
   cont =new Account ();
}

   public pagereference AccountPopulated(){
  cont=[select AccountNumber,ParentId  from Account where id=:cont.ParentId limit 1];
  AccNo= cont.AccountNumber;
  return null;
  }
 }

   <apex:page controller="Lookup_populate" >
   <apex:form >
 <apex:pageBlock >
   <apex:pageBlockSection title="Account Information">
     <apex:inputField value="{!cont.ParentId}">
        <apex:actionSupport event="onchange" action="{!AccountPopulated}" rerender="accinfo"/> 
       </apex:inputField>
       <apex:outputPanel >
       <apex:inputText value="{!AccNo}" id="accinfo"/>
       </apex:outputPanel>
    </apex:pageBlockSection>
     </apex:pageBlock>  
    </apex:form>
 </apex:page>

The basic problem is AccountId is coming null and i dont see you are populating the same .Using the reference should solve the problem

  • thanks for reply I have tried this one it gives the same error. – Udaya Dec 16 '13 at 14:06
  • edited the answer .The instance should be inside getter – Mohith Shrivastava Dec 16 '13 at 14:16
  • thanks for reply I have tried this but i getting this error Error: Lookup_populate Compile Error: Non-void method might not return a value or might have statement after a return statement. at line 4 column 28 – Udaya Dec 16 '13 at 14:22
  • hey if you use pagereference return null else on void dont return anything.Please see the return type – Mohith Shrivastava Dec 16 '13 at 14:35
  • Sorry for the trouble but i getting the same error and i add a error image – Udaya Dec 16 '13 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.