2

Sorry for the lengthy title. But I want to make sure that I was not able to solve my problem with the standard answers to questions like this.

I have a CustomObject_c that has many record types and a Parent_c object where a user should be able to add record of this CustomObject__c. But instead of showing the regular "New" button with the subsequent record type selection page I want multiple New buttons for each Record Type.

This first part is easy as in my newRecordOfTpeX() action method I can just add the record type as part of the URL. But I couldn't find a single word on how to easily prepopulate the Parent__c relationship.

I analysed the URLs that the URLFOR method created an it uses field IDs as parameter keys. How - without using the Metadata API from APEX - should I ever be able to find out the Ids of my relationship fields?!

1

Even metadata API won't help you in finding field Ids ;)

For custom fields (like a lookup to your parent object) they're just Ids you see in the URL when you navigate in setup to the field definition. For standard fields - it's easiest to just use Firebug or similar tool that lets you inspect HTML and learn name attributes of fields on standard page layouts. (this works on custom fields too, just sounds like bit more work when the Id is sitting right there in the URL...)

There's excellent blog post by Ray Dehler to get you started. Another useful link lists all standard fields.


Now, whether you should do it... Field Id will be different when you deploy the field from sandbox to prod. It will get stable once the field got deployed to production and you refresh all sandboxes from prod (like record types or reports if you've ever noticed that). If the field is already there - you could hardcode it and the URLFOR-like solution might be really a way to go ;) It's a bit fragile though and forget about it if you'll ever plan to package your custom buttons... You could store it in some custom setting I guess?

A more sane approach would be to create a VF page & controller extension and then make several buttons that link to it. Something like /apex/newChildObjectPage?parent=001...&recordTypeName=rec1 will be more readable later in maintenance. Painful to have to recreate the new/edit page from scratch though...

  • The custom new page solution is not going to work for me as my page is part of a managed extension package and the New buttons are for an object in the managed base package. I cannot overwrite the New page for this object. – Robert Sösemann Mar 4 '13 at 15:05
  • You can remove standard "New" button from the related list (on parent's page layout) and use your custom button. Not exactly an override but works ;) Standard "new" would be still accessible from tab for example (if child objects have their own tab) but that's not your problem anymore - somebody who uses tab to make them kind of agrees that there will be no context from which the parent should be magically selected. I don't see an elegant way if it's an extension... Instruct your end users / clients how to obtain the field Id & create the buttons themselves? – eyescream Mar 4 '13 at 15:21

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.