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
jeremiah_smalljeremiah_small 

Overriding New

Hello everyone,

I have a custom object called Stacked Rebate Group (SRG), which is always created from the context of a custom object called Rebate, via the New button on the related list. There are no required fields in SRG. Two lookup fields are populated by the before trigger when the object is saved.

The before trigger automatically duplicates the Rebate two times, and associates the two new copies with the two Lookup relationship for the new Stacked Rebate Group, making a Stacked Rebate Group with three copies of the same Rebate each associated by one of three Lookup fields.

What happens using the default New behavior is the user sees the Edit layout, and the fields that should relate to the two new copies are not filled in yet. Clicking save, without filling in either of the two new records fires the before trigger, and the copies are created, and the Lookup fields are filled in, no problem.

See screen shots below.

The trouble is, I would like to make a simple VF page to override the New behavior, automatically attempting to "click Save" for the user, but I can't seem to get it to work the same way I do with the default behavior, even though I think it should be doing the same thing. Here's the simplest form of what I'm trying in the VF.

Code:
<apex:page standardController="SMART_Stacked_Rebate_Group__c"
action="{!save}">
    <apex:messages />
    <apex:Detail subject="{!SMART_Stacked_Rebate_Group__c.id}"/>
</apex:page>

What happens when I use this page is my trigger show my custom error that says "SRG must be created from and ongoing offer," which only happens if there's no Rebate to duplicate (like if someone were allowed to just click New on the SRG object directly). This seems to point to the fact that the standard controller doesn't get the hand-off in VF the same way that it does when I enable the default New behavior. I've tried removing the Save action and  adding...

Code:
<p>SMART_Stacked_Rebate_Group__c.id: {!SMART_Stacked_Rebate_Group__c.id}</p>

...and it certainly doesn't see the New SRG, but that makes sense until it's been saved. Since it's based on the standard controller, what else would I have to do in my VF to be able to access the handed off Rebate record that is to be duplicated by the trigger.

As I mentioned, the attached pictures show what happens using the default New behavior. First the user is presented with a blank form, excepting the Ongoing Offer Rebate. Then by simply clicking Save, the trigger fills in the other two. I'd like to programmatically click save for them. Can anyone offer any suggestions here?

Jeremiah


Pictures of the default "New" behavior:



Then just click save, and this is where it ends up. I'd like to be able to skip the above step and go directly to the below state on New.


jeremiah_smalljeremiah_small
Solution:

Code:
<apex:page standardController="SMART_Stacked_Rebate_Group__c" extensions="SMART_New_Stacked_Rebate_Group" action="{!save}">
    <apex:form >
        <apex:inputField value="{!theSRG.R3_Ongoing_Offer_ID__c}"/>
    </apex:form>
    <apex:messages />
    <apex:Detail subject="{!SMART_Stacked_Rebate_Group__c.id}"/>
</apex:page>

 using a controller extension like this:

Code:
public class SMART_New_Stacked_Rebate_Group {

// This class is the VF controller extension for the page of the same name. jss

private SMART_Stacked_Rebate_Group__c theSRG;

public SMART_Stacked_Rebate_Group__c getTheSRG(){
return theSRG;
}

public SMART_New_Stacked_Rebate_Group(ApexPages.StandardController stdController){
theSRG = (SMART_Stacked_Rebate_Group__c) stdController.getRecord();
theSRG.R3_Ongoing_Offer_ID__c = ApexPages.currentPage().getParameters().get('sourceRebateId');
}

}

 
And accessed via a Custom List Button on the child object like this:

Code:
Label: New Stacked Rebate Group With This Rebate As Ongoing Offer
Display Type: List Button
Content Source: URL
Button URL: /apex/SMART_New_Stacked_Rebate_Group?sourceRebateId={!SMART_Rebate__c.Id}

 Then by editing the Related list in the Parent object, I deselected the default New button and selected my custom New button. That did it.

The problem was populating the foreign key field in the uncommitted object. By passing the value via the url, and using a simple controller extension, it works great.

HTH

Jeremiah



Message Edited by jeremiah_small on 11-12-2008 03:26 PM