@@ -73,6 +73,8 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
7373 **/
7474 private Boolean enforceFLS ;
7575
76+ private Boolean sortSelectFields = true ;
77+
7678 /**
7779 * The relationship and subselectQueryMap variables are used to support subselect queries. Subselects can be added to
7880 * a query, as long as it isn't a subselect query itself. You may have many subselects inside
@@ -173,6 +175,17 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
173175 return this ;
174176 }
175177
178+ /**
179+ * Sets a flag to indicate that this query should have ordered
180+ * query fields in the select statement (this at a small cost to performance).
181+ * If you are processing large query sets, you should switch this off.
182+ * @param whether or not select fields should be sorted in the soql statement.
183+ **/
184+ public fflib_QueryFactory setSortSelectFields (Boolean doSort ){
185+ this .sortSelectFields = doSort ;
186+ return this ;
187+ }
188+
176189 /**
177190 * Selects a single field from the SObject specified in {@link #table}.
178191 * Selecting fields is idempotent, if this field is already selected calling this method will have no additional impact.
@@ -408,6 +421,10 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
408421 }
409422
410423 fflib_QueryFactory subselectQuery = new fflib_QueryFactory (relationship );
424+
425+ // The child queryFactory should be configured in the same way as the parent by default - can override after if required
426+ subSelectQuery .setSortSelectFields (sortSelectFields );
427+
411428 if (assertIsAccessible ){
412429 subSelectQuery .assertIsAccessible ();
413430 }
@@ -537,13 +554,17 @@ public class fflib_QueryFactory { //No explicit sharing declaration - inherit fr
537554 if (fields .size () == 0 ){
538555 if (enforceFLS ) fflib_SecurityUtils .checkFieldIsReadable (table , ' Id' );
539556 result += ' Id ' ;
540- }else {
557+ }else if ( sortSelectFields ) {
541558 List <QueryField > fieldsToQuery = new List <QueryField >(fields );
542559 fieldsToQuery .sort (); // delegates to QueryFilter's comparable implementation
543560 for (QueryField field : fieldsToQuery ){
544561 result += field + ' , ' ;
545562 }
546- }
563+ }else {
564+ for (QueryField field : fields )
565+ result += field + ' , ' ;
566+ }
567+
547568 if (subselectQueryMap != null && ! subselectQueryMap .isEmpty ()){
548569 for (fflib_QueryFactory childRow : subselectQueryMap .values ()){
549570 result += ' (' + childRow .toSOQL () + ' ), ' ;
0 commit comments