• Mendy - Kurant
  • 4 Points
  • Member since 2014

  • Chatter
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
  • 2
I have seen threads with dozens of questions about this over the last 6-7 years, all with the answer of "can't be done".  But, I keep hoping that each new version of SFDC might include a way of accomplishing it, so I'll ask the question once again.

I would like to use SOQL to query a parent object based on whether it does or does not have any child records (whether MD or Lookup).

The perfect query would be something like: 
- SELECT Id FROM Account WHERE COUNT(Contacts__r) = 0
- SELECT Id FROM Account WHERE Contacts__r.size() = 0
- SELECT Id FROM Account WHERE Contacts__r = null

From what I have found in threads as late as 2013, this does not appear to be possible.

Is that still the case in the Summer 2014 release?

It would appear that (for my needs) the best way to execute this type of query would be to:
- SELECT Name, (SELECT name FROM Contacts) FROM Account
- Then programmatically check the size of the returned set of Contacts

Is this my best option?  I know that I could:
- SELECT Id, Name FROM Account WHERE Id IN (SELECT AccountId FROM Contact)
But the actual objects that I want to interogate are more that the 50k limit for this type of query.


Bryan Hunt

I'm trying to copy a new user as contact by using a trigger but I'm getting the following error


MIXED_DML_OPERATION, DML operation on setup object is not permitted after you have updated a non-setup object (or vice versa): Contact, original object: User


trigger Acceleration_User_Copyto_Contact on User (after insert, after update) {
   // Map<String,User> newLead = new Map <String, User>();
    system.debug('Acceleration_User_Copyto_Contact Trigger....');
    for ( User user : System.Trigger.new)
        if  (user.Email.contains('acceleration'))
            system.debug('Acceleration_User_Copyto_Contact Trigger..2.');
            Integer RecordCount = [select count() from Contact c where c.Email = : user.Email];
            system.debug('Acceleration_User_Copyto_Contact Trigger..2a .' + RecordCount);
            if (RecordCount == 0)
                String AccountId = '';
                for ( Account a : [Select a.Id From Account a where Name = 'Acceleration']) 
                    system.debug('Acceleration_User_Copyto_Contact Trigger..3.');
                    AccountId = a.Id;
                    Contact con = New Contact();
                    con.AccountId = AccountId ;
                    con.Email = user.Email;
                    con.Firstname = User.Firstname;
                    con.Lastname = User.Lastname ;
                    con.User__c = User.Id;
                    insert con;