0

I have a Contact lookup that the user selects:

<apex:page standardController="Software_Activation__c" extensions="SoftwareActivationController">
    ...
    <apex:inputField id="contact" value="{!Software_Activation__c.Contact__c}"/>
    <apex:inputText id="shipToEmail" value="{!Software_Activation__c.Contact__r.Email}" disabled="true" />
    ...
</apex:page>

As you can see, I also expose the email (Software_Activation__c.Contact__r.Email) of this Contact. But, there is a problem. I thought this would work to set the value of the inputText once a Contact is selected via the lookup inputField, but it is not set automatically. It is only set when I load my page with the contact (Software_Activation__c.Contact__c) already set.

How can I populate the inputText's value automatically when a lookup for a Contact happens?

Edit: I have tried the following but it does not update the value of the inputText field:

<apex:inputField id="contact" value="{!Software_Activation__c.Contact__c}">
    <apex:actionSupport event="onchange" action="{!ContactSelected}" reRender="shipToEmail" />
</apex:inputField>
<apex:inputText value="{!Software_Activation__c.Contact__r.Email}" id="shipToEmail" disabled="true" />

Here is my controller:

public class SoftwareActivationController {

    private final Software_Activation__c activation;

    public SoftwareActivationController(ApexPages.StandardController controller) {
        controller.addFields(new List<String>{'Activation_ID__c'});
        activation = (Software_Activation__c)controller.getRecord();
    }

    public PageReference ContactSelected() {
        return null;
    }
}
5
  • You will need to update the value in your controller and rerender that section once the Contact is selected. On load the reference is null to the shiptoemail is null and it does not update automatically as you have found
    – Eric
    Apr 24 '17 at 14:29
  • @Eric Yes, but how? What is the standard way of doing this in Visualforce / Apex?
    – Alexandru
    Apr 24 '17 at 14:33
  • Use an actionSupport that executes a method in your apex controller to update the values and then use the rerender property of the actionSupport to rerender the parent element. We would need to see the "ContactSelected" method in your controller as well as how you are instantiating the contact in your controller
    – Eric
    Apr 24 '17 at 14:35
  • @Eric I have added my controller method. I am not sure how it should be instantiated in this case, so for sure I must be missing something. I just assumed that (from my web development experience) it would automatically present the correct data from when the contact is selected. But if that is not the case, could you show me how to instantiate it please?
    – Alexandru
    Apr 24 '17 at 14:35
  • your method does nothing so I am not sure what you expected. See me answer for a working example.
    – Eric
    Apr 24 '17 at 14:47
1

Here is what I quickly worked up:

Page

<apex:page standardController="Contact" extensions="mytestClass">
    <apex:form id="theFrm">
    <apex:inputField id="contact" value="{!Contact.AccountId}">
        <apex:actionSupport event="onchange" action="{!accountSelected}" rerender="theFrm"/>
    </apex:inputField>
    <apex:inputText id="shipToEmail" value="{!Contact.Account.Name}" disabled="true" />
    </apex:form>
</apex:page>

Controller

public class myTestClass{

    private ApexPages.StandardController internalCon;

    public mytestClass(ApexPages.StandardController con){
        internalCon = con;
    }

    public void accountSelected(){
        Contact c = (Contact)internalCon.getRecord();
        Account a = [Select Id, Name From Account where Id = :c.AccountId];

        c.Account = a;

    }


}

Basically, assign the standard controller to a private property so it can be manipulated later.

Then on selection of the Account, query the account and set the contact.Account to the account.

No error handling included and there may be better ways but it works

Using your object

Page

<apex:page standardController="Software_Activation__c" extensions="mytestClass">
    <apex:form id="theFrm">
    <apex:inputField id="contact" value="{!Software_Activation__c.Contact__c}">
        <apex:actionSupport event="onchange" action="{!contactSelected}" rerender="theFrm"/>
    </apex:inputField>
    <apex:inputText id="shipToEmail" value="{!Software_Activation__c.Contact__r.Email}" disabled="true" />
    </apex:form>
</apex:page>

Controller

public class myTestClass{

    private ApexPages.StandardController internalCon;

    public mytestClass(ApexPages.StandardController con){
        internalCon = con;
    }

    public void contactSelected(){
        Software_Activation__c sa = (Software_Activation__c)internalCon.getRecord();
        Contact c = [Select Id, Email From Contact where Id = :sa.Contact__c];

        sa.Contact__r = c;

    }


}
8
  • You're exampling a different use case applied to a standard controller, but my controller is an extension which makes things a lot trickier. Now I'm stuck as how to get the contact ID.
    – Alexandru
    Apr 24 '17 at 15:39
  • @Alexandru - I do not have the objects you do. My example uses a controller extension as well. The concepts are the same regardless of object. You have a standard controller as well....
    – Eric
    Apr 24 '17 at 15:41
  • Erm, sorry, I meant, I am using a custom object with a lookup to a contact and not a standard object like Contact, and that's what makes it tricky.
    – Alexandru
    Apr 24 '17 at 15:42
  • @Alexandru - Just replace the object and relationship names. Do the above example in isolation from the rest of your code, get it to work with your custom object, then go from there when you understand what it is doing
    – Eric
    Apr 24 '17 at 15:43
  • 1
    @Alexandru - Great. Important to note then that you should notice how the StandardController is all "by reference" in a way. Thats why we set it on instantiation so we can modify it within the class as needed. Removes the need to created other properties etc to work on and keeps things simple.
    – Eric
    Apr 24 '17 at 15:56

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.