@@ -4,6 +4,8 @@ public without sharing class LeadRepository extends SObjectRepository {
44
55 public LeadRepository () {
66 super (LeadRepository .DEFAULT_FIELD_SET );
7+ // Any conditions added in the constructor will apply to all queries in this class
8+ this .whereIsConverted (false );
79 }
810
911 // Overload the constructor if you want to allow other code to specify the field set used
@@ -13,65 +15,55 @@ public without sharing class LeadRepository extends SObjectRepository {
1315
1416 // ISObjectRepository requires at least 2 methods, getRecord & getList
1517 public Lead getRecord (Id leadId ) {
16- String query = this
17- .addConditionIdEquals (leadId )
18+ return ( Lead ) this
19+ .whereIdEquals (leadId )
1820 .setAsUpdate ()
19- .getQuery ();
20-
21- return (Lead )Database .query (query )[0 ];
21+ .getFirstQueryResult ();
2222 }
2323
2424 public List <Lead > getList (List <Id > leadIdList ) {
25- String query = this
26- .addConditionIdIn (leadIdList )
25+ return ( List < Lead >) this
26+ .whereIdIn (leadIdList )
2727 .setAsUpdate ()
28- .getQuery ();
29-
30- return (List <Lead >)Database .query (query );
28+ .getQueryResults ();
3129 }
3230
3331 // Add public methods needed that return the query results
3432 // Only methods that return an SObject or collection of SObjects should be made public
3533 public List <Lead > getListForSources (List <String > leadSourceList ) {
36- String query = this
37- .addCondition (Schema .Lead .LeadSource + ' IN ' + CollectionUtils . toString ( leadSourceList ) )
34+ return ( List < Lead >) this
35+ .whereFieldIn (Schema .Lead .LeadSource , leadSourceList )
3836 .orderBy (Schema .Lead .CreatedDate )
39- .getQuery ();
40-
41- return (List <Lead >)Database .query (query );
37+ .getQueryResults ();
4238 }
4339
4440 public List <Lead > getListForStatus (String status , Integer limitCount ) {
45- String query = this
46- .addConditionIsConverted (false )
47- .addConditionStatusEquals (status )
41+ return ( List < Lead >) this
42+ .whereIsConverted (false )
43+ .whereStatusEquals (status )
4844 .limitCount (limitCount )
4945 .orderBy (Schema .Lead .LastModifiedDate , SObjectRepository .SortOrder .DESCENDING )
5046 .setAsUpdate ()
51- .getQuery ();
52-
53- return (List <Lead >)Database .query (query );
47+ .getQueryResults ();
5448 }
5549
5650 public List <Lead > searchInAllFields (String searchTerm ) {
57- String query = this
58- .addConditionIsConverted (false )
51+ return ( List < Lead >) this
52+ .whereIsConverted (false )
5953 .orderBy (Schema .Lead .CreatedDate , SObjectRepository .SortOrder .DESCENDING )
6054 .limitCount (10 )
6155 .setAsUpdate () // SOSL cannot use FOR UPDATE. This will execute, but a warning debug statement will indicate that it is ignored
62- .getSearchQuery (searchTerm , SObjectRepository .SearchGroup .ALL_FIELDS );
63-
64- return (List <Lead >)Search .query (query )[0 ];
56+ .getSearchResults (searchTerm , SObjectRepository .SearchGroup .ALL_FIELDS );
6557 }
6658
6759 // You can add additional builder methods for any commonly used filters for this SObject
6860 // All builder methods should be kept as private or protected
69- private LeadRepository addConditionIsConverted (Boolean bool ) {
70- return (LeadRepository )this .addCondition (Schema .Lead .IsConverted + ' = ' + bool );
61+ private LeadRepository whereIsConverted (Boolean bool ) {
62+ return (LeadRepository )this .whereFieldEquals (Schema .Lead .IsConverted , bool );
7163 }
7264
73- private LeadRepository addConditionStatusEquals (String status ) {
74- return (LeadRepository )this .addCondition (Schema .Lead .Status + ' = ' + StringUtils . wrapInSingleQuotes ( status ) );
65+ private LeadRepository whereStatusEquals (String status ) {
66+ return (LeadRepository )this .whereFieldEquals (Schema .Lead .Status , status );
7567 }
7668
7769}
0 commit comments