1

I'm experiencing something strange with my SOQL results. Our salesforce has a custom sObject named Deals__c and an intermediary object, contact_deals__c which solves the m2m relationship between contacts and deals. I'm trying to grab all the contacts that have at least 5 deals associated with them. To do this, we can simply count how many contact_deals__c.id are associated to the contact.

Contact --> Contact_deals__c = Master --> Detail

Contact_Deals_Count__c is a custom Rollup field that counts how many Contact_deals__c are associated with the contact.

I get two, slightly overlapping results between these two queries:

SELECT name, Contact_Deals_Count__c 
FROM contact 
WHERE contact_deals_count__c > 4

AND

SELECT name, Contact_Deals_Count__c, 
    (SELECT id 
     FROM contact_deals__r) 
FROM contact 
WHERE contact_deals_count__c > 4

I would think that I should get two identical lists, one having an additional field showing the id's of the Contact_Deals__c. Instead, I get differing results.

How can this be? Especially since we're including only records that the rollup field (contact_deals_count__c) has confirmed has at least 5 Contact_Deals__c associated to them. Why would the second SOQL query leave some of these results out?

Thank you all! You are saving my job!

*EDIT: Seeing the comments here, perhaps there is nothing wrong with the queries and something wrong elsewhere. I'm going to focus on trying to isolate parts of the env to figure it out. The comment to try workbench will likely come in handy ;)

With gratitude,

Zac

  • You mind sharing the results you are getting? – Santanu Halder May 8 '15 at 21:12
  • I wish - but then I'd get fired and sued... – ZAR May 8 '15 at 21:19
  • 1
    Lol. Nevermind, I just created 2 custom objects and tried your queries. Its working for me. Did you try running the queries in WorkBench? workbench.developerforce.com/login.php – Santanu Halder May 8 '15 at 21:26
  • Will check it out soon – ZAR May 8 '15 at 21:56
  • When you experience different results, are you running as the same user/profile/role in both cases? – Mark Pond May 8 '15 at 22:21
0

You are right. Both queries should return same number of records. But second query will have additional contact_Deal__c records under each contact record.

If it is a small set of data, try creating a report with condition "contact_deals_count__c > 4" and find which query is giving correct result.

  • Right - the report is agreeing with the first one. But that isn't really helping since I do need the ID's of the Contact_Deals__c. I guess I can break it up into two different queries, but I have no idea why the above isn't just working... – ZAR May 8 '15 at 21:25
  • You might have to raise a ticket with salesforce – Victor May 8 '15 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.