trigger ReportCPileUp on Report__c (after delete, after insert, after update) {
|
// TODO 今後 Owner_System__c を設定する場合があります。
|
// // LastModifiedDateとCreatedDateの比較での判断は無理、WF経由なので新規時も時間がずれてる
|
// // なのでここで設定するしかないです。
|
// if (Trigger.isBefore) {
|
// for (Report__c rpt : Trigger.new) {
|
// if (Trigger.isInsert || Trigger.isUpdate && Trigger.oldMap.get(rpt.Id).get('Status__c') != '申請中') {
|
// if (rpt.Status__c == '申請中') {
|
// rpt.Submit_report_day__c = Date.today();
|
// }
|
// }
|
// }
|
// } else {
|
// 商談に Num_Of_OPD__c を積み上げ
|
List<String> oppIds = new List<String>();
|
|
RecordType rt = [select Id from RecordType where IsActive = true and DeveloperName = 'OPD' and SObjectType = 'Report__c'];
|
|
if (Trigger.isDelete) {
|
for (Report__c rpt : Trigger.old) {
|
if (rpt.RecordTypeId == rt.Id) {
|
if (String.isBlank(rpt.Opportunity1__c) == false) oppIds.add(rpt.Opportunity1__c);
|
if (String.isBlank(rpt.Opportunity2__c) == false) oppIds.add(rpt.Opportunity2__c);
|
if (String.isBlank(rpt.Opportunity3__c) == false) oppIds.add(rpt.Opportunity3__c);
|
if (String.isBlank(rpt.Opportunity4__c) == false) oppIds.add(rpt.Opportunity4__c);
|
if (String.isBlank(rpt.Opportunity5__c) == false) oppIds.add(rpt.Opportunity5__c);
|
}
|
}
|
}
|
if (Trigger.isInsert) {
|
for (Report__c rpt : Trigger.new) {
|
// OPD
|
if (rpt.RecordTypeId == rt.Id) {
|
if (String.isBlank(rpt.Opportunity1__c) == false)
|
oppIds.add(rpt.Opportunity1__c);
|
if (String.isBlank(rpt.Opportunity2__c) == false)
|
oppIds.add(rpt.Opportunity2__c);
|
if (String.isBlank(rpt.Opportunity3__c) == false)
|
oppIds.add(rpt.Opportunity3__c);
|
if (String.isBlank(rpt.Opportunity4__c) == false)
|
oppIds.add(rpt.Opportunity4__c);
|
if (String.isBlank(rpt.Opportunity5__c) == false)
|
oppIds.add(rpt.Opportunity5__c);
|
}
|
}
|
}
|
if (Trigger.isUpdate) {
|
for (Report__c rpt : Trigger.new) {
|
Report__c oldrpt = Trigger.oldMap.get(rpt.Id);
|
Boolean statusChange = false;
|
if (oldrpt.Status__c != rpt.Status__c) {
|
StatusChange = true;
|
}
|
|
// OPD
|
if (rpt.RecordTypeId == rt.Id) {
|
if (oldrpt.Opportunity1__c != rpt.Opportunity1__c || statusChange) {
|
if (String.isBlank(rpt.Opportunity1__c) == false) {
|
oppIds.add(rpt.Opportunity1__c);
|
}
|
if (oldrpt.Opportunity1__c != null) {
|
oppIds.add(oldrpt.Opportunity1__c);
|
}
|
}
|
if (oldrpt.Opportunity2__c != rpt.Opportunity2__c || statusChange) {
|
if (String.isBlank(rpt.Opportunity2__c) == false) {
|
oppIds.add(rpt.Opportunity2__c);
|
}
|
if (oldrpt.Opportunity2__c != null) {
|
oppIds.add(oldrpt.Opportunity2__c);
|
}
|
}
|
if (oldrpt.Opportunity3__c != rpt.Opportunity3__c || statusChange) {
|
if (String.isBlank(rpt.Opportunity3__c) == false) {
|
oppIds.add(rpt.Opportunity3__c);
|
}
|
if (oldrpt.Opportunity3__c != null) {
|
oppIds.add(oldrpt.Opportunity3__c);
|
}
|
}
|
if (oldrpt.Opportunity4__c != rpt.Opportunity4__c || statusChange) {
|
if (String.isBlank(rpt.Opportunity4__c) == false) {
|
oppIds.add(rpt.Opportunity4__c);
|
}
|
if (oldrpt.Opportunity4__c != null) {
|
oppIds.add(oldrpt.Opportunity4__c);
|
}
|
}
|
if (oldrpt.Opportunity5__c != rpt.Opportunity5__c || statusChange) {
|
if (String.isBlank(rpt.Opportunity5__c) == false) {
|
oppIds.add(rpt.Opportunity5__c);
|
}
|
if (oldrpt.Opportunity5__c != null) {
|
oppIds.add(oldrpt.Opportunity5__c);
|
}
|
}
|
}
|
}
|
}
|
|
if (oppIds.size() > 0) {
|
// 先に親をlockする
|
List<Opportunity> opps = new List<Opportunity>();
|
List<Opportunity> oppList = ControllerUtil.oppSelectForLock(oppIds);
|
|
// 各種カウント
|
Map<Id, Integer> opdCntMap = new Map<Id, Integer>(); // oppId => OPD件数
|
List<Report__c> opdOppList = ControllerUtil.selectReportForAggregateOpdToOpp(oppIds);
|
System.debug('opdOppList.size():::::' + opdOppList.size());
|
for (Report__c rpt : opdOppList) {
|
System.debug('rpt.Opportunity1__c:::::' + rpt.Opportunity1__c);
|
System.debug('rpt.Opportunity2__c:::::' + rpt.Opportunity2__c);
|
System.debug('rpt.Opportunity3__c:::::' + rpt.Opportunity3__c);
|
System.debug('rpt.Opportunity4__c:::::' + rpt.Opportunity4__c);
|
System.debug('rpt.Opportunity5__c:::::' + rpt.Opportunity5__c);
|
if (String.isBlank(rpt.Opportunity1__c) == false) {
|
if (opdCntMap.containsKey(rpt.Opportunity1__c)) {
|
opdCntMap.put(rpt.Opportunity1__c, opdCntMap.get(rpt.Opportunity1__c) + 1);
|
} else {
|
opdCntMap.put(rpt.Opportunity1__c, 1);
|
}
|
} else {
|
opdCntMap.put(rpt.Opportunity1__c, 0);
|
}
|
if (String.isBlank(rpt.Opportunity2__c) == false) {
|
if (opdCntMap.containsKey(rpt.Opportunity2__c)) {
|
opdCntMap.put(rpt.Opportunity2__c, opdCntMap.get(rpt.Opportunity2__c) + 1);
|
} else {
|
opdCntMap.put(rpt.Opportunity2__c, 1);
|
}
|
} else {
|
opdCntMap.put(rpt.Opportunity2__c, 0);
|
}
|
if (String.isBlank(rpt.Opportunity3__c) == false) {
|
if (opdCntMap.containsKey(rpt.Opportunity3__c)) {
|
opdCntMap.put(rpt.Opportunity3__c, opdCntMap.get(rpt.Opportunity3__c) + 1);
|
} else {
|
opdCntMap.put(rpt.Opportunity3__c, 1);
|
}
|
} else {
|
opdCntMap.put(rpt.Opportunity3__c, 0);
|
}
|
if (String.isBlank(rpt.Opportunity4__c) == false) {
|
if (opdCntMap.containsKey(rpt.Opportunity4__c)) {
|
opdCntMap.put(rpt.Opportunity4__c, opdCntMap.get(rpt.Opportunity4__c) + 1);
|
} else {
|
opdCntMap.put(rpt.Opportunity4__c, 1);
|
}
|
} else {
|
opdCntMap.put(rpt.Opportunity4__c, 0);
|
}
|
if (String.isBlank(rpt.Opportunity5__c) == false) {
|
if (opdCntMap.containsKey(rpt.Opportunity5__c)) {
|
opdCntMap.put(rpt.Opportunity5__c, opdCntMap.get(rpt.Opportunity5__c) + 1);
|
} else {
|
opdCntMap.put(rpt.Opportunity5__c, 1);
|
}
|
} else {
|
opdCntMap.put(rpt.Opportunity5__c, 0);
|
}
|
}
|
|
for (Opportunity opp : oppList) {
|
if (opp.Num_Of_OPD__c != (opdCntMap.containsKey(opp.Id) ? opdCntMap.get(opp.Id) : 0)) {
|
opps.add(opp);
|
opp.Num_Of_OPD__c = opdCntMap.containsKey(opp.Id) ? opdCntMap.get(opp.Id) : 0;
|
}
|
}
|
|
if (opps.size() > 0) {
|
StaticParameter.EscapeNFM007Trigger = true;
|
StaticParameter.EscapeOpportunityBefUpdTrigger = true;
|
StaticParameter.EscapeOpportunityHpDeptUpdTrigger = true;
|
StaticParameter.EscapeSyncOpportunityTrigger = true;
|
System.debug('ReportCPileUpから、商談トリガをEscape');
|
System.debug('EscapeNFM007Trigger:::::' + StaticParameter.EscapeNFM007Trigger);
|
System.debug('EscapeOpportunityBefUpdTrigger:::::' + StaticParameter.EscapeOpportunityBefUpdTrigger);
|
System.debug('EscapeOpportunityHpDeptUpdTrigger:::::' + StaticParameter.EscapeOpportunityHpDeptUpdTrigger);
|
System.debug('EscapeSyncOpportunityTrigger:::::' + StaticParameter.EscapeSyncOpportunityTrigger);
|
ControllerUtil.updOppList(opps);
|
}
|
}
|
// }
|
}
|