-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreate_Targeting_Allocations.tgr
83 lines (77 loc) · 4.43 KB
/
Create_Targeting_Allocations.tgr
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
trigger Create_Targeting_Allocation on Opportunity (after insert, after update) {
if (trigger.isinsert || trigger.isundelete) {
list<targeting_allocation__c> toInsert = new list<targeting_allocation__c>();
for (opportunity o: trigger.new) {
if (o.id != null && (o.RecordTypeId=='012a00000018Esx' || o.RecordTypeId == '012300000006GiS' || o.RecordTypeId == '012300000006GiI')){
targeting_allocation__c x, y, z;
x = new targeting_allocation__c (); y = new targeting_allocation__c (); z = new targeting_allocation__c ();
x.allocation_opportunity__c = o.id; y.allocation_opportunity__c = o.id; z.allocation_opportunity__c = o.id;
x.Allocation_Percent__c = 100; y.Allocation_Percent__c = 100; z.Allocation_Percent__c = 100;
x.Program_Type__c = o.program_type__c; y.Program_Type__c = o.program_type__c;
if (o.program_type__c == 'MMA Balanced') {
x.Allocation_Percent__c = 50; y.Allocation_Percent__c = 50;
x.Program_Type__c = 'MMA mPower'; y.Program_Type__c = 'MMA nSpire';
toInsert.add(x); toInsert.add(y);
} else if (o.program_type__c == 'Bend the Arc JFSJ Note') {
x.Allocation_Percent__c = 40; y.Allocation_Percent__c = 40; z.Allocation_Percent__c = 20;
x.Program_Type__c = 'Affordable Housing'; y.Program_Type__c = 'Small Business'; z.Program_Type__c = 'Education';
toInsert.add(x); toInsert.add(y); toInsert.add(z);
} else if (o.program_type__c == 'Custom Targeted') {
x.Program_Type__c = 'Untargeted';
toInsert.add(x);
} else if (o.program_type__c == 'NPCA Microenterprise Program Note') {
x.Program_Type__c = 'Microfinance';
toInsert.add(x);
} else {
toInsert.add(x);
}
}
}
try {
insert toInsert;
} catch (system.DmlException e) {
system.debug(e);
}
}
if (trigger.isupdate) {
set<id> ids = new set<id>();
list<targeting_allocation__c> toInsert = new list<targeting_allocation__c>();
for(opportunity o: trigger.new) {
if (o.id != null && (o.RecordTypeId=='012a00000018Esx' || o.RecordTypeId == '012300000006GiS' || o.RecordTypeId == '012300000006GiI')) {
if (o.program_type__c != trigger.oldmap.get(o.id).program_type__c) {
ids.add(o.id);
}
}
}
for(opportunity o: [select id, program_type__c from opportunity where id in :ids]) {
targeting_allocation__c x, y, z;
x = new targeting_allocation__c (); y = new targeting_allocation__c (); z = new targeting_allocation__c ();
x.allocation_opportunity__c = o.id; y.allocation_opportunity__c = o.id; z.allocation_opportunity__c = o.id;
x.Allocation_Percent__c = 100; y.Allocation_Percent__c = 100; z.Allocation_Percent__c = 100;
x.Program_Type__c = o.program_type__c; y.Program_Type__c = o.program_type__c;
if (o.program_type__c == 'MMA Balanced') {
x.Allocation_Percent__c = 50; y.Allocation_Percent__c = 50;
x.Program_Type__c = 'MMA mPower'; y.Program_Type__c = 'MMA nSpire';
toInsert.add(x); toInsert.add(y);
} else if (o.program_type__c == 'Bend the Arc JFSJ Note') {
x.Allocation_Percent__c = 40; y.Allocation_Percent__c = 40; z.Allocation_Percent__c = 20;
x.Program_Type__c = 'Affordable Housing'; y.Program_Type__c = 'Small Business'; z.Program_Type__c = 'Education';
toInsert.add(x); toInsert.add(y); toInsert.add(z);
} else if (o.program_type__c == 'Custom Targeted') {
x.Program_Type__c = 'Untargeted';
toInsert.add(x);
} else if (o.program_type__c == 'NPCA Microenterprise Program Note') {
x.Program_Type__c = 'Microfinance';
toInsert.add(x);
} else {
toInsert.add(x);
}
}
try {
delete [select id from targeting_allocation__c where allocation_opportunity__c in :ids];
insert toInsert;
} catch (system.DmlException e) {
system.debug(e);
}
}
}