Skip to content

Commit 8ca19a1

Browse files
authored
Added TaskQueryRepository (#5)
* Changed the deploy button in README.md to open in a new window * Added Task.object with 1 sample field set, MyFieldSet, containing a few standard fields * Added 'without sharing' to LeadQueryRepository.cls This should be tailored to fit your needs * Created TaskQueryRepository.cls - it mirrors the lead repo, with a method to get tasks by WhoId I also removed the comments that exist in LeadQueryRepository.cls - no sense in duplicating/maintaining the comments in 2 places * Went ahead and added 'without sharing' to QueryGenerator.cls as well
1 parent 5dd5969 commit 8ca19a1

File tree

7 files changed

+86
-3
lines changed

7 files changed

+86
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Apex Query Generator
2-
<a href="https://githubsfdeploy.herokuapp.com?owner=jongpie&repo=ApexQueryGenerator">
2+
<a target="_blank" href="https://githubsfdeploy.herokuapp.com?owner=jongpie&repo=ApexQueryGenerator">
33
<img alt="Deploy to Salesforce"
44
src="https://raw.githubusercontent.com/afawcett/githubsfdeploy/master/src/main/webapp/resources/img/deploy.png">
55
</a>

src/classes/LeadQueryRepository.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class LeadQueryRepository {
1+
public without sharing class LeadQueryRepository {
22

33
// Each SObject should have its own repository class that contains methods for commonly used queries
44
// Each method can use a different field set to generate the query fields,

src/classes/QueryGenerator.cls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
public class QueryGenerator {
1+
public without sharing class QueryGenerator {
22

33
// Each query repository class will have one or more methods that handle generating a query
44
// This class handles taking a fieldSet and an optional string for a WHERE statement
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
public without sharing class TaskQueryRepository {
2+
3+
// This class largely follows the same pattern as LeadQueryRepository.cls
4+
// For more information on how these classes should work, refer to the lead class
5+
// The main difference in this class is the inclusion of the getTasksByWhoId methods
6+
// Even though you may want to get all tasks for a lead, the query to handle
7+
// querying for those records should live in this class since ultimately
8+
// they are task records, so they fall under the TaskQueryRepository's domain
9+
10+
public static Task getTaskById(Id taskid) {
11+
return getTasksById(new Set<Id>{taskid})[0];
12+
}
13+
14+
public static List<Task> getTasksById(List<Task> taskList) {
15+
return getTasksById(new Map<Id, Task>(taskList).keySet());
16+
}
17+
18+
public static List<Task> getTasksById(List<Id> taskIdSet) {
19+
return getTasksById(new Set<Id>(taskIdSet));
20+
}
21+
22+
public static List<Task> getTasksById(Map<Id, Task> taskMap) {
23+
return getTasksById(taskMap.keySet());
24+
}
25+
26+
public static List<Task> getTasksById(Set<Id> taskIdSet) {
27+
String whereClause = 'WHERE ' + Schema.Task.Id + ' IN :taskIdSet';
28+
String query = new QueryGenerator(SObjectType.Task.FieldSets.MyFieldSet).buildQuery(whereClause);
29+
30+
return (List<Task>)Database.query(query);
31+
}
32+
33+
public static List<Task> getTasksByWhoId(Id whoId) {
34+
return getTasksByWhoIds(new List<Id>{whoId});
35+
}
36+
37+
public static List<Task> getTasksByWhoIds(List<Id> whoIdList) {
38+
String whereClause = 'WHERE ' + Schema.Task.WhoId + ' IN :whoIdList';
39+
String query = new QueryGenerator(SObjectType.Task.FieldSets.MyFieldSet).buildQuery(whereClause);
40+
41+
return (List<Task>)Database.query(query);
42+
}
43+
44+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<apiVersion>38.0</apiVersion>
4+
<status>Active</status>
5+
</ApexClass>

src/objects/Task.object

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CustomObject xmlns="http://soap.sforce.com/2006/04/metadata">
3+
<fieldSets>
4+
<fullName>MyFieldSet</fullName>
5+
<description>Sample field set</description>
6+
<displayedFields>
7+
<field>Description</field>
8+
<isFieldManaged>false</isFieldManaged>
9+
<isRequired>false</isRequired>
10+
</displayedFields>
11+
<displayedFields>
12+
<field>WhoId</field>
13+
<isFieldManaged>false</isFieldManaged>
14+
<isRequired>false</isRequired>
15+
</displayedFields>
16+
<displayedFields>
17+
<field>WhatId</field>
18+
<isFieldManaged>false</isFieldManaged>
19+
<isRequired>false</isRequired>
20+
</displayedFields>
21+
<displayedFields>
22+
<field>Subject</field>
23+
<isFieldManaged>false</isFieldManaged>
24+
<isRequired>false</isRequired>
25+
</displayedFields>
26+
<displayedFields>
27+
<field>Type</field>
28+
<isFieldManaged>false</isFieldManaged>
29+
<isRequired>false</isRequired>
30+
</displayedFields>
31+
<label>My Field Set</label>
32+
</fieldSets>
33+
</CustomObject>

src/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</types>
77
<types>
88
<members>Lead.MyFieldSet</members>
9+
<members>Task.MyFieldSet</members>
910
<name>FieldSet</name>
1011
</types>
1112
<version>38.0</version>

0 commit comments

Comments
 (0)