Joining LoginHistory to User using implicit parent-child relationship - salesforce

I have been trying to bring in fields from the User object in my LoginHistory query. I keep getting an error saying the relationship is not understood even though there is a parent-child relationship.
select Userid, user.Username from loginHistory
The above is not working and to me it seems its the simplest query. What am I doing wrong?

LoginHistory.UserId.getDescribe() shows "null" as the "relationshipName".
You can not join LoginHistory with User. I tried it but no luck. I fetched data locally and then applied the join.

Related

parent-child and child-parent relationships in one query SOQL

Is it possible to write a query to access parent-child and child-parent objects, in one SOQL query?
I have a scenario where, I need to access Account Objects from child and child of Account too, in the same query.
Example:
Select Id,
(Select Id,Name, (Select Id, Address from Addresses__r) from x__r.Account),
x__r.Account.Name
From x
.
(Pardon me, if I use any wrong terms. I am pretty new to Salesforce)
Yes, it is possible to do so.
In the example below, we are using sub-query to retrieve Ids of all Account Territories (parent-to-child relationship) and, at the same time, we are using child-to-parent relationship to retrieve TimeZoneSidKey of the User who has created the record.
SELECT Id, (SELECT Id FROM Accounts_Territories__r), CreatedBy.TimeZoneSidKey FROM Account
Documentation on the topic

Join User and LoginHistory in SalesForce?

I am unable to get the user information from LoginHistory object.
I tried using this
SELECT User.FirstName FROM LoginHistory
but not it says
INVALID_FIELD:
SELECT User.FirstName FROM LoginHistory
^
ERROR at Row:1:Column:8
Didn't understand relationship 'User' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
System.debug(JSON.serializePretty(LoginHistory.UserId.getDescribe())); shows "null" as the "relationshipName". Not all relations allow going "up" this way, you can check my answer https://salesforce.stackexchange.com/a/23507/799 for some examples.
You'll need to make-do with two separate queries. There's equally no way to reverse it because the related list doesn't have name either. This won't work:
SELECT FirstName,
(SELECT Id FROM LoginHistories LIMIT 10)
FROM User
LIMIT 10
Maybe there's an idea you can upvote? Maybe you'll have more luck with Event Monitoring. Haven't used it personally but my understanding is it can track login, logout, exporting a report... Might be easier to query.

Solr join returns zero results

I am using Solr 6.4.2. I have defined 2 cores:
companies, with the fields 'Id, Town, Name, Type, ManagerId'
users, with the fields 'Id, Login, ManagerId, Email'
In users core the field ManagerId is parent-child relation (ManagerId->Id).
Companies and users are related by companies.ManagerId->users.Id
I am trying to build a very simple join query:
{!join from=ManagerId to=Id fromIndex=users}Login:someuser1
url looks like:
?q=*:*&fq={!join%20from=ManagerId%20to=Id%20fromIndex=users}Login:someuser1
nothing works, I always get zero results. I just want to understand how Solr join works. It seems to me that there is a big difference in understanding between Solr joins and SQL joins.
In fact I want to do the queries like:
get all docs from users by company Types
get companies by user managers
Right now I always get zero results no matter how I write the join query.
Try this on the company core (assuming you want the get all companies run by a certain manager login):
login:someuser1 is the filter condition you put on the child table, this should be the manager login you are looking for
from=ManagerId should be the id on the child table, so this is wrong
to=id is the field on the parent table that relates to the child table, so this is wrong
fromIndex is the child table, this is correct
{!join from=ManagerId to=Id fromIndex=users}Login:someuser1

SalesForce SOQL join Leads to Contact

I'm trying to query data out of SalesForce Lead using SOQL and JOIN and obtain contact information where for the Lead Created through the contacts object.
I'm not sure which objects to use how to go about doing it.
Does anyone know of a detail schema of what can be used with SOQL relationship queries.
I've tried some examples from the following links but don't know how to do the same with Leads, contacts:
https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html
First of all you didnt mention properly what you want
List<lead> info = [Select * from Lead where Id = 'Id name of that Particular record of lead'];
system.debug(info);
now you can see all information of that particular lead in logs.

Salesforce SOQL Queries and Tags

I'm just getting started with the Salesforce Web Services API and I'm surprised that there isn't an obvious way to do a query for all e.g. Account objects that contain certain tags.
What would you say is the best way to find all objects that contain certain tags?
I imagine it involves a join on Account.id and AccountTag.id or something similar, but despite some real research, I'm not sure how best to solve this problem.
Thanks in advance!
Update: I guess I could do a select from AccountTag and then get the account objects based on ItemId, but the ideal would be to do a query on Account, with Tags being only one part of the criteria.
You can use the SOQL-R style queries to do this, e.g. this will fetch the account Id and account Name for all the accounts with the internet tag.
select item.id, item.name from accountTag where name='internet'
in this case the item relationship is to the account that was tagged, so you can select any field from the account object through the item relationship path.
See the SOQL-R docs for more info

Resources