3

I have written a function like the one below to check if there is atleast one occurrence of a given Id in either of the two objects. I am having doubts on if I should use an if branch or use a ternary expression like this? Which is more readable?

public static Boolean isPresent(SObject object) {
    if(object.Id == null) return false;
    return
           [SELECT count() FROM A__c WHERE Obj__c =: object.Id LIMIT 1] > 0 ? true :
           [SELECT count() FROM B__c WHERE Obj__c =: object.Id LIMIT 1] > 0;
}
2
  • In each case, your example shows Obj__c as the filter field. Is this specifically a case of wanting to check to see if any of the following objects have a populated relationship field where this record is the value in that field?
    – pchittum
    Oct 26, 2015 at 11:12
  • @Peter yes. The query checks to see if the argument object is a value in either A__c or B__c
    – s4san
    Oct 26, 2015 at 12:12

2 Answers 2

1

Inferring a little bit here, based on your question that A__c and B__c are both children of Obj__c.

If that were the case, and you have to take the hit on the query anyway, why not do a relationship query on the parent record. (I renamed A__c and B__c to "Plant__c" and "Animal__c" because it reads better):

Obj__c myRecord = [SELECT Id, 
                    (SELECT Id FROM Plants__r LIMIT 1),
                    (SELECT Id FROM Animals__r LIMIT 1)
                   FROM Obj__c
                   WHERE Id: = object.Id];

return myRecord.Plants__r.size() > 0 || myRecord.Animals__r.size() > 0;

You may need a null check in there on Plants__r and Animals__r (I'm doing this freehand, no actual checking). But in theory, if you're making one trip to the DB, why not just get everything you want the one time? If you're just trying to see if the Id is used in one-or-more children...relationship queries will help you do this.

6
  • This is probably the neater way but my Obj__c is just a lookup from Plants__c and Animals__c. There is no master-detail relationship involved here.
    – s4san
    Oct 26, 2015 at 12:24
  • That is irrelevant as to whether or not this should work. Relationship fields are relationship fields when it comes to SOQL. The only question is whether or not the field is nullable (M-D: yes, lookups: usually). From the standpoint of querying children, that doesn't matter.
    – pchittum
    Oct 26, 2015 at 12:26
  • Please forgive me if I wasn't being clear, what I meant was, there is no such relationship as Obj__r.Plants__c in my schema. Obj does not contain Plants rather Plants containObj (sometimes). Does the query work on such cases too?
    – s4san
    Oct 26, 2015 at 12:30
  • If Plant__c (the object) has a lookup field that points to/references Obj__c, then this works. Likewise with Animal__c. Or another way to think of it: if I look at the page layout of Obj__c and there is (or I have the option to put) a related list that shows records from Animal__c and Plant__c, then this works.
    – pchittum
    Oct 26, 2015 at 12:48
  • Plant__c has a lookup to Obj__c and not the other way round.
    – s4san
    Oct 26, 2015 at 12:56
2

It seems a bit unclear to me, but it's perfectly acceptable. I'd go with something like:

return     [SELECT count() FROM A__c WHERE Id =: object.Id LIMIT 1] > 0 ||
           [SELECT count() FROM B__c WHERE Id =: object.Id LIMIT 1] > 0;

Take a look at this thread too.

EDIT: for more of similar branches, I'd go with something more funny, like:

return     [SELECT count() FROM A__c WHERE Id =: object.Id LIMIT 1] +
           [SELECT count() FROM B__c WHERE Id =: object.Id LIMIT 1] +
           [SELECT count() FROM C__c WHERE Id =: object.Id LIMIT 1] +
           [SELECT count() FROM D__c WHERE Id =: object.Id LIMIT 1] > 0;
5
  • I have edited the question to reflect my actual usecase, where the count can be greater than 1, basically the idea is to stop counting after finding 1 matching record.
    – s4san
    Oct 26, 2015 at 8:39
  • I edited my code snippet, now it has also limit clause Oct 26, 2015 at 8:41
  • A follow-up question to your answer: Say, I have 3-4 || branches, like the one you have mentioned, do I abstract each SOQL query into a separate function and apply || conditions, or is it fine to chain them altogether?
    – s4san
    Oct 26, 2015 at 8:55
  • 1
    I think having an || field is better than + because || guarantees 1 query in best case and 2 in the average to be executed unlike + which executes all 4 all the time.
    – s4san
    Oct 26, 2015 at 12:27
  • True, in terms of performance, I don't think that there is anything better than the first example out there. Oct 26, 2015 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .