I'm using Salesforce and trying to write a SOQL statement. My tables look like:
Person: [id, name]
Related: [id, personid1, personid2]
In sql, to find all the people someone is related to, I might write something like:
select person2.name from
person person1, related, person person2
where person1.id = 'xyz'
and person1.id = related.personid1
and related.person2 = person2.id
How can I achieve the same result set using a SOQL statement?
For the purposes of this query I'm going to assume your custom objects and fields use the regular Salesforce naming conventions.
If you're querying with a record ID:
select personid2__r.Name from Related__c where personid1__c = 'xxxyyyzzz123123'
Or if you're querying with a name:
select personid2__r.Name from Related__c where personid1__r.Name = 'John Doe'
If you absolutely need to return records of type Person__c, then you could do something like:
select Id, Name from Person__c where Id in (select personid2__c from Related__c where personid1__c = 'xxxyyyzzz123123')
Related
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.
I have a requirement to fetching data from Sales force. I need to get the data from two custom objects. I
have written query in sql can anyone help me to convert it into SOQL
SELECT ID, Name, Crop_Year__c, Targeted_Enrollment_Segments__c, Description__c, Start_Date__c,
End_Date__c from Enrollment_Program__c EP
Left Join Account_Enrollment__c AE on EP.Crop_Year__c = AE.Crop_Year__c and EP.ID =
AE.Enrollment_Program__c
where AE.Account__c = 'xyz'
As you probably know, Salesforce SOQL doesn't have explicit JOIN clauses. It does that for you implicitly based on related object fields. That means you'll have to query Account_Enrollment__c and traverse the fields to get the related Enrollment_Program__c Lookup relationship.
Another problem is Salesforce only performs joins based on primary and foreign keys, so the EP.Crop_Year__c = AE.Crop_Year__c in your query won't work.
So, with that said, you can try this:
SELECT Enrollment_Program__c, Enrollment_Program__e.Name,
Enrollment_Program__r.Crop_Year__c, Enrollment_Program__r.Targeted_Enrollment_Segments__c,
Enrollment_Program__r.Description__c, Enrollment_Program__r.Start_Date__c,
Enrollment_Program__r.End_Date__c
FROM Account_Entrollment_Program__c WHERE Account__c = 'zyz'
If you know beforehand what the Crop_Year__c value is, you can just add this to your query:
AND Crop_Year__c=:year AND Enrollment_Program__c.Crop_Year__c=:year
Some details on the queries:
The __r suffix is how you get the lookup object addressed in the query. If you are interested only in the id, you can use __c.
The :year is how you pass the parameter year to the query. If you want to append it as text you can just use ... Crop_Year='+ year + '.
I would like to fetch the attribute names like Listname,type,member type as shown in fig:
I used the following query:
SELECT Name,
*
FROM SavedQuery
WHERE ReturnedTypeCode = 4300
AND StateCode = 0
AND Name LIKE 'Active Marketing Lists%'
You have to query from list entity, that will result the needed columns of Marketing List.
SELECT listname, purpose, listid, membertype, type, lastusedon
FROM list
ORDER BY listname DESC
SavedQuery is view definition stored in DB like Active Marketing Lists.
Update:
The view columns which are designed/defined in view will be stored in layoutxml & layoutjson columns of savedquery records.
I'm trying to figure out how to query a custom object that is related to opportunities.
The object name is McaApp__Offer__c
The lookup field for that object is McaApp__Opportunity__c (master-detail)
This is what I have, but I'mk missing something as this object is not related to accounts, what do I need to change?
SELECT id, Name,
(
Select Id, Name From Opportunities ORDER BY Id DESC LIMIT 1
),
(
SELECT McaApp__Funder__c, McaApp__Status__c FROM McaApp__Offers__r WHERE McaApp__Opportunity__c = 'oppidxxx'
)
FROM Account
WHERE id = 'acctidxxx'
You can't query McaApp__Offer__c from within Account as there is no direct relationship. Account < Opportunity < McaApp__Offer__c this is how it realted.
SOQL statements cannot query aggregate relationships more than 1 level
away from the root entity object.
You can do like this.
SELECT Id, Name, AccountId,
(SELECT McaApp__Funder__c, McaApp__Status__c
FROM McaApp__Offers__r)
FROM Opportunity
WHERE AccountId = 'acctidxxx'
LIMIT 1
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)