-
Notifications
You must be signed in to change notification settings - Fork 8
/
StateFul_BatchClass
129 lines (108 loc) · 4.86 KB
/
StateFul_BatchClass
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
*************************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
*************************************************************************************************
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>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,10); //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>, Database.Stateful{
integer RecordProcessed=0;
integer RecordFailed=0;
list<Opportunity> lstOpPr= new list<Opportunity>();
//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);
string USDrate= USDCurr.substringAfter('USD');
string USDfRate =USDrate.SubStringBefore(')');
string FinalUSD=USDfRate;
op.USD__c=FinalUSD;
if(op.USD__c!=Null && op.USD__c!=''){
RecordProcessed=RecordProcessed+1;
lstOpPr.add(op);
lstOpp.add(op);
}
else{
RecordFailed=RecordFailed+1;
}}
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);
system.debug('@@@@ Records processed: '+RecordProcessed);
system.debug('@@@@ Records failed: '+RecordFailed);
system.debug('@@@@ lstOpPr: '+lstOpPr);
system.debug('@@@@ lstOpPr size: '+lstOpPr.size());
}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%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;
BatchOpportunityCurrency bc = new BatchOpportunityCurrency();
Database.executebatch(bc);
//update op;
system.assertEquals(op.StageName,'Qualification');
test.stopTest();
}catch(exception ex){
}}
@isTest public static void TestMethod2(){
try{
Opportunity op=[select Id, Name,CloseDate,StageName,Multi_currecy__c,USD__c from Opportunity Limit 1];
test.startTest();
op.StageName='Qualification';
op.Multi_currecy__c='AUD 20,930.00 (EUD 32,11.90)';
update op;
BatchOpportunityCurrency bc = new BatchOpportunityCurrency();
Database.executebatch(bc);
//update op;
system.assertEquals(op.StageName,'Qualification');
test.stopTest();
}catch(exception ex){
}
}
}