4

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?

6

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.

  • Won't this code throw errors without addFields? – Adrian Larson Jul 18 '16 at 1:28
  • @AdrianLarson Won't be an issue if you reference them in the page – Lance Shi Jul 18 '16 at 1:37
  • @AdrianLarson You'd only crash if (a) Id was provided to an existing record, and (b) the field was not referenced in the page. For new records, no query is generated, so this code works fine either way. For existing records, the field needs to be bound to the page if the page can be accessed with or without the parameter. – sfdcfox Jul 18 '16 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.