You need to sign in to do that
Don't have an account?
JosephT
Filter Trigger to Run by Specific Profile
I created the following Trigger for Lead Based Activites but now, I would like to filter the trigger to only run/be applied to a specific Profile... BUT, I am not exactly sure where I would add it?
Here is my current APEX Trigger;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | trigger taskTrigger on Task (after insert, after update, after delete) { if ((Trigger.isInsert || Trigger.isUpdate || Trigger.isDelete) && Trigger.isAfter) { set<Id> LeadIds = new set<Id>(); if(trigger.isInsert || trigger.isUpdate){ for(Task p : trigger.new){ LeadIds.add(p.WhoId); } } if(trigger.isDelete){ for(Task p : trigger.old){ LeadIds.add(p.WhoId); } } map<Id,Double> LeadMap = new map<Id,Double> (); CRMfusionDBR101.DB_Globals.triggersDisabled = true; for(AggregateResult q : [select WhoId,sum(Task_Name_Count__c) from Task WHERE CreatedDate = TODAY AND status = 'Completed' AND (Task_Type__c = 'Call' OR Task_Type__c = 'Call: Reached' OR Task_Type__c = 'Appointment Kept' OR Task_Type__c = 'Appointment Scheduled' OR Task_Type__c = 'VM' OR Task_Type__c = 'Email') AND WhoId IN :LeadIds group by WhoId]){ LeadMap.put((Id)q.get('WhoId'),(Double)q.get('expr0')); } List <Lead> LeadsToUpdate = new List <Lead> (); for(lead l : [Select Id, Task_Counter__c from Lead where Id IN :LeadIds AND (Status = 'To Be Contacted' OR Status = 'Contact Made'OR Status = 'Contact Attempted')]){ Double Sum = LeadMap.get(l.Id); l.Task_Counter__c = Sum; LeadsToUpdate.add(l); } update LeadsToUpdate; CRMfusionDBR101.DB_Globals.triggersDisabled = false; } } |
Hi,
You have to use an outermost if , that filters based on currently logged in users profile id
//Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
Please refer UserInfo.getProfileId() in the Apex Documentation.
Thanks
Aweesome. Where would I need to place this filter within my code?
It could be the first thing that you check in your trigger. If condition does not match exit the trigger.
That worked out well. Thank you. However, now, I am not getting my required Code coverage... Mind taking a peek at my Code?
Here it is;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
private class TestLeadRollUpTasks {
static testMethod void testRollup() {
Profile pf = [Select Id from Profile where Name = 'Publishing Associate'];
User u = new User();
u.FirstName = 'Test1';
u.LastName = 'User1';
u.Email = 'testuser1@test123456789.com';
u.CompanyName = 'test.com';
u.Title = 'Test User';
u.Username = 'testuser1@test123456789.com';
u.Alias = 'testuse1';
u.CommunityNickname = 'Test_User1';
u.TimeZoneSidKey = 'America/Mexico_City';
u.LocaleSidKey = 'en_US';
u.EmailEncodingKey = 'ISO-8859-1';
u.ProfileId = pf.Id;
u.LanguageLocaleKey = 'en_US';
insert u;
system.runAs(u){
Lead l = new Lead();
l.FirstName = 'Joe';
l.LastName = 'Smith';
l.Status = 'To Be Contacted';
l.Company = 'Test';
insert l;
system.assertEquals(l.Task_Counter__c, null);
Task t1 = new Task();
t1.WhoId = l.Id;
t1.Subject = 'test';
t1.Priority = 'Normal';
t1.Status= 'Completed';
t1.Task_Type__c = 'Call';
insert t1;
Lead lu1 = [select Task_Counter__c from Lead where Id = :l.Id];
system.assertEquals(lu1.Task_Counter__c,1);
Task t2 = new Task();
t2.WhoId = l.Id;
t2.Subject = 'test';
t2.Priority = 'Normal';
t2.Status= 'Completed';
t2.Task_Type__c = 'Call';
insert t2;
AggregateResult ag1 = [select sum(Task_Name_Count__c) from Task where WhoId = :l.Id];
Lead lu3 = [select Task_Counter__c from Lead where Id = :l.Id];
system.assertEquals(lu3.Task_Counter__c,ag1.get('expr0'));
delete t2;
AggregateResult ag2 = [select sum(Task_Name_Count__c) from Task where WhoId = :l.Id];
Lead lu4 = [select Task_Counter__c from Lead where Id = :l.Id];
system.assertEquals(lu4.Task_Counter__c,ag2.get('expr0'));
}
}
}
What line of code/trigger are not being hit? Does you code look for profile 'Publishing Associate' or something else?