Combine two Salesforce SOQL Query

728 views Asked by At

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.

1

There are 1 answers

0
eyescream On

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.