Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Determine Record's Object Type w/ Javascript or jQ...
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Determine Record's Object Type w/ Javascript or jQuery
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-13-2012 10:31 PM
Can someone suggest a way to retrive the object type of a current record using JS or jQuery only?
Twitter: www.twitter.com/kirkevonphilly
LinkedIn: http://www.linkedin.com/in/kirksteffke
Re: Determine Record's Object Type w/ Javascript or jQuery
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 08:46 AM
You can call sforce.connection.getGlobalDescribe(), then iterate through the results, calling getDescribe() on each object, checking getKeyPrefix() against the first three characters of the ID value you're testing for. Use caching for performance, if using multiple ID values.
~ sfdcfox ~
I am a sandwich. That is all.
Re: Determine Record's Object Type w/ Javascript or jQuery
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 08:48 AM
If this is going to be in Visualforce, you could also use a @RemoteAction function, passing the ID value as a parameter, calling getSObjectType() on that in Apex Code, and returning the result back to the page. Since key prefixes are dynamic across orgs (especially related to custom objects), you need to use either describe information and/or Apex Code.
~ sfdcfox ~
I am a sandwich. That is all.
Re: Determine Record's Object Type w/ Javascript or jQuery
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 07:23 PM
Thanks sfdcfox!
Let me give this a shot.
Twitter: www.twitter.com/kirkevonphilly
LinkedIn: http://www.linkedin.com/in/kirksteffke
Re: Determine Record's Object Type w/ Javascript or jQuery
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-14-2012 11:16 PM
Works great!
Is there a better way to grab the current record's ID than this?
function getRecID(){
return String(window.location.pathname.match(/\w{15}|\.co m\/\w{18}/));
}
Twitter: www.twitter.com/kirkevonphilly
LinkedIn: http://www.linkedin.com/in/kirksteffke
Re: Determine Record's Object Type w/ Javascript or jQuery
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
12-15-2012 03:13 PM
That's basically how I've done it in the past (when I didn't have access to Visualforce, for example). But, you should be aware that there may be conditions where the ID would be in a different location (e.g. in a Visualforce page, it will usually be a URL parameter: "id=<recordId>"). I would probably instead do this, though:
window.location.pathname.match(/\w{15}\w{3}?/)
The last 3 characters will (usually) only appear when a redirection has occurred from a Visualforce page (since the API always uses the 18 character ID values); you can safely ignore these three extra characters. Note that some Visualforce pages might lose the record ID after an AJAX refresh.
~ sfdcfox ~
I am a sandwich. That is all.

