You need to sign in to do that
Don't have an account?
geeljire
How to update related object fields?
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; } }
Thank you.
In your code you are not filling new information in to child objects, you have to do it explicitely like below..
All Answers
In your code you are not filling new information in to child objects, you have to do it explicitely like below..
Thank you immensely Shiv.
A few minutes before I read your answer, another solution occurred to me which also works perfectly: adding the following line to saveRecord():
update entity.LookupField__r
Do you see any potential weaknesses with this approach?