2

I'm building a VF page to update 2 objects at the same time. Using Stack Force I was able to figure out how to build a VF page with an extension and now need to figure out how to get the parent record to show on the VF page.

Here is what I have.

Apex Class

public with sharing class SaveExtension
{
    ApexPages.StandardController sc;
    public AVTRRT__Job__c objBJob {get; set;}

    public SaveExtension(ApexPages.StandardController sc)
    {
        this.sc = sc;
        objBJob = new AVTRRT__Job__c();
    }

    public ApexPages.PageReference SaveBoth()
    {
        insert objBJob;
        return sc.Save();
    }
}

Visualforce Page

<apex:page StandardController="AVTRRT__Job_Applicant__c" extensions="SaveExtension">
  <apex:form >
  <apex:pageblock mode="edit">
       <apex:inputField value="{!AVTRRT__Job_Applicant__c.AVTRRT__Stage__c}" />
       <apex:inputField value="{!objBJob.Job_Type__c}" />
       <apex:CommandButton action="{!saveboth}" value="Save"/>
  </apex:pageblock>
</apex:form> 
</apex:page>

I call the vf page by passing the Child ID in the URL and when saved this record is updated.
https://c.cs10.visual.force.com/apex/Update_Job_and_Job_App_w_Extension?ID=a0DJ000000FlalG

The object in the extension AVTRRT__Job__c ObjBJob is the parent of the object in the standard controller. The key is I want to be able to allow the user to make changes to the parent record (Job Order) when they are making updates on the child (Applicants).

Any code examples are appreciated.

Thanks, Ed

3

Here is one way to query the data, where you need to change ... to be the comma-separated list of field API names that you want to edit:

public SaveExtension(ApexPages.StandardController sc)
{
    this.sc = sc;
    objBJob = [
            select ...
            from AVTRRT__Job__c
            where Id in (
                    select AVTRRT__Job__c
                    from AVTRRT__Job_Applicant__c
                    where Id = :sc.getId()
                    )];
}

See the Relationship Queries documentation to see the query patterns available when dealing with related objects and information about naming patterns.

12345

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.