8

I have a Visualforce page for creating new Cases. During this process, I want to retrieve the contact's mailing address whenever the contact lookup field is changed.

I'm using ActionSupport but it's not working. When I enter a value in the contact lookup field and check the view state, caseMR is always null and so there's no value for the lookup field either.

I've tried a few things like using/not using immediate="true" or using/not using but nothing changes.

I've looked at this thread but it doesn't work for me. Have I missed something obvious? Populate address field upon Contact lookup selection

Keep in mind that I am creating a new Case in the Visualforce page, not working with an existing case.

I'm wondering if I have to save the Case first before I can do the contact lookup cleanly.

Thanks!

Here's my VF page:

<apex:page standardController="Case" extensions="tempLookup" showHeader="true" id="casePage" >
<apex:pageMessages id="pageMessages" />
<apex:form id="casePageForm">
    <apex:pageBlock title="Case" id="casePageBlock" >
        <apex:ActionRegion >        
        <apex:pageBlockSection title="Case Detail" columns="1" collapsible="false" id="caseDetail">
            <apex:pageBlockSectionItem id="caseContactItem">
                <apex:outputLabel value="Contact" />
                <apex:inputField value="{!caseMR.ContactId}" required="true" id="caseContact">
                    <apex:actionSupport event="onchange" immediate="true" action="{!obtainAddress}" rerender="resContactAddress" /> 
                </apex:inputField>
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
        </apex:ActionRegion>
        <apex:pageBlockSection title="Contact Address Info" id="ResContactAddress" >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Contact Mailing Street" />
                <apex:outputText value="{!con.MailingStreet}" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>

Here's my class:

    public class tempLookup {

    public Case caseMR { get; set; }
    public Contact con { get; set; } 

    public tempLookup(ApexPages.StandardController std) {
        caseMR = (Case)std.getRecord();
    }

    public void obtainAddress() {
        if(caseMR.ContactId != null) {
            con = [select MailingStreet from Contact where Id = :caseMR.ContactId];
        }
    }
}

2 Answers 2

5
public class tempLookup {

public Case caseMR { get; set; }
public Contact con { get; set; } 

public tempLookup(ApexPages.StandardController std) {
    caseMR=new Case();//Create a new instance 
    con=new Contact();
}

public pagereference obtainAddress() {
    if(caseMR.ContactId != null) {
        con = [select MailingStreet from Contact where Id = :caseMR.ContactId];
    }

    return null;
  }
}

 <apex:page standardController="Case" extensions="tempLookup" showHeader="true" id="casePage" >
   <apex:pageMessages id="pageMessages" />
      <apex:form id="casePageForm">
        <apex:pageBlock title="Case" id="casePageBlock" >       
           <apex:pageBlockSection title="Case Detail" columns="1" collapsible="false" id="caseDetail">
        <apex:pageBlockSectionItem id="caseContactItem">
            <apex:outputLabel value="Contact" />
            <apex:inputField value="{!caseMR.ContactId}" required="true" id="caseContact">
                <apex:actionSupport event="onchange"  action="{!obtainAddress}" rerender="ResContactAddress" /> 
            </apex:inputField>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>

    <apex:pageBlockSection title="Contact Address Info" id="ResContactAddress" >
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Contact Mailing Street" />
            <apex:outputText value="{!con.MailingStreet}" />
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>

You will have to instantiate a new instance to get All the values of contacts else only contact linked with the particular case will come as you guessed .I have completely modified your code .

enter image description here

0

Mohith, thanks for your reply. I found a solution on Bob Buzzard's blog that fits my needs: http://bobbuzzard.blogspot.com/2011/11/retrieve-related-object-fields.html

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

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

 public void AccountPopulated()
 {
  Contact cont=(Contact) stdCtrl.getRecord();
  cont.Account=[select AccountNumber, Site from Account where id=:cont.AccountId];
 }
}

    <apex:page standardcontroller="Contact" extensions="RelatedController">
 <apex:form >
   <apex:pageMessages id="msgs"/>
   <apex:pageBlock title="Contact Create/Edit">
      <apex:pageBlockSection title="Contact Information">
       <apex:inputField value="{!contact.FirstName}"/>
       <apex:inputField value="{!contact.LastName}"/>
      </apex:pageBlockSection>
       <apex:actionRegion >
          <apex:pageBlockSection id="accinfo" title="Account Information">
         <apex:inputField value="{!contact.AccountId}">
            <apex:actionSupport event="onchange" action="{!AccountPopulated}" rerender="accinfo, msgs"/> 
         </apex:inputField>
         <apex:outputField value="{!contact.Account.AccountNumber}"/>
         <apex:outputField value="{!contact.Account.Site}"/>
        </apex:pageBlockSection>
      </apex:actionRegion>
      <apex:pageBlockButtons >
        <apex:commandButton value="Cancel" action="{!cancel}"/>
        <apex:commandButton value="Save" action="{!save}"/>
      </apex:pageBlockButtons>
   </apex:pageBlock>
  </apex:form>
</apex:page>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .