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
CraigHCraigH 

Programmatic lookup of custom object page name & field ids for pre-population

I am using the method documented in various posts to pre-populate default values in a standard page for a custom object.  i.e. I when the user wants to create a new record for the object, I generate this URL in Apex:

 

https://na7.salesforce.com/a0G/e?00NA0000003UhJ8=2010

 

This will open up the standard new screen for a custom object (page name = aOG) and in a year field put in 2010 (field id = 00NA0000003UhJ8)

 

This works fine, however, if I move the code to a new org, the page name and field ID will be different.

 

Does anybody know if there is a way to dynamically figure out the page name for a custom object and a field id for a field so I can programmatically figure out the appropriate URL vs hard coding it)? 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You can describe all sobjects to determine the 3 character abbreviation for a custom object by name.

 

I don't think there's anything you can do for the field id.

 

We normally store this information in custom settings.

All Answers

bob_buzzardbob_buzzard

You can describe all sobjects to determine the 3 character abbreviation for a custom object by name.

 

I don't think there's anything you can do for the field id.

 

We normally store this information in custom settings.

This was selected as the best answer
sforce2009sforce2009
ahab1372ahab1372

at least the domain part you can omit,  /a0G/e?00NA0000003UhJ8=2010 should work, too

CraigHCraigH

For reference here is the code:

 

1) Lookup by object name in code:

 

 

Schema.DescribeSObjectResult describe = Schema.SObjectType.MGA_Client_file__c;
String pageName = describe.getKeyPrefix();

 

Schema.SObjectType.YourCustomObject__c.getKeyPrefix();

 

 

2) Lookup by object name as String:

 

Schema.getGlobalDescribe().get('YourCustomObject__c').getDescribe().getKeyPrefix())

 

Note - would want to harden this for production by doing null check if object name not found