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
Related
I am trying to create a SOQL query (and later a report) in Salesforce that generates the following data:
Count of Child records grouped by Parent records where the Parent does not have child records created in the past year.
I tried this first; however, salesforce returned an error stating 'Nesting of semi join sub-selects is not supported'.
SELECT Id, Name, Training_Course__c
FROM Training_Record__c
WHERE Training_Course__c IN (
SELECT Id
FROM Training_Course__c
WHERE Id NOT IN (
SELECT Training_Course__c
FROM Training_Record__c
WHERE CreatedDate != Last_n_Days:365
)
)
The requirements are to use a single query to obtain the data requested without forcing them to run two reports and use Excel to get the data. I'm not sure if that's possible given Salesforce's constraints.
Is this possible in SOQL? If so, what can I do differently?
This is not perfect but a decent start, you'd need to manually call count/size/length on the child records (exact method depends on your client application's language). In a SF report you can group by parent Id and call it a day. Read about "cross filters" in SF reports.
SELECT Id, Name,
(SELECT Id FROM Opportunities)
FROM Account
WHERE Id NOT IN (SELECT AccountId FROM Opportunity WHERE CloseDate = LAST_N_DAYS:365)
LIMIT 100
This will not compile:
SELECT AccountId, COUNT(Id)
FROM Opportunity
WHERE AccountId NOT IN (SELECT AccountId FROM Opportunity WHERE CloseDate = LAST_N_DAYS:365)
Error: The inner and outer selects should not be on the same object type
but if you really need something like that you could experiment, do it in 2 steps. Create helper field on account, tick the checkbox for eligible accs (with some nightly batch job maybe), then query based on that.
(normally you could do it with rollup summary field from opps to account but rollups have to be based on solid data, "deterministic". "LAST 365 DAYS", "TODAY" etc can't be used as rollup criteria)
I have a custom Commission table which has a master detail relationship with contact. I have a List of AccountIds being passed into the function and then into the query. I'm getting the error: " Didn't understand relationship 'Contacts' in FROM part of query call" Any help would be great.
List<Commission__c> comList2 = [SELECT commission_amount__c, date_given__c,
(SELECT Id FROM Contacts WHERE AccountId in : accountIds)
FROM Commission__c];
SELECT commission_amount__c, date_given__c, Contact__r.Name, Contact__r.Email
FROM Commission__c
WHERE Contact__r.AccountId IN :accountIds
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_understanding.htm
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)
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);
}
}
I have 3 tables, a parent table and 2 child tables. Lets say Mother_c is the parent. Then Child_c and Pet_c are the 2 child tables that have master-detail relationship pointer to Mother_c.
I have the Id of one row from Child_c, I want to retrieve all the rows from Pet_c that correspond to the Mother_c of that single Child_c row.
I'm wondering if this is possible in one SOQL query?
Yes, this is totally possible with semi-join SOQL. I tested this out with the standard CRM objects like this:
SELECT Id,
(SELECT Id FROM Cases)
FROM Account
WHERE Id IN (SELECT AccountId
FROM Contact
WHERE Id = '0036000000qCwp9'
)
To walk you through this, with a given Contact id, you first find the parent Account, and then traverse back down to the child Cases. In your example with custom objects, it would be very similar, but would use the __r custom relationships names instead:
SELECT Id,
(SELECT Id FROM Pet__r)
FROM Mother__c
WHERE Id IN (SELECT Mother__c
FROM Child__c
WHERE Id = '003a000000qCwp9'
)