Ordering and Limiting A Subquery In Salesforce SOQL - salesforce

I am attempting to retrieve the owner of a case, based on a partial match, where we choose the most recent case that matches the partial match.
This is the query I am attempting:
SELECT User.CustomField__c
FROM User
WHERE User.Id IN (
SELECT OwnerId
FROM Case
WHERE Case.CaseNumber LIKE '%1026'
ORDER BY Case.CreatedDate DESC LIMIT 1)
The following query works on its own, but doesn't seem happy as part of the subquery:
SELECT OwnerId
FROM Case
WHERE Case.CaseNumber LIKE '%1026'
ORDER BY Case.CreatedDate DESC LIMIT 1
Equally, if I drop the ORDER BY and LIMIT it works:
SELECT User.NVMContactWorld__NVM_Agent_Id__c
FROM User
WHERE User.Id IN (
SELECT OwnerId FROM Case
WHERE Case.CaseNumber LIKE '%1026')
Are order / limit queries not allowed in a SOQL subquery?
Just to clarify this issue, the scenario I am dealing with looks like this...
A Salesforce organisation can configure the "display format" for Case Numbers. If they select "4" digits, you get case numbers like:
0001
0125
1234
33456
It is possible to reconfigure your case numbers to get the following case numbers as well as the case numbers above...
000001
001234
033456
I didn't want people to be confused by the LIKE statement, the issue is that 001234 and 1234 are different cases, so if a customer supplies 1234 and I find two records, I want to start off assuming that they are the most recent case.
So either consider the LIKE statement or an IN statement that contains ('001234', '1234')

There is no documentation I could find that specifies that LIMIT and/or ORDER BY do not work with subqueries, but I ran into the same error you mentioned.
However, it may work to start at the Case object and look up to the User, similar to the Lookup Relationships and Outer Joins section in the SOQL documentation. I'm not sure if this would work for you, but it's something you may want to try.
Here's an example:
-- Edit --
SELECT OwnerId, Owner.CustomField__c
FROM Case WHERE
CaseNumber LIKE '%1026'
ORDER BY CreatedDate DESC
LIMIT 1
Turns out Custom Fields are not accessible because OwnerId is a polymorphic key referencing either Group or User. That means the above won't work, sorry.
To work-around this is very complicated. You would have to create a custom lookup field called "User Owner", or something. That would store a lookup reference to the User, if the Owner is a User (this can be checked by comparing the beginning of OwnerId to '005', the User ID prefix). That field would need to be populated using a after insert, after update Trigger. All values for this new field would need to be dataloaded for previously existing Cases. But, once you have this new "User Owner" field, you can access custom fields on User through SOQL, using it.

ORDER BY and LIMIT don't make sense in your subquery because you're not returning records from the subquery. Instead, the subquery just builds a list of IDs used to filter the main query.
If you use a subquery in a way that the subquery records are returned, these clauses are fine. For example, this works:
SELECT Name,
(SELECT FirstName, LastName FROM Contacts ORDER BY LastName LIMIT 10)
FROM Account

I think it's worth adding that with new features of SOQL that were not available when this question was first asked and answered, that the approach of querying from Case should now be viable (with access to custom fields).
In particular the TYPEOF feature provides access to type specific fields through polymorphic lookups: http://www.salesforce.com/us/developer/docs/soql_sosl/Content/sforce_api_calls_soql_select_typeof.htm
It is worth noting that this feature is still in Developer Preview.
SELECT
TYPEOF Owner
WHEN User THEN CustomField__c
END
FROM CASE

Did you try switching your query around to be something like this?
SELECT OwnerId, (select id from user)
FROM Case
WHERE Case.CaseNumber LIKE '%1026'
ORDER BY Case.CreatedDate DESC LIMIT 1

Related

System.sobjectexception: sobject row was retrieved via soql without querying the requested field: Asset.Product2

I have a problem on the query on my test class. I have put AssetId__r.Product2Id on the 1st query, and put Product2Id in the Asset query. but still the error persist. as the error says it needs Asset.Product2 which is an SObject , not a field and I am lost on how to fix this error. does anyone encounter this error before? please I need your help.
would be easier if you'd post the actual query.
You probably have something like that in your code:
String name = myObject.AssetId__r.Product2.Name;
but the query SELECT AssetId__r.Product2Id, ... FROM MyTable WHERE ...
So what you can change is put more product fields in the query. You can go up to 5 dots "up".
SELECT AssetId__r.Product2.Id,
AssetId__r.Product2.Name,
AssetId__r.Product2.ProductCode
FROM myObject
WHERE ...
It will work and then Apex that uses it can treat AssetId__r.Product2 as a normal Product2 object, as if it was queried separately. It'll have Id, Name, ProductCode fields set.
The query is also "safe". If AssetId__c is null or Product2Id is null - it'll still execute OK. You'll have to do null checks in Apex (or in query's WHERE clause)

Didn't understand relationship 'LinkedEntityId' in field path

So I am generally able to query the relationships but I do not have the access to this object in object manager, so what should be the query to access accountId from ContentDocumentLink object when I know LinkedEntity is a case.
My Code:
List<ContentDocumentLink> case_file_links=
[
SELECT Id,ContentDocumentId,LinkedEntityId.AccountId,ShareType
FROM ContentDocumentLink
WHERE Id IN: case_file_link_Ids
];
Error:
SELECT Id,ContentDocumentId,LinkedEntityId.AccountId,ShareType
^
ERROR at Row:2:Column:45
Didn't understand relationship 'LinkedEntityId' in field path. If you..
LinkedEntityId is a weird mutant lookup to many tables, any table that supports File upload. Official name is "polymorphic lookup".
Maybe you worked with Tasks before, they have similar fields. WhatId can point to Account, Case, Opportunity plus many custom objects. and WhoId points to User/Contact/Lead.
So it's a lookup to "something". It's not guaranteed it's to Cases so at compile time SF can't just blindly accept AccountId. What if the field is not there.
You might have to use TYPEOF in SOQL.
SELECT Id, ContentDocumentId, ShareType,
TYPEOF LinkedEntity WHEN Case THEN CaseNumber, Subject, AccountId END
FROM ContentDocumentLink
WHERE Id IN ('06A7a000001VafmEAC', '06A7a000001VafnEAC')
Developer Console is too stupid to display it properly but workbench should manage
In Apex you'll need an instanceof to check and cast the LinkedEntity to right sObject... or use the generic sObj.get('AccountId') and if it's not there it'll return null?
If this sounds like too much hassle - you can always split it into 2 queries.
Query just SELECT Id, ContentDocumentId, LinkedEntityId, ShareType FROM ContentDocumentLink
Loop through results, collect LinkedEntityId into a helper Set<Id>.
Map<Id, Case> cases = new Map<Id, Case>([SELECT Id, AccountId FROM Case WHERE Id IN :mySet]);

How do I select an absent column using a literal using SOQL?

Is it possible to do something like this?
SELECT
Id,
'1999-01-01T23:01:01Z' for Some_New_Date_Field__c,
SystemModstamp
FROM Opportunity
Some_New_Date_Field__c has not been added to Opportunity yet, but I would like to return a literal value for this for now to unblock some downstream engineering work.
It's not possible to include a literal return value for a column in SOQL, and you also cannot set values for arbitrary property names on returned sObjects.
In Aggregate SOQL, but not non-aggregate queries like this one, you can provide a name for a value that's used in the returned List<AggregateResult>; for example, you could do
SELECT OwnerId, MAX(CloseDate) largestCloseDate FROM Opportunity GROUP BY OwnerId
and get back AggregateResult objects that have a property largestCloseDate. I don't think that helps you here.

is SOQL capable of self joins mysql style?

Is it possible to do a self join with table aliasing? Lets's say I'm a book shop and I want to get all customers that purchased a book last week AND this week. In MySQL this would look somehow like this:
SELECT Account.id from Opportunity o1, Opportunity o2
WHERE o1.closeDate = Last_WEEK AND o2.closeDate = This_WEEK
AND o1.Account = o2.Account
What would be the equivalent in MySQL? It keeps puzzling me.
You can't compare one Opportunity Account Id with another in SOQL (o1.Account = o2.Account). If you try you will get the message:
Bind variables only allowed in Apex code
If instead you rework the SOQL to use a sub query you will get a different error.
SELECT AccountId from Opportunity o1
where o1.closeDate = LAST_WEEK
AND o1.Account in
(SELECT AccountId from Opportunity o2 WHERE o2.closeDate = THIS_WEEK)
Gives:
The inner and outer selects should not be on the same object type.
You will need to either:
perform two queries to get the required data. You could feed the resulting Account Ids of the first query into the second query as a where clause filter.
retrieve the Account Ids with either close date in one SOQL query and then build up the required sets with code.
Incidentally, the Salesforce StackExchange site is a great place to ask Salesforce specific questions.

How to query all task which are having relatedto(whatId) as Account or Contact

I am using an soql query to get tasks. My requirement is to get all tasks which are related account or contact object. Also few other fields inside Account or contact object whichever the object related. Is there a simple way instead of writing multiple queries.
Please provide more specific information next time. Generally speaking you can refer to a parent object by reference name followed with a dot. Here is an example
Select Account.Name, AccountId From Task Where Account.Name = 'John'
Here Account is the name of the reference (from task) and AccountId is the referencing field.
Your question is a little unclear. Are you looking for all TASKS related to any ACCOUNT or CONTACT or related to specific ACCOUNTS or CONTACTS?
If the former, try
SELECT Id, Subject,
FROM TASK
WHERE
What.Type = 'Account' OR
Who.Type = 'Contact'
if the latter, use the IN :list syntax already suggested by Moti. The What.Type is useful, as opposed to AccountId!=null, because it will not return TASKS associated with, for example, OPPORTUNITIES (if you want that behavior, use AccountId!=null and maybe drop the Who.Type if you associate all CONTACTS with OPPORTUNITIES, as it would be redundant).
In either case, your issue will be pulling specific data from CONTACT whos, as polymorphic fields only allow access to a limited number of fields. Can't seem to find that list right now. I don't believe old SOQL supports the kind of the syntax to do that in one query--that's why SOQL polymorphism made such a bang--though I could be wrong.
Now, if you have SOQL TYPEOF available to you, you should be able to do something more interesting like:
SELECT Id, Subject,
TYPEOF What
WHEN Account THEN AccountNumber
END,
TYPEOF Who
WHEN Contact THEN FirstName
END
FROM Task
WHERE
What.Type = 'Account' OR
Who.Type = 'Contact'
How about
select {column list] from task where parentid in (select id from account where ...) or parentid in (select id from contact where ...)
Alternatively, if you're inside Apex and already have the contact or account ids in a list (we'll use idList), you can use:
select {column list] from task where parentid in :idList

Resources