高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
global class OppMonthlyRatingBatch implements Database.Batchable<sObject> {
    /**
     * コンスタント、パラメータを受け取る
     */
    global OppMonthlyRatingBatch() {
    }
    
    /**
     * startには、queryを実行、商談検索
     */
    global Database.QueryLocator start(Database.BatchableContext BC){
        Date prevDate = Date.today().addMonths(-1);
        Date prevYMD = Date.newInstance(prevDate.year(), prevDate.month(), 1);
        // 前月クローズ/クローズしてない商談は対象
        return Database.getQueryLocator([select Id, Rating__c, Rating01__c, Rating02__c, Rating03__c
                        , Rating04__c, Rating05__c, Rating06__c, Rating07__c, Rating08__c, Rating09__c
                        , Rating10__c, Rating11__c, Rating12__c
                 from Opportunity
                where RecordType.DeveloperName = 'Opportunity'
                  and (Click_Close_Date__c = null or Click_Close_Date__c >= :prevYMD)]) ;
    }
    
    global void execute(Database.BatchableContext BC, List<SObject> sObjList) {
        List<Opportunity> oppList = new List<Opportunity>();
        Integer thisMonth = Date.today().month();
        for (SObject oppObj : sObjList) {
            Opportunity opp = (Opportunity) oppObj;
            if (thisMonth == 1) {
                if (opp.Rating12__c != opp.Rating__c || opp.Rating01__c != null) {
                    opp.Rating12__c = opp.Rating__c;
                    opp.Rating01__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 2) {
                if (opp.Rating01__c != opp.Rating__c || opp.Rating02__c != null) {
                    opp.Rating01__c = opp.Rating__c;
                    opp.Rating02__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 3) {
                if (opp.Rating02__c != opp.Rating__c || opp.Rating03__c != null) {
                    opp.Rating02__c = opp.Rating__c;
                    opp.Rating03__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 4) {
                if (opp.Rating03__c != opp.Rating__c || opp.Rating04__c != null) {
                    opp.Rating03__c = opp.Rating__c;
                    opp.Rating04__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 5) {
                if (opp.Rating04__c != opp.Rating__c || opp.Rating05__c != null) {
                    opp.Rating04__c = opp.Rating__c;
                    opp.Rating05__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 6) {
                if (opp.Rating05__c != opp.Rating__c || opp.Rating06__c != null) {
                    opp.Rating05__c = opp.Rating__c;
                    opp.Rating06__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 7) {
                if (opp.Rating06__c != opp.Rating__c || opp.Rating07__c != null) {
                    opp.Rating06__c = opp.Rating__c;
                    opp.Rating07__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 8) {
                if (opp.Rating07__c != opp.Rating__c || opp.Rating08__c != null) {
                    opp.Rating07__c = opp.Rating__c;
                    opp.Rating08__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 9) {
                if (opp.Rating08__c != opp.Rating__c || opp.Rating09__c != null) {
                    opp.Rating08__c = opp.Rating__c;
                    opp.Rating09__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 10) {
                if (opp.Rating09__c != opp.Rating__c || opp.Rating10__c != null) {
                    opp.Rating09__c = opp.Rating__c;
                    opp.Rating10__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 11) {
                if (opp.Rating10__c != opp.Rating__c || opp.Rating11__c != null) {
                    opp.Rating10__c = opp.Rating__c;
                    opp.Rating11__c = null;
                    oppList.add(opp);
                }
            } else if (thisMonth == 12) {
                if (opp.Rating11__c != opp.Rating__c || opp.Rating12__c != null) {
                    opp.Rating11__c = opp.Rating__c;
                    opp.Rating12__c = null;
                    oppList.add(opp);
                }
            } else {}
        }
        if (oppList.size() > 0) update oppList;
    }
    
    global void finish(Database.BatchableContext BC) {
        // 今回はやることないです
    }
}