+ Start a Discussion
udayar_jayamudayar_jayam 

populate Account Number when the user click Account lookup field

I 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;set;}
public void AccountPopulated()
{
cont = new Account();
cont=[select AccountNumber from Account where id=:AccountId limit 1];
AccNo= cont.AccountNumber;
}
}

 

<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>

udayar_jayamudayar_jayam

I got the solution

 

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>