6

I have a long SOQL query with a lot of fields in the SELECT clause and conditions in the where clause. How do you write it, so it's stylistically sound?

I get that this is subjective...

0
11

The convention I adopt when selecting many fields is to:

  • Put the SELECT clause on its own line
  • Indent each line in the actual fields by 1 tab/4 spaces
  • Generally put as many fields as possible on one line without overrunning 80/100/120 according to line length preference
  • Put each sub-query on its own line
  • I also find it's best to put each clause on its own line, including WHERE and all its sub-clauses, GROUP BY, LIMIT, etc.

Example

List<MyObject__c> records = [
    SELECT
        Field1__c, Field2__c, Field3__c,
        Field4__c, Field5__c, Field6__c,
        (SELECT Field__c FROM Children1__c),
        (SELECT Field__c FROM Children2__c)
    FROM MyObject__c
    WHERE Condition1
    AND Condition2
    GROUP BY Field1__c
    ORDER BY Field1__c
    LIMIT 1000
];

That said, your format shouldn't really matter, and I've found getting hung up on this stuff does not make me a better programmer or colleague during peer review. The above is developed 100% based on my personal preference, and there are many other valid approaches.


Generally I try to take up as few lines as possible without making any individual line excessively long. So I relax the one-lining to some extent based on that. Some other formats that litter my own codebase:

List<MyObject__c> records = [SELECT SingleField__c FROM MyObject__c];
for (MyObject__c record : [
    SELECT Field1__c, Field2__c, Field3__c, Field4__c
    FROM MyObject WHERE Field5__c = :value
    ORDER BY Field1__c DESC
]) // do stuff
5
  • what about the begining and end brackets? – PartOfTheOhana Sep 2 '16 at 22:03
  • what's the convention based on? – PartOfTheOhana Sep 2 '16 at 22:05
  • It's just what I like. That's the point here, it just boils down to preference. – Adrian Larson Sep 2 '16 at 22:07
  • 1
    I also adopt this. For larger queries I put multiple fields on a single line for smaller queries I put each field on a separate line (Makes it easy to see what is and is not there). i would prefer to put fields on a separate line all the time but it just takes up too much dang room. Single fields / filter etc per row also allow you to make comments on that row as to why they should not be removed or are needed. A good IDE also helps. For example, Idea with Illuminated cloud will highlight a field in a SOQL query that was mistakenly entered twice. It all comes down to preference – Eric Sep 3 '16 at 1:56
  • 1
    I'd say applying the same tab-rules to your where expressions as your select field values would make it even nicer. – Samuel De Rycke Sep 3 '16 at 11:19
3

While this remains open, I'll extend it to include how static SOQL fits with other code elements and offer this as an example:

for (Diagnosis__c d : [
        select Id, MedicalCodeType__c, Journal__c
        from Diagnosis__c 
        where Journal__c in :journals
        and Id not in :exclusions
        ]) {
    // ...
}

where the main point is that the major structural elements of the SOQL are left clear of other language tokens and separated by line feeds. The double indent implies a continuation of an element to differentiate that from a new block.

Personally not fond of capitalizing in SOQL as that requires better keyboard skills than I have to do consistently and quickly.

(This style of for loop avoids the need for a variable to hold the list - often not needed - and for big queries is kinder on heap usage.)

4
  • This is perfect.... Do you have other stylistic guidelines beyond my q? – PartOfTheOhana Sep 2 '16 at 22:16
  • I sensed the initial question was how to handle it if you have many fields. I'm sure you would split it out eventually? – Adrian Larson Sep 2 '16 at 22:19
  • @user11235813 Yeah we work to some pretty specific standards as our code is worked on by multiple people and we care. The standards are not something we share at the moment though. – Keith C Sep 2 '16 at 22:19
  • @AdrianLarson Yeah the many fields looked untidy when I had it in the example so I took it out... but yes wrapping when too wide. – Keith C Sep 2 '16 at 22:21
1

For a large # of fields I definitely I prefer this style


List records = [
    SELECT
        Field1__c, Field2__c, Field3__c, Field4__c, Field5__c, Field6__c, 
        Field7__c, Field8__c, Field9__c, Field10__c, Field11__c, Field12__c,
        Field13__c, Field14__c, Field15__c, Field16__c, Field17__c, Field18__c
    FROM MyObject__c
    WHERE Foo__c = 'Bar'
    ORDER BY Field1__c
];

Not a fan of this style


List records = [
    SELECT
        Field1__c, 
        Field2__c, 
        Field3__c,
        Field4__c, 
        Field5__c, 
        Field6__c, 
        Field7__c,
        Field8__c, 
        Field9__c, 
        Field10__c,
        Field11__c,
        Field12__c,
        Field13__c,
        Field14__c,
        Field15__c,
        Field16__c,
        Field17__c,
        Field18__c
    FROM MyObject__c
    WHERE Foo__c = 'Bar'
    ORDER BY Field1__c
];
1
  • So basically you support what I already wrote? :P – Adrian Larson Sep 12 '16 at 12:06

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.