2

I'm trying to update fields of a custom object along with fields of a related object. The fields of the primary object are updated without a problem, but none of the related object fields are updated. Notice that the saveRecord() method calls update on both objects. How can I accomplish this?

Visualforce

<apex:page showHeader="false" standardController="PrimaryObject__c" extensions="controlEntity" sidebar="false" standardstylesheets="false">

        <apex:form>
            <div>
                    <apex:inputField value="{!PrimaryObject__c.Name}" />

                    <apex:commandButton action="{!saveRecord}" value="Save"/>

                    <apex:inputField value="{!PrimaryObject__c.LookupField__r.Name}" />
                    <apex:inputField value="{!PrimaryObject__c.LookupField__r.Address__c}" />
                    <apex:inputField value="{!PrimaryObject__c.LookupField__r.City__c}" />
                    <apex:inputField value="{!PrimaryObject__c.LookupField__r.State_Province__c}" />
                    <apex:inputField value="{!PrimaryObject__c.LookupField__r.Zip_Postal_Code__c} " /> 
                    <apex:inputField value="{!PrimaryObject__c.Email_1__c}" />
                    <apex:inputField value="{!PrimaryObject__c.Email_2__c}" />
                    <apex:inputField value="{!PrimaryObject__c.Phone_1__c}" />
                    <apex:inputField value="{!PrimaryObject__c.Phone_2__c}" />
                    <apex:inputField value="{!PrimaryObject__c.Inactive__c}" />
            </div>
        </apex:form>

    </apex:page>

Extension class:

public with sharing class controlEntity {
    public PrimaryObject__c entity;
    public RelatedObject__c contact;

    public controlEntity(ApexPages.StandardController controller) {
        this.entity = (PrimaryObject__c)controller.getRecord();
        this.contact = [SELECT Name, Address__c, City__c, State_Province__c, Zip_Postal_Code__c FROM RelatedObject__c WHERE Customer_Vendor__c = :ApexPages.currentPage().getParameters().get('id')];
    }

    public PageReference saveRecord() {
        update entity;
        update contact;
        return null;
    }
 }

Clarification: PrimaryObject__c.LookupField__r is the related object.

4

Is the PrimaryObject__c.Lookupfield__r the related object? If so, then you don't need to query it directly in your controller but just let Salesforce get it for you.

The PrimaryObject has two pieces of information that can be used to get to its LookupField:

  1. The LookupField__c field: This is the ID of the related LookupField
  2. The LookupField__r relationship navigation: This is, essentially, a reference to the LookupField record which allows you to navigate to its fields (like you do in the Visualforce) and make changes to those fields. Conceptually, this is as if the PrimaryObject has-a LookupField instance which you can make changes to.

When you update the PrimaryObject in your controller it will wipe out all changes not directly on the PrimaryObject, which in this case includes changes made to the fields on the LookupField instance since they are not directly on the PrimaryObject.

If you first update the LookupField and then the entity, both of them should get updated in the DB.

update PrimaryObject__c.LookupField__r;
update PrimaryObject__c;
return null;
  • Sweet. This occurred to me 15 minutes before reading your answer. However, I reversed the update calls: I'm updating the primary object, then the related object via the relationship. Seems to be working OK. – geeljire Mar 26 '13 at 12:17

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.