I'm implementing a custom UI for Salesforce using Visualforce and Remote Actions. When I'm displaying a specific record, I look for all visible fields and display it on the screen.

The code in the Apex Remote Action is something like this:

String queryString = 'SELECT ' + String.join(fieldNames, ',') + ' FROM ' + dr.getName() +' WHERE Id = :recordId FOR VIEW';

The problem is that not all objects accept FOR VIEW in the SOQL query. Sometimes, the following error is displayed:

Visualforce Remoting Exception: FOR VIEW/REFERENCE can only be used on an object where viewing is tracked

Doing a little bit of research, I've found out that if the object does not have any Tab, you can't use FOR VIEW. How do I know if the object has a Tab or accepts FOR VIEW? Is calling Schema.describeTabs() the only solution?

1 Answer 1

  1. You could use the Describe Metadata API call as you mention. This may be additional overhead to your app though and cost you an additional API call which is wasteful.

  2. You could do a try/catch block in your code. Catch the exception and re-query without the FOR VIEW. This would be more efficient than #1 since you only make a second API call when necessary.

Note: You only need to add the FOR VIEW clause if you want to update the recently viewed related lists in the SFDC UI (Sidebar, Tabs, etc). If you don't care about that then you don't even need to do the FOR VIEW clause.

  • Thanks @CoryCowgill for your answer. I need to use FOR VIEW, since I'm building a completely new UI for Salesforce and I want to record to be marked as seen on the Recent Items. At the end I chose the option of using Describe Metadata API. Salesforce got rid of the governor limits and it's safe to use in an Apex Controller. I check if there is any Tab created for this object and use FOR VIEW only if there is any.
    – Ricard C
    Commented Jan 29, 2015 at 9:24
  • Cool. I was thinking this was from context of a remote application (like a hybrid-mobile app using REST API). You're right you shouldn't hit that describe limit in Apex now on latest versions. Commented Jan 29, 2015 at 15:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .