function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tsalbtsalb 

Simple Controller - update related object field?

How would I update a lookup's field from this controller? The value is saved, but no DML is performed because I think my save method is incorrect.

 

Vendor_Name__c on Service_Request__c is a lookup to Contact.

 

public class VendorInvoiceController {

	private final Service_Request__c srf;
	public Contact vendor;
	
	public string orderId = ApexPages.currentPage().getParameters().get('orderId');
	public string vendorId = ApexPages.currentPage().getParameters().get('vendorId');
	public string rfpId = ApexPages.currentPage().getParameters().get('rfpId');

	//Constructor
	public VendorInvoiceController(){
		srf = [SELECT
			   Vendor_Name__r.Name, Vendor_Name__r.Id, Vendor_Name__r.MailingStreet,                                   Vendor_Name__r.Payment_Info__c,
			   Vendor_Invoiced_Received__c
			   FROM Service_Request__c WHERE Id =: orderId];
	}
	
	public Service_Request__c getSrf() {
		return srf;
	}
    
	public pageReference save(){
		vendor = [select Id from Contact where Id =: vendorId];
		//vendor.payment_info__c = 'test';
		update vendor;
		return null;
	}
	
	public pageReference updateMe() {
		update srf;
		return null;
	}

}

 

Page

<apex:page controller="VendorInvoiceController" showHeader="false" sidebar="false">

  <apex:form >
    <apex:pageMessages />
    <apex:pageBlock tabstyle="Service_Request__c">
    
      <apex:pageBlockSection title="Payment Information" id="test">
        <apex:outputfield value="{!srf.Vendor_Name__r.MailingStreet}"/>
        <apex:outputfield value="{!srf.Vendor_Name__r.Payment_Info__c}"/>
      </apex:pageBlockSection>
      
      <apex:pageBlockSection title="Update Me">
        <apex:inputField value="{!srf.Vendor_Name__r.Payment_Info__c}"/> //re-renders but no DML?
        <apex:inputField value="{!srf.Vendor_Invoiced_Received__c}"/>
      </apex:pageBlockSection>
      
      <apex:commandButton value="Update Address" action="{!save}" rerender="test"/>
      <apex:commandbutton value="Update Me" action="{!updateMe}"/>
      
    </apex:pageBlock>
  </apex:form>

</apex:page>

 

dmchengdmcheng
you have not defined a public set method for srf.

Instead of 

public Service_Request__c getSrf() {
return srf;
}

I would use
public Service_Request__c srf ( get; set; }
tsalbtsalb

Then for my vendor update - i would go from:

 

      <apex:pageBlockSection title="Update Me">
        <apex:inputField value="{!srf.Vendor_Name__r.Payment_Info__c}"/> //re-renders but no DML?
        <apex:inputField value="{!srf.Vendor_Invoiced_Received__c}"/>
      </apex:pageBlockSection>

 to

 

      <apex:pageBlockSection title="Update Me">
        <apex:inputField value="{!vendor.Payment_Info__c}"/> //re-renders but no DML?
        <apex:inputField value="{!srf.Vendor_Invoiced_Received__c}"/>
      </apex:pageBlockSection>

and have

 

public Contact vendor {get;set;}?

 

So, i wouldn't want to do srf.vendor_name__r.field then?

dmchengdmcheng

No just replace that code block  that I mentioned and everything else should work.  Using the default getter/setter methods is sufficient for what you need.

tsalbtsalb

No, that doesn't seem to work. It's updated in the visualforce page - but the object (Related contact) is not updated.

 

the update call in my save button calls to update vendor; but is that kind of operation a problem?