-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpportunityTriggerHandler.cls
69 lines (57 loc) · 3.02 KB
/
OpportunityTriggerHandler.cls
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
/********************************************************************************************************************
* @ClassName : OpportunityTriggerHandler
* @Description : Handler class for Opportunity Trigger
********************************************************************************************************************/
public class OpportunityTriggerHandler {
public static Map<Id,Exchange_Rate__c> exchangeRatesIdVsRateMap = new Map<Id,Exchange_Rate__c>();
/**
* @Name : getOpportunityToUpdateOnInsert
* @Purpose : Get opportunities to be updated
* @opportunities : List of opportunities
* @return : List<Opportunity>
*/
public static List<Opportunity> getOpportunityToUpdateOnInsert(List<Opportunity> opportunities){
List<Opportunity> oppToUpdate = new List<Opportunity>();
for(Opportunity opp : opportunities){
exchangeRatesIdVsRateMap.put(opp.Exchange_Rate__c, null);
}
return opportunities;
}
/**
* @Name : getOpportunitiesToUpdate
* @Purpose : Get opportunities to be updated
* @oldOpportunityMap : Map of old opportunity data
* @opportunityMap : Map of new opportunity data
* @return : List<Opportunity>
*/
public static List<Opportunity> getOpportunitiesToUpdate(Map<Id, Opportunity> oldOpportunityMap, Map<Id, Opportunity> opportunityMap){
List<Opportunity> oppToUpdate = new List<Opportunity>();
for(ID opportunityId : opportunityMap.keySet()){
Opportunity opp = opportunityMap.get(opportunityId);
if(oldOpportunityMap != null
&& oldOpportunityMap.containsKey(opportunityId)
&& !oldOpportunityMap.get(opportunityId).StageName.contains('Closed')){
exchangeRatesIdVsRateMap.put(opp.Exchange_Rate__c, null);
oppToUpdate.add(opp);
}
}
return oppToUpdate;
}
/**
* @Name : updateRateDataOnOpportunity
* @Purpose : Update Amount fields using selected target currency Rates
* @opportunities : List of opportunities to update
* @return : void
*/
public static void updateRateDataOnOpportunity(List<Opportunity> oppToUpdate){
exchangeRatesIdVsRateMap = new Map<Id, Exchange_Rate__c>([select Id, Rate__c
from Exchange_Rate__c
where Id in : exchangeRatesIdVsRateMap.keySet()
limit 50000]);
for(Opportunity opp : oppToUpdate){
if(exchangeRatesIdVsRateMap.keySet().contains(opp.Exchange_Rate__c)){
opp.Exchange_Rate_Value__c = exchangeRatesIdVsRateMap.get(opp.Exchange_Rate__c).Rate__c;
}
}
}
}