-
Notifications
You must be signed in to change notification settings - Fork 8
/
CallBatchApex_FromTrigger_BreakString
133 lines (105 loc) · 4.71 KB
/
CallBatchApex_FromTrigger_BreakString
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
*************************Problem Statement*****************************************************
When StageName is changing, you have to:
1. break the field Value e.g. giving in format AUD 20,930.00 (USD 32,11.90)
2. And put 32,11.90 this in different field
Approach:
1. Writing trigger
2. Calling batch Apex from Trigger to perform this operation
Explaination: https://youtu.be/sqDNn-rF2WE
*************************************************************************************************
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Trigger<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
trigger ExtractCurr on Opportunity (before Update) {
try{
if(trigger.isExecuting && trigger.isBefore && trigger.isUpdate){
for(Opportunity op:trigger.new){
if(op.StageName!=trigger.oldmap.get(op.Id).StageName){
//Calling batch class from Trigger
BatchOpportunityCurrency btOpCu= new BatchOpportunityCurrency();
Database.executebatch(btOpCu,200); //controlling batch size
}
}
}
}catch(exception ex){
system.debug('@@@@ ex.getLineNumber: '+ex.getLineNumber());
system.debug('@@@@ ex.getLineNumber: '+ex.getMessage());
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Batch Class<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
public class BatchOpportunityCurrency implements Database.Batchable<Sobject>{
//start method
public Database.QueryLocator start(Database.BatchableContext bc){
return database.getQueryLocator('select Id, Multi_currecy__c, USD__c from Opportunity');
}
//execute method
public void execute(Database.BatchableContext bc, list<Opportunity>records){
list<Opportunity> lstOpp= new list<Opportunity>();
for(Opportunity op:records){
string USDCurr = string.ValueOf(op.Multi_currecy__c); //current value AUD 20,930.00 (USD 32,11.90)
string USDrate= USDCurr.substringAfter('USD');
string USDfRate =USDrate.SubStringBefore(')');
string FinalUSD=USDfRate;
op.USD__c=FinalUSD; //Value now inside field: 32,11.90
lstOpp.add(op);
}
update lstOpp;
}
//finish method
public void finish (Database.BatchableContext bc){
AsyncApexJob job=[SELECT Id, CreatedDate, CreatedById, JobType, ApexClassId, Status, JobItemsProcessed,
TotalJobItems, NumberOfErrors, CompletedDate, MethodName,
ExtendedStatus FROM AsyncApexJob where Id=:bc.getJobId()] ;
system.debug('@@@@ JobId: '+bc.getJobId());
system.debug('@@@@ job: '+job);
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Test Class<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@isTest
public class TestBatchOpportunityCurrency {
@TestSetUp public static void CreateData(){
try{
Opportunity opp = new Opportunity();
opp.Name='testOp';
opp.CloseDate=system.today();
opp.StageName='Prospecting';
opp.Multi_currecy__c='AUD 20,930.00 (USD 32,11.90)';
insert opp;
}catch(exception ex){}
}
@isTest public static void TestMethod1(){
try{
Opportunity op=[select Id, Name,CloseDate,StageName,Multi_currecy__c,USD__c from Opportunity Limit 1];
test.startTest();
op.StageName='Qualification';
update op;
system.assertEquals(op.StageName,'Qualification');
test.stopTest();
}catch(exception ex){
}}
}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Test class in case not called from trigger:
@isTest
public class TestBatchOpportunityCurrency {
@TestSetUp public static void CreateData(){
try{
Opportunity opp = new Opportunity();
opp.Name='testOp';
opp.CloseDate=system.today();
opp.StageName='Prospecting';
opp.Multi_currecy__c='AUD 20,930.00 (USD 32,11.90)';
insert opp;
}catch(exception ex){}
}
@isTest public static void TestMethod1(){
try{
Opportunity op=[select Id, Name,CloseDate,StageName,Multi_currecy__c,USD__c from Opportunity Limit 1];
test.startTest();
op.StageName='Qualification';
update op;
BatchOpportunityCurrency bc = new BatchOpportunityCurrency();
Database.executebatch(bc);
system.assertEquals(op.StageName,'Qualification');
test.stopTest();
}catch(exception ex){
}}
}