0

I have about 2 weeks experience in Visualforce and Apex and I am having trouble with displaying an address field related to a contact object. I have a custom Order object called WIMS_Order_c. With a lookup field called Customer_c which is a lookup for the Contact object. All I want to do is upon Customer (contact) lookup selection is to populate the mailing street address label field. However I get the following error after selection:

System.QueryException: List has no rows for assignment to SObject Error is in expression '{!ContactPopulated}' in page orderentrytest2

Class.SFOEController.ContactPopulated: line 13, column 1

Please help. Very much appreciate it. Extension controller and VF code below.

Visual Force

<apex:page standardController="WIMS_Order__c"  extensions="SFOEController" sidebar="false" showHeader="false">
   <apex:sectionHeader title="Order Entry" subtitle="{!WIMS_Order__c.Customer__c}"/>
   <apex:form >
      <apex:pageBlock title="Order Entry">
         <apex:actionRegion >
            <apex:pageBlockSection title="Order Header" columns="2">
               <apex:pageBlockSectionItem >
                  <apex:outputLabel value="Customer"/>
                  <apex:outputPanel >
                     <apex:inputField value="{!WIMS_Order__c.Customer__c}">
                        <apex:actionSupport event="onchange" 
                           action="{!ContactPopulated}"
                           rerender="thePageBlock"
                           status="status"/>
                     </apex:inputField>
                     <apex:actionStatus startText="applying address..." 
                        id="status"/>
                  </apex:outputPanel>
               </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
         </apex:actionRegion>
         <apex:pageBlockSection title="Address" columns="2" id="thePageBlock" rendered="true">
            <apex:pageBlockSectionItem >
               <apex:outputLabel value="Mailing Address"/>
               <apex:outPutField value="{!WIMS_Order__c.Customer__r.MailingStreet}"/>
            </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
      </apex:pageBlock>
   </apex:form>
</apex:page>

EXT Controller

public with sharing class SFOEController 
{
 private ApexPages.StandardController stdCtrl;

 public SFOEController(ApexPages.StandardController std)
 {
  stdCtrl=std;
 }

 public void ContactPopulated()
 {
  WIMS_Order__c cont=(WIMS_Order__c) stdCtrl.getRecord();
  cont.Customer__r =[select MailingStreet, MailingCity from contact where id=:cont.id];
 }
}
1

Further to Adrian Larson's answer

public with sharing class SFOEController {
  private ApexPages.StandardController stdCtrl;
  public Contact wimsContact {get;set;}

public SFOEController(ApexPages.StandardController std) {
  stdCtrl=std;
}

public void ContactPopulated(){
WIMS_Order__c cont=(WIMS_Order_c) stdCtrl.getRecord();
wimsContact =[select id, MailingStreet, MailingCity from contact where id=:cont.Customer
_c]; }

Then change the output field to
<apex:outPutField value="{!wimsContact.MailingStreet}"/>

Let me know how that goes.

  • This is exactly what I was looking for. Thanks for the assist! – Jason J Jul 23 '13 at 5:14
1

The root cause is actually that you are looking for Contact records that have a WIMS_Order__c Id. Your query will always return an empty list because this is an impossible circumstance.

A secondary problem is you are trying to set an SObject as a lookup instead of the SObject's Id. I am fairly certain you cannot use the __r suffix for setting Lookups, so instead you would need to set cont.Customer__c = someId;.

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.