+ Start a Discussion
MellowRenMellowRen 

Getter for Detail object not working (and button executing without being pressed)

Hi

I am putting together a system and something is going awefully wrong. May be I am too tired but I simply can't understand what is going on.

I have two Objects "Prod" and "Prod Detail" in a Master/Detail relationship. I have a Visualforce page "ProdEdit" for Prod with an extension. I want the page to have a button that takes a User (supplied) and adds a Prod Detail record. I have done similar tings to this before but this is not working. I have copied over a reduced set of code to my developer account and get the same behaviour.

The Page:
Hi
 
I am putting together a system and something is going awfully wrong. May be I am too tired but I simply can't understand what is going on.
 
I have two Objects "Prod" and "Prod Detail" in a Master/Detail relationship. I have a Visualforce page "ProdEdit" for Prod with an extension. I want the page to have a button that takes a User (supplied) and adds a Prod Detail record. I have done similar tings to this before but this is not working. I have copied over a reduced set of code to my developer account and get the same behavior.
<apex:page standardcontroller="Prod__c" extensions="Prod_Edit_Ext">
    <!-- The page -->
    <apex:form id="thePage">
        
    	<!-- In View Mode: Has Options Lists. -->
        	<apex:pageBlock title="Prod Detail" mode="maindetail">
                <apex:pageBlockSection title="Information" columns="2">
                    <apex:outputField value="{!Prod__c.Name}"/>
                    <apex:outputField value="{!Prod__c.Manufacturer__c}"/>
                    <apex:outputField value="{!Prod__c.OwnerId}" label="Prod Manager"/>
                </apex:pageBlockSection>
                <br/><br/>
            </apex:pageBlock>
         	<apex:pageBlock mode="edit">
                <apex:pageBlockSection columns="2" id="newPD">
                    <apex:inputField label="New Manager" value="{!newProdDetailWithUser.User__c}"/>
                    <apex:commandButton value="Add Prod Detail" action="{!addProdDetail}" immediate="true"/>
                </apex:pageBlockSection>
        </apex:pageBlock>                        
    </apex:form>
</apex:page>

The Extension
public class Prod_Edit_Ext {
    //Variables
    private ApexPages.StandardController stdProdCtrl; //for a link to the Controller class (that this is extending) so as to use its methods.
    private Prod__c  theProd;
        
    //Getters & Setters (Simple)
    public Prod_Detail__c newProdDetailWithUser {get; set;}
    
	//Constructor
    public Prod_Edit_Ext(ApexPages.StandardController controller) {
System.debug('zzz In Constructor');
        stdProdCtrl = controller;
        theProd = (Prod__c) stdProdCtrl.getRecord(); //use std controller to get record.

        //Initialise Prod Details
        newProdDetailWithUser = new Prod_Detail__c();
    }
    
    //Action Methods
    public void addProdDetail() {
        // This function runs when a user hits "add" button.
System.debug('zzz Start Add: '+newProdDetailWithUser);
       // if (newProdDetailWithUser.User__c != null) {
            newProdDetailWithUser.Prod__c = theProd.Id;
            newProdDetailWithUser.Name = 'Test: '+Datetime.now().format();
System.debug('zzz Ready to insert: '+newProdDetailWithUser);
System.debug('zzz User to insert: '+newProdDetailWithUser.User__c);
            INSERT newProdDetailWithUser;
System.debug('zzz Inserted: '+newProdDetailWithUser);
      //  }
        newProdDetailWithUser = new Prod_Detail__c();
    }
}

This is creating bizarre results:
  1. When I first load the page, a record is created even though the button is not yet pushed.
  2. After I select a User in the field, and press the button, a Prod Detail record is created but only with the Name (and the Prod link) but not with the User I just selected. That field is left blank. The debug statements show it is null before the Insert.
I am going stir-crazy. I have experimented with page block modes, full on getters and setters, etc but cannot get it to work. Would really appreciate if someone was able to tell me what I have done wrong.

Regards
MellowRen
MellowRenMellowRen
Sorry. I tried to fix some spelling mistakes before posting the question and somehow created the repeated paragraph. Now can’t edit. Need some sleep.