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
Allan NolandAllan Noland 

How can you access the id of a parent record on a visual force page so you can create a new instance of the child record

Hello,

I am pretty green when it comes to programming in general so this is probably simple for the experienced folks out there, but this is vexing me quite severly.  I am trying to determine by page location when a link is clicked what row of data it refers to and more specifically what opportunity id. 

There are two objects in play here: 
Opportunity: Standard object we are all familiar with.
Revenue Projection: This is a child of the opportunity object and it is custom object in master detail relationship

I am using the standard list controller for the Opportunity object with an extension controller to make this page do what i want.

My issue is with the add new button.  How do i feed the id of the opportunity into my controller extension so i can create a new revenue projection tied to that opportunity and then refresh the page so that when they click add new it actually adds new.  

Main Objective: get possesion of the opportunity id in question so i can use it in the code.

Here is the visualforce markup
 
<apex:page Standardcontroller="Opportunity" recordSetVar="Opp" standardStylesheets="True" showHeader="false" Extensions="EditRevProjectionController"    >
    <apex:Form >
        <apex:pageBlock title="Revenue List" mode="Edit">
        <apex:pageBlockButtons location="both">
            <apex:commandButton action="{!previous}" value="Previous"/>
            <apex:commandButton action="{!quickSave}" value="Save"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
            <apex:commandButton action="{!next}" value="Next"/>
        </apex:pageBlockButtons> 
        <apex:selectList value="{!filterid}" size="1">
          <apex:selectOptions value="{!listviewoptions}"/>
      </apex:selectList>
      <apex:commandButton action="{!first}" value="Apply"/>    
            <apex:pageBlockTable value="{!Opp}" var="op" width="100%">
              <!--<apex:Column width="100 (px)" value="{!op.Ownerid}" headerValue="Owner"></apex:Column>-->
              <apex:Column value="{!op.id}"></apex:Column>
              <apex:Column width="200 (px)" value="{!op.Name}" headerValue="Opportunity"></apex:Column>
              <apex:Column headerValue="Stage"><apex:inputfield value="{!op.StageName}" /><Apex:commandLink action="{!AddNewProjection}" Value="Add New"/></apex:Column>
            <apex:column >
            <apex:pageBlockTable value="{!Op.RevenueProjections__r}" var="re">
              <apex:Column width="50(px)"  headerValue="Year"><apex:inputfield value="{!re.Year__c}" /></apex:Column>
              <apex:Column width="50(px)"  headerValue="January"><apex:inputfield value="{!re.January__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="February"><apex:inputfield value="{!re.February__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="March"><apex:inputfield value="{!re.March__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="April"><apex:inputfield value="{!re.April__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="May"><apex:inputfield value="{!re.May__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="June"><apex:inputfield value="{!re.June__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="July"><apex:inputfield value="{!re.July__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="August"><apex:inputfield value="{!re.August__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="Setember"><apex:inputfield value="{!re.September__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="October"><apex:inputfield value="{!re.October__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="November"><apex:inputfield value="{!re.November__c}"/></apex:Column>
              <apex:Column width="50(px)"  headerValue="December"><apex:inputfield value="{!re.December__c}"/></apex:Column>
          </apex:pageBlockTable>           
          </apex:column>
        </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:Form>
</apex:page>
As you can see from the mark up each row in the pageblock table has an add new link which references a method of the controller extension. 

Here is the code for the extension with the opportunity id hard coded i want to make that dynamic
 
public class EditRevProjectionController {

    public EditRevProjectionController(ApexPages.StandardSetController controller) {}



        Public PageReference AddNewProjection(){
        revenueprojection__c projection = new revenueprojection__c();
        projection.April__c = 0;
        projection.August__c = 0;
        projection.December__c = 0;
        projection.February__c = 0;
        projection.January__c = 0;
        projection.July__c = 0;
        projection.June__c = 0;
        projection.March__c = 0;
        projection.November__c = 0;
        projection.May__c = 0;
        projection.November__c = 0;
        projection.October__c = 0;
        projection.September__c = 0;
        projection.Year__c = '2015';
        projection.Opportunity__c = '006M000000CsqMI';
        insert projection;
        PageReference editpage = New PageReference('https://aviacode--partialsan--c.cs7.visual.force.com/'+projection.id);
        Return editpage;        
    }

I have looked at get element by id and seen references to the $component variable but,  I am so very green it doesn't make a lot of sense to me.  Can somebody give some guidance on how best to go about this?

I have added bold and underline to emphasize key parts of the code.

Thanks,
Allan



 
William TranWilliam Tran
Try this,

Thx
public class EditRevProjectionController {

private final Opportunity opp;

    public EditRevProjectionController(ApexPages.StandardSetController controller) {

this.opp = (Opportunity)stdController.getRecord();
}



        Public PageReference AddNewProjection(){
        revenueprojection__c projection = new revenueprojection__c();
        projection.April__c = 0;
        projection.August__c = 0;
        projection.December__c = 0;
        projection.February__c = 0;
        projection.January__c = 0;
        projection.July__c = 0;
        projection.June__c = 0;
        projection.March__c = 0;
        projection.November__c = 0;
        projection.May__c = 0;
        projection.November__c = 0;
        projection.October__c = 0;
        projection.September__c = 0;
        projection.Year__c = '2015';

        <u><b>projection.Opportunity__c = '006M000000CsqMI';</b></u>


        projection.Opportunity__c = opp.id;

        insert projection;
        PageReference editpage = New PageReference('https://aviacode--partialsan--c.cs7.visual.force.com/'+projection.id);
        Return editpage;        
    }

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
Allan NolandAllan Noland
Let me add a shot of the page maybe that will help clarify the question.  I think you are thinking that there is only one opp id involved in the page.User-added image
you can see the add new link it fires the method defined in the extension.  I want the add new link to create a new projection object with opportunity id defined as the corresponding opportunity id

I have circled the link and drawn a line to the opp id i want it to use.  it should populate with a different id depending on which instance of the add new link is clicked.

I had to correct the code above so it reads (ApexPages.StandardSetController stdcontroller) { hopefully that doesn't break what you were trying to do and was just a typo.

Also william if you have time can you walk me through the controller constructor.  I have tried to find some good reading material on the controller class so i can understand its members better but i am struggling.  Any direction you have would be greatly appreciated.

Why is the variable at the begining privately set and why is it final.  As i recall final means that no matter what happens it stays the same during the entire execution context.  Doesn't it need to change in order to accomodate the user clicking two or more add new links while they are using the page.  (oh that reminds me my intention is not to have the link go to a new page but rather it is going to refesh the current page) i set it to go somewhere else so i could keep a closer eye on what it was doing using the url.  Also in this way i could see everything the action was setting as field values.

By the way with the code you provided it says i am not setting the opportunity id on the new record which is required because of the master detail relationship.  Since i don't fully understand what that controller contructor is doing i am not sure why opp.id is coming back not set.

Thanks for all your help he is the re written code.
 
public class EditRevProjectionController {

    private final opportunity opp;
    public EditRevProjectionController(ApexPages.StandardSetController stdcontroller) {
        this.opp = (Opportunity)stdController.getRecord();
    }



        Public PageReference AddNewProjection(){
        revenueprojection__c projection = new revenueprojection__c();
        projection.April__c = 0;
        projection.August__c = 0;
        projection.December__c = 0;
        projection.February__c = 0;
        projection.January__c = 0;
        projection.July__c = 0;
        projection.June__c = 0;
        projection.March__c = 0;
        projection.November__c = 0;
        projection.May__c = 0;
        projection.November__c = 0;
        projection.October__c = 0;
        projection.September__c = 0;
        projection.Year__c = '2015';
        projection.Opportunity__c = opp.id;
        insert projection;
        PageReference editpage = New PageReference('https://aviacode--partialsan--c.cs7.visual.force.com/'+projection.id);
        Return editpage;        
    }