How to combine FOR VIEW and FOR REFERENCE?
Based on the documentation of the syntax:
SELECT ***fieldList*** [subquery][...]
[TYPEOF typeOfField whenExpression[...] elseExpression END][...]
FROM objectType[,...]
[USING SCOPE filterScope]
[WHERE conditionExpression]
[WITH [DATA CATEGORY] filteringExpression]
[GROUP BY {fieldGroupByList|ROLLUP (fieldSubtotalGroupByList)|CUBE (fieldSubtotalGroupByList)}
[HAVING havingConditionExpression] ]
[ORDER BY fieldOrderByList {ASC|DESC} [NULLS {FIRST|LAST}] ]
[LIMIT numberOfRowsToReturn]
[OFFSET numberOfRowsToSkip]
[FOR {VIEW | REFERENCE}[,...] ]
[ UPDATE {TRACKING|VIEWSTAT}[,...] ]
and the Typographical Conventions (including a relevant example that uses the same syntax)
[...] and [,...]
Square brackets containing an ellipsis indicate that the preceding element can be repeated up to the limit for the element. If a comma is also present, the repeated elements must be separated by commas. If the element is a list of choices grouped with curly braces, you can use items from the list in any order. For example, in the clause
UPDATE {TRACKING|VIEWSTAT}[,...]
, the[,...]
indicates that you can useTRACKING
,VIEWSTAT
, or both:
UPDATE TRACKING
UPDATE VIEWSTAT
UPDATE TRACKING, VIEWSTAT
This should be able to be executed as
FOR VIEW, REFERENCE
Unfortunately, this throws syntax errors in a v.39 org. Not sure about a v.40 org myself. Currently it looks like a few places have bad documentation around this, since the consideration to use them in conjunction is sprinkled throughout the docs for those optional clauses.
If I read the Typographical Conventions in This Document
| - The pipe character separates alternate elements. For example, in the clause UPDATE {TRACKING|VIEWSTAT}[,...], the | character indicates that you can use either TRACKING or VIEWSTAT after UPDATE.
And
[...] and [,...] - Square brackets containing an ellipsis indicate that the preceding element can be repeated up to the limit for the element. If a comma is also present, the repeated elements must be separated by commas. If the element is a list of choices grouped with curly braces, you can use items from the list in any order. For example, in the clause UPDATE {TRACKING|VIEWSTAT}[,...], the [,...] indicates that you can use TRACKING, VIEWSTAT, or both:
So, following query works for me for querying Knowledge Article using VIEWSTAT,TRACKING
with FOR REFERENCE
Valid
List<Vendor_Sheet__ka> lstKav = [SELECT Id,LastViewedDate
From Vendor_Sheet__ka WHERE Id = 'kA2q00000008UvV'
FOR REFERENCE UPDATE VIEWSTAT,TRACKING];
System.debug('LastViewedDate=' + lstKav[0].LastViewedDate);
That it also working for FOR VIEW
List<Vendor_Sheet__ka> lstKav = [SELECT Id,LastViewedDate
From Vendor_Sheet__ka
WHERE Id = 'kA2q00000008UvV'
FOR VIEW UPDATE VIEWSTAT,TRACKING];
System.debug('LastViewedDate=' + lstKav[0].LastViewedDate);
Invalid Scenario:
If I put wrong id (i.e. Knowledge article Id) to retrieve the Knowledge article version like this:
List<Vendor_Sheet__kav> lstKav = [SELECT Id From Vendor_Sheet__kav
WHERE Id='kA2q00000008UvV'
FOR VIEW UPDATE VIEWSTAT];
It shows me error message:
FOR VIEW/REFERENCE can only be used on an object where viewing is tracked
That's interesting !!!
So, accordingly to me based on error message, Salesforce supports either FOR VIEW
or FOR REFERENCE
and not both.
The pipeline between two words clearly applicable in case of FOR {VIEW | REFERENCE}
Also I am interpreting the pipe in ORDER BY fieldOrderByList {ASC|DESC}
where same field cannot be used for both ASC and DESC.
Invalid
List<Account> acct = [SELECT Id, Name FROM Account
Order By Name ASC, DESC LIMIT 2];
Valid
List<Account> acct = [SELECT Id, Name FROM Account
Order By Name ASC, Id DESC LIMIT 2];