[SalesForce] How to pass parameter in the new button to VF page in related list section

I have made a VF page and redirected the new button functionality into that VF page. The related SObject is List__c and it has a parent object called Folder__c. And in folder__c page layout I have the list__c related lists. And there is a new list button there.

By clicking that new list button, I realize the page is redirecting to this URL:

https://c.ap2.visual.force.com/apex/Choose_List_Type?CF00N2800000CopEM=Real+Estate&CF00N2800000CopEM_lkid=a032800000CVqB6&scontrolCaching=1&retURL=%2Fa032800000CVqB6&sfdc.override=1

In this, CF00N2800000CopEM_lkid seems to be the id of the folder. However, it doesn't seem to be the parameter name is something I can rely on. Seems to me it can change once the package is deployed into another org. So how can I retrieve the parent Id here?

Best Answer

Visualforce interprets fields like into the underlying SObject. That means that you can reliably determine the parent record like this:

public MyController(ApexPages.StandardController ctrl) {
    MySObject__c record = (MySObject__c)ctrl.getRecord();
    Id parentId = record.ParentField__c;
    // Do something with parentId
}

You should never rely on ApexPages.currentPage().getParameters() for values like "Id" or a specific field name, because they could theoretically change at any time. Always use supported methods, like StandardController.getRecord(), to get fields that are passed in to the Visualforce page via the system.

Related Topic