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
RustyboyRustyboy 

Cannot invoke new detail page from Master-detail VisualForce page

I have a Visualforce page that allows users to view the Master-detail in one UI. The page uses the Master StandardController with an Extension which allows updates/delete to the detail records.

 

Try as I might, I cannot get the "Add new Detail" button to invoke the Edit page for a new detail item (see Apex code excerpt below). Whatever I seem to do, it always invokes the "edit" of the Master record.

 

    // Invoked when user presses "newDetail" button from VF page

    public PageReference newDetail()
    {
        Detail__c newDetail = new Detail__c(Master__c=getMaster().ID);
        PageReference newDetailPage = new ApexPages.StandardController(newDetail).edit();
        newDetailPage.setRedirect(true);
        Return newDetailPage;
    }

 

NB: I am trying to pre-populate the master record's ID -- I want this to work as if the user pressed the "New" button on the standard Salesforce related list page

Best Answer chosen by Admin (Salesforce Developers) 
GSBassoGSBasso

Sorry - my mistake.

 

Returning to the detail page after a Save is standard behaviour.

 

The retURL parameter has no bearing on the result of the Save action.

 

All Answers

GSBassoGSBasso

I would suggest you instantiate a PageReference object and return that.

 

Basically the PageReference should duplicate the url (including the query string) that results when you click on the standard New button from the related list of the Master detail page.

 

When you look at the query string you should see two parameters prefixed with CF. The string of characters that follows CF is the Salesforce Id of the Master__c field.

 

Unfortunately this id cannot be obtained programmatically in Apex. You can obtain it from the url that results when you click the New button (as previously stated) or from the url when you view the detail page of the Master__c field itself.

 

In essence, you must hard-code this field id. The good news is that the Id will be the same across all instances for a given org (i.e. the sandbox value will be the same as in production).

 

Your resulting code should look something like the following:

 

PageReference pr = new PageReference('/' + Detail__c.SObjectType.getDescribe().getKeyPrefix() + '/e');
String cfId = '...'; // 15 character id of the Detail__c.Master__c field - constant across prod and sandbox instances
pr.getParameters().put('CF' + cfId, getMaster().Name); // ensures Name appears in lookup field
pr.getParameters().put('CF' + cfId + '_lkid', getMaster().ID);
pr.getParameters().put('retURL', '...'); // ensures user is returned to your VF page after cancelling 
return pr;

 

Note that it is good practice to also specify the retURL in the query string so that the user is returned to the previous page after clicking Cancel.

 

Hope this helps.

RustyboyRustyboy

Thanks, this works fine after I manipulated the return URL.

 

Just one minor point on the return URL. It worked perfectly for CANCEL but not for SAVE which returned me to back to the view of the standard detail page.

 

Any ideas?

GSBassoGSBasso

Sorry - my mistake.

 

Returning to the detail page after a Save is standard behaviour.

 

The retURL parameter has no bearing on the result of the Save action.

 

This was selected as the best answer
RustyboyRustyboy

Great - thanks for the help