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

Build apex page action with urlfor in controller

Hi Community,
I have created a new 'OpportunityOffer' object and I can only create a new OpportunityOffer object as a child object of an Opportunity.

So I want to override the New button of the OppertunityObject, because I have to set the recordtype automatically. If I have an Opportunity with type (custom field) X, than I have to set the recordtype X for the new OpportunityOffer.

I created the following:

<apex:page standardController="OpportunityOffer__c" extensions="ManualOfferRecordTypeSelectionExtension"
           action="{!URLFOR($Action.OpportunityOffer__c.New, null, [retURL=URLFOR($Action.Opportunity.Tab, $ObjectType.Opportunity), RecordType='{!recordType}'], true)}">

My Controller finds the right recordtype - but I think that the action does not wait for the result of my controller - the wrong recordtype is set. I tested to set a recordtype Id into the action, that worked - but not with a controller value.

Is there a way to create the action in the controller? I found nothing in the internet that help me.. 
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi MXG, 

you can develop the redirection into am method of ManualOfferRecordTypeSelectionExtension extension using pagereference. Thereafter you can also put record type id to that URL. I think it is not happening in your code as generation of  {!recordType} is happening later than the page getting loaded.

Thank for your answer, but I am new at apex and I don't know how to build the URLFOR function as a pagereference. Co you have a example?
Prosenjit Sarkar 7Prosenjit Sarkar 7
Can you please share the code of ManualOfferRecordTypeSelectionExtension class if possible ?
Hi Prosenjit, here is my Extension code:
public Id recordType { get; set; }
    // constructor
    public ManualOfferRecordTypeSelectionExtension(ApexPages.StandardController controller) {
        OpportunityOffer__c opportunityOffer = (OpportunityOffers__c)controller.getRecord();
        Id opportunityParentId = this.opportunityOffer.OpportunityRef__c;
        Opportunity opportunity = [SELECT Id, ProductDescription__c FROM Opportunity where Id=:this.opportunityParentId];
        RecordType recordType = [SELECT Id FROM RecordType WHERE Description=:opportunity.ProductDescription__c];
        this.recordType = recordType.Id;