4

I'm making a Visualforce page where a new Customer__c record is created. Customer__c has a lookup field (recordContact__r) which refers to another custom object: Contact__c.

While the main object record is created fine, the related object record isn't created with this code. What am I missing?

Visualforce

<apex:page showHeader="false" standardController="Customer__c" extensions="ExtendCustomers" sidebar="false" standardstylesheets="false">
    <apex:form >

        <div>
                <apex:commandButton action="{!saveRecord}" value="Save"/>
        </div>
        <div>
                <apex:inputField value="{!Customer__c.Name}" />
                <apex:inputField value="{!Customer__c.recordContact__r.Name}" />
                <apex:inputField value="{!Customer__c.recordContact__r.Address__c}" />
                <apex:inputField value="{!Customer__c.recordContact__r.City__c}" />
                <apex:inputField value="{!Customer__c.recordContact__r.State__c}" />
                <apex:inputField value="{!Customer__c.recordContact__r.Zip__c} " /> 
                <apex:inputField value="{!Customer__c.Email__c}" />
                <apex:inputField value="{!Customer__c.Phone__c}" />
        </div>
    </apex:form>
</apex:page>

Extension

public with sharing class ExtendCustomers{
    public Customer__c newCustomer = new Customer__c();

    public ExtendCustomers(ApexPages.StandardController controller) {
        this.newCustomer = (Customer__c)controller.getRecord();
    }

    public PageReference saveRecord() {
        insert newCustomer;
    }
 }
3

Any DML operations only operate on the primary object, any modifications to parent objects are ignored (with some exceptions for specifying a parent relationship with a foreign id). You'll need to insert the Contact__c separately.

public void saveRecord() {
  upsert newCustomer.recordContact__r;
  upsert newCustomer;
}
|improve this answer|||||
  • Thanks a lot Ralph. I previously tried solutions along the same lines, but couldn't (and still can't) overcome a NullPointerException on insert recordContact. (System.NullPointerException: Attempt to de-reference a null object) – geeljire Mar 28 '13 at 12:37
  • just don't use parent relationships for data entry, only display, updated my answer to show a fully two object solution – Ralph Callaway Mar 28 '13 at 17:18
  • also looks like you're reference the wrong variables, the variable your page is acting on is the controller record, not newCustomer which is probably nulll – Ralph Callaway Mar 28 '13 at 17:20
  • Ralph, thank you again. I was finally able to get it to work. Although you certainly steered me in the right direction on this problem, it appears that you can in fact manipulate related objects using DML. In an edit page with the same objects, I can successfully update newCustomer.recordContact__r while referencing all related object fields like so: newCustomer.recordContact__r.Name – geeljire Mar 29 '13 at 22:48
  • hmm, that's news to me, could you post a gist (gist.github.com) with a simplified version of your code? I'd love to give that a try – Ralph Callaway Mar 30 '13 at 1:44

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.