global class OppMonthlyRatingBatch implements Database.Batchable { /** * コンスタント、パラメータを受け取る */ 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 sObjList) { List oppList = new List(); 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) { // 今回はやることないです } }