You need to sign in to do that
Don't have an account?
Florian Boulet
Process result of SOQL query
Hello,
I'm getting the following data :
Have you any idea how to do it ? It looks very simple problem but i'm stuck since few hours now ... oO'
I'm getting the following data :
SELECT Id, Contact.Id, Contact.Name, Contact.AccountId, Contact.Account.Name FROM CampaignMember WHERE Campaign.Type = 'Programme Application'Now for each row I would like the count of each contact in the account with a CampaignMember.
SELECT COUNT() FROM CampaignMember WHERE Contact.AccountId = $$CURRENT_CONTACT_ACCOUNT$$I tried the following but it doesn't works :
SELECT Id, Contact.Id, Contact.Name, Contact.AccountId, Contact.Account.Name, (SELECT COUNT() FROM CampaignMember cm2 WHERE cm2.Contact.AccountId = cm.Contact.AccountId) as countMDC FROM CampaignMember cm WHERE Campaign.Type = 'Programme Application'With APEX i tried to parse every row of the first query to run the second query, but i can't access the Contact.AccountId value of each row. The returned object is a CampaignMember with only ContacId, CampaignMemberID and CampaignId.
for(SObject currentContact : [SELECT Id, Contact.Id, Contact.Name, Contact.AccountId, Contact.Account.Name FROM CampaignMember WHERE Campaign.Type = 'Programme Application']){ System.debug(currentContact); }
Have you any idea how to do it ? It looks very simple problem but i'm stuck since few hours now ... oO'
It sounds like you want each campaign member to indicate the total number of contacts in the campaign that have the same account as the contact on the campaign member record. To store a value on an SObject record, even if you're not saving it to the CampaignMember object, you'll need that field to exist on the SObject. So *if* you create a "contactCount" field on the CampaignMember object, then you code code it like this:
If you don't want the new field in the CampaignMember object, then you'll need to create a wrapper object that has the CampaignMember record as one member (or each element of CampaignMember you need) and the count as another. For example (I've renamed the initial SOQL results because it's no longer the result that is returned):
All Answers
I'm sorry but i don't understand your reply.
When accessing related fields from a generic SObject as you have it coded, you have to retrieve each piece separately. So you would have to access it like this:
If you have a single campaign Id you're interested in you can get the number of contacts for each account with something like this:
I was missing the ".getSObject()" function. Now i can manage to access the good data.
But I still need help tp aggregate data.
Here my function : Thanks you for your help.
Florian
It sounds like you want each campaign member to indicate the total number of contacts in the campaign that have the same account as the contact on the campaign member record. To store a value on an SObject record, even if you're not saving it to the CampaignMember object, you'll need that field to exist on the SObject. So *if* you create a "contactCount" field on the CampaignMember object, then you code code it like this:
If you don't want the new field in the CampaignMember object, then you'll need to create a wrapper object that has the CampaignMember record as one member (or each element of CampaignMember you need) and the count as another. For example (I've renamed the initial SOQL results because it's no longer the result that is returned):
Thank you very much for your anwsers and your time !