+ Start a Discussion
geeljiregeeljire 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Shiv ShankarShiv Shankar

In your code you are not filling new information in to child objects, you have to do it explicitely like below..

public with sharing class controlEntity {
    public PrimaryObject__c entity;
    public RelatedObject__c contact;
	public public ApexPages.StandardController SC ; // standard controller instace
    
    public controlEntity(ApexPages.StandardController controller) {
		SC = controller; // assign standard controller reference
        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;
		
		PrimaryObject__c rec = (PrimaryObject__c) SC.getRecord();
		contact. Address__c = rec.LookupField__r.Address__c;
		contact.State_Province__c = rec.LookupField__r.State_Province__c;
				.
				.
				Do this for all fields
				.
				.
			
		
		
        update contact; // update contact
        return null;
    }
 }

 

 

 

All Answers

Shiv ShankarShiv Shankar

In your code you are not filling new information in to child objects, you have to do it explicitely like below..

public with sharing class controlEntity {
    public PrimaryObject__c entity;
    public RelatedObject__c contact;
	public public ApexPages.StandardController SC ; // standard controller instace
    
    public controlEntity(ApexPages.StandardController controller) {
		SC = controller; // assign standard controller reference
        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;
		
		PrimaryObject__c rec = (PrimaryObject__c) SC.getRecord();
		contact. Address__c = rec.LookupField__r.Address__c;
		contact.State_Province__c = rec.LookupField__r.State_Province__c;
				.
				.
				Do this for all fields
				.
				.
			
		
		
        update contact; // update contact
        return null;
    }
 }

 

 

 

This was selected as the best answer
geeljiregeeljire

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?