Combine two Salesforce SOQL Query - salesforce

I am using two SOQL query in Salesforce.
First Query: Select Id, FirstName, LastName from User where Id='00000ADFEDSFSRTGDR'
Second Query: Select IsFrozen from UserLogin where UserId='00000ADFEDSFSRTGDR'
Can we combine these two query into a single query. Please help me on this.

No. If you use "describe" calls on User or UserLogin you'll see they are linked but there's no "relationshipName" field in the describe's result. That's what's used to link, bit like table alias in normal database.
// No going "up"
System.debug(UserLogin.UserId.getDescribe().getRelationshipName());
// And no going "down" either
for(Schema.ChildRelationship cr : User.SObjectType.getDescribe().getChildRelationships()){
if(cr.getChildSObject() == UserLogin.sObjectType && cr.getField() == UserLogin.UserId){
System.debug(cr);
System.debug(cr.getRelationshipName());
}
}
So you can do
SELECT Id, Name,
(SELECT PermissionSet.Name FROM PermissionSetAssignments)
FROM User
because PermissionSetAssignment.AssigneeId has relationshipName. But not
SELECT Id, Name,
(SELECT IsFrozen FROM UserLogin)
FROM User
Going "up" doesn't work either. SELECT Account.Name FROM Contact works OK but SELECT IsFrozen, User.Name FROM UserLogin doesn't. Again - because there's no relationshipName in the describe results.
You'll have to query separately and link them them in code as Map<Id, User> for example.

Related

Apply query criteria based on parameters

I need to run a query in a MS Access Database providing some parameters from a form. Imagine the next example:
I have a form which contains:
CheckBox1 and Text1
CheckBox2 and Text2
Button (to run query)
Now imagine a query with two fields: ID, NAME.
I want to filter ID by Text1 only when CheckBox1 is enabled. If not, I want the query not to filter ID in any way (as if the 'query' input was empty).
In the same way, I want to filter NAME by Text2 only when CheckBox2 is enabled. If not, I want the query not to filter NAME in any way (just like ID before).
I've tried so many things for a couple of days and have sniffed tons of internet pages and still don't come up with a solution.
You can use a SQL query such as the following:
select * from YourTable t
where
([Forms]![YourForm]![CheckBox1] = False or t.ID = [Forms]![YourForm]![Text1]) and
([Forms]![YourForm]![CheckBox2] = False or t.NAME = [Forms]![YourForm]![Text2])
(Change YourTable to the name of your table and YourForm to the name of your form; t is merely an alias so that you only have to change the table name in one place in the code).

Select certain properties (fields) when using BOTH in Orient SQL

Using OrientDB 2.* with OrientSQL.
I have a simple graph with Class Users that has a number of properties (username, country, pets, etc). Each user also has outgoing friends edge. I want to select only the username and country from all users who are friends with a specific user.
My query so far:
SELECT EXPAND( BOTH('friends') ) FROM users WHERE #rid = #12:0
returns the full user objects for those who are friends of #12:0
I want only to return the username and country of those friends.
Am I missing something simple? Much appreciated!
You can:
select expand(both('friends').include('username', 'country'))
from #12:0
Note that you should:
select from #12:0
instead of:
select from Users where #rid = #12:0
A sub-query would work. I don't think there is any other way to do this.
Try this :
select
username, country
from (select
expand( both('friends') )
from
#12:0)

Salesforce SOQL Filter by child relationship

I have the following simple query which shows I can access the field I want to filter by:
SELECT Id, Name, (SELECT HC4__IsSearchableExternally__c FROM Contacts)
FROM Account
However, what I really want to do is return only the Id and Name properties for Accounts that have at least one Contact where HC4__IsSearchableExternally__c is true. Is this possible to do with a Salesforce query?
Basically, I want to do something like the following (nonfunctional query):
SELECT Id, Name
FROM Account
WHERE (SELECT COUNT(Id) FROM Contacts WHERE HC4__IsSearchableExternally__c = true) > 0
Thanks for any help you can provide!
You can do this with a semi-join, e.g:
select id, name from account
where id in (select accountId from contact where HC4__IsSearchableExternally__c = true)

Salesforce - Apex - query accounts based on Activity History

Hey Salesforce experts,
I have a question on query account information efficiently. I would like to query accounts based on the updates in an activityHistory object. The problem I'm getting is that all the accounts are being retrieved no matter if there's "complete" activeHistory or not. So, Is there a way I can write this query to retrieve only accounts with activeHistory that has status="complete" and Type_for_reporting='QRC'?
List<Account> AccountsWithActivityHistories = [
SELECT
Id
,Name
,( SELECT
ActivityDate
,ActivityType
,Type_for_Reporting__c
,Description
,CreatedBy.Name
,Status
,WhatId
FROM ActivityHistories
WHERE Status ='complete' and Type_for_Reporting__c = 'QRC'
)
FROM Account
];
You have a WHERE clause on the histories but you still miss one on the Account level.
For example this would return only Accounts that have Contacts:
SELECT Id, Name
FROM Account
WHERE Id IN (SELECT AccountId FROM Contact) // try with NOT IN too
With Activities it's trickier because they don't like to be used in WHERE in that way.
http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select.htm
The following objects are not currently supported in subqueries:
ActivityHistory
Attachments
Event
EventAttendee
Note
OpenActivity
Tags (AccountTag, ContactTag, and all other tag objects)
Task
Additionally the fine print at the bottom of ActivityHistory definition is also a bit discouraging.
The following restrictions on users who don’t have “View All Data” permission help prevent performance issues:
In the main clause of the relationship query, you can reference only
one record. For example, you can’t filter on all records where the
account name starts with ‘A’; instead, you must reference a single
account record.
You can’t use WHERE clauses.
You must specify a limit of 499 or fewer on the number of rows returned in the list.
You must sort on ActivityDate in ascending order and LastModifiedDate in descending order; you can display nulls last. For
example: ORDER BY ActivityDate ASC NULLS LAST, LastModifiedDate DESC.
Looks like you will need multiple queries. Go for Task (or Event, depending for which the custom field is visible), compose a set of AccountIds and then query the Accounts?
Or you can manually filter through list from your original query, copying accounts to helper list:
List<Account> finalResults = new List<Account>();
for(Account a : [SELECT...]){
if(!a.ActivityHistories.isEmpty()){
finalResults.add(a);
}
}

soql select individual CaseComment with all its FeedComments

I am trying to select all comments ,feeds and feedcomments for an individual case.
The hierarchy is like
Case
|
CaseComment
|
FeedComments(commnets or feeds under a CaseComment)
I could not find any relation between CaseComments and FeedComments nor CaseComments and CaseFeeds.
How can I select all together in a soql or individual soqls which relates Case, CaseComment,CaseFeed,FeedComment?
EDIT
The query you've included in the comment looks good. I'd write it as something like that:
SELECT Id, Body, ParentId, Parent.CaseNumber, CreatedDate,
(SELECT Id, CommentBody, CommentType FROM FeedComments)
FROM CaseFeed
ORDER BY Parent.CaseNumber, CreatedDate
(this is sample output rendered in Real Force Explorer, a pretty neat tool)
If I'll click into the "2 records" bit I can drill down to the fields selected from FeedComment for "this" CaseFeed:
If your query renders differently for you (some stuff is blank) - maybe try this different editor or even go to https://workbench.developerforce.com
If only some comments contain text - they might be uploaded images for example - filter them by CommentType = 'TextComment'?
ORIGINAL
FeedComments(commnets or feeds under a CaseComment)
No, not really. FeedComment is a Chatter table that can link to many objects but CaseComment is not one of them.
Maybe study the Chatter Entity Relationship Diagram?
Anyway - relationship to feed* objects doesn't have a nice name exposed so we can't query it all in one go:
I think you'll need something like this:
SELECT Id, CaseNumber,
(SELECT Id, CommentBody FROM CaseComments),
(SELECT Id, Body FROM Feeds)
FROM Case
SELECT Id, FeedItemId, ParentId, CommentBody
FROM FeedComment
WHERE ParentId = :caseIdHere

Resources