/**
|
* 保有機材に項目履歴ができるようになる
|
*/
|
// 2023/03/17 LHJ 新建设备时不再创建历史 Start
|
//trigger MakeAssetHistory on Asset (after insert, after update, after delete) {
|
trigger MakeAssetHistory on Asset (after update, after delete) {
|
|
//deloitte-zhj 20231124 本地化导入 start
|
if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
|
return;
|
}
|
//deloitte-zhj 20231124 本地化导入 end
|
|
// 2023/03/17 LHJ 新建设备时不再创建历史 End
|
//add by gzw 20270718 start
|
if (StaticParameter.EscapeVMCTrigger) {
|
return ;
|
}
|
//add by gzw 20270718 end
|
List<String> accIdList = new List<String>();
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
|
List<Id> userIdList = new List<String>();
|
List<String> newInspection_report_number_List = new List<String>();
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
|
|
if (Trigger.isDelete) {
|
OFSInsReportAssetHistoryController.delAssetHistory();
|
} else {
|
String columnstr = System.Label.AssetHistory_Columns;
|
List<String> columnList = columnstr.split(',');
|
Schema.DescribeSObjectResult assetDesc = Asset.SObjectType.getDescribe();
|
Map<String, Schema.SObjectField> fsMap = assetDesc.fields.getMap();
|
List<AssetHistory__c> ahList = new List<AssetHistory__c>();
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
|
List<String> ahDelExtIdList = new List<String>();
|
List<AssetHistory__c> ahUpsertList = new List<AssetHistory__c>();
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
|
for (Asset a : Trigger.new) {
|
// 2023/03/17 LHJ 新建设备时不再创建历史 Start
|
/* if (Trigger.isInsert) {
|
Map<String, AssetHistory__c> historyMap = OFSInsReportAssetHistoryController.astHistoryMap.get(a.Id);
|
if (historyMap == null || historyMap.containsKey('Id') == false) {
|
AssetHistory__c ah = new AssetHistory__c();
|
ah.AssetId__c = a.Id;
|
ah.Operator__c = System.Userinfo.getUserId();
|
ah.Field_Api_Name__c = 'Id';
|
ah.Field_Lable_Name__c = 'Id';
|
ahList.add(ah);
|
if (historyMap == null) {
|
historyMap = new Map<String, AssetHistory__c>();
|
OFSInsReportAssetHistoryController.astHistoryMap.put(a.Id, historyMap);
|
}
|
historyMap.put('Id', ah);
|
}
|
} else { */
|
// 2023/03/17 LHJ 新建设备时不再创建历史 End
|
Map<String, AssetHistory__c> historyMap = OFSInsReportAssetHistoryController.astHistoryMap.get(a.Id);
|
for (String colApiName : columnList) {
|
colApiName = colApiName.trim();
|
if (colApiName == 'Inspection_report_number__c') {
|
// 定義しないはず、念のため、ここでcontinue;
|
continue;
|
}
|
if (Trigger.oldMap.get(a.Id).get(colApiName) != Trigger.newMap.get(a.Id).get(colApiName)) {
|
AssetHistory__c ah = new AssetHistory__c();
|
if (historyMap == null || historyMap.containsKey(colApiName) == false) {
|
if (colApiName == 'AccountId') {
|
accIdList.add(String.valueOf(Trigger.oldMap.get(a.Id).get(colApiName)));
|
accIdList.add(String.valueOf(Trigger.newMap.get(a.Id).get(colApiName)));
|
}
|
ah.AssetId__c = a.Id;
|
ah.Operator__c = System.Userinfo.getUserId();
|
ah.Field_Api_Name__c = colApiName;
|
ah.Field_Lable_Name__c = fsMap.get(colApiName).getDescribe().getLabel();
|
ah.OldValue__c = fsMap.get(colApiName).getDescribe().getType().name() == 'Date' ? (Trigger.oldMap.get(a.Id).get(colApiName) == null ? '' : ((Date) Trigger.oldMap.get(a.Id).get(colApiName)).format()) : String.valueOf(Trigger.oldMap.get(a.Id).get(colApiName));
|
ah.NewValue__c = fsMap.get(colApiName).getDescribe().getType().name() == 'Date' ? (Trigger.newMap.get(a.Id).get(colApiName) == null ? '' : ((Date) Trigger.newMap.get(a.Id).get(colApiName)).format()) : String.valueOf(Trigger.newMap.get(a.Id).get(colApiName));
|
if (historyMap == null) {
|
historyMap = new Map<String, AssetHistory__c>();
|
OFSInsReportAssetHistoryController.astHistoryMap.put(a.Id, historyMap);
|
}
|
historyMap.put(colApiName, ah);
|
} else {
|
// newValueを更新
|
ah = historyMap.get(colApiName);
|
if (colApiName == 'AccountId') {
|
accIdList.add(String.valueOf(Trigger.newMap.get(a.Id).get(colApiName)));
|
}
|
ah.NewValue__c = fsMap.get(colApiName).getDescribe().getType().name() == 'Date' ? (Trigger.newMap.get(a.Id).get(colApiName) == null ? '' : ((Date) Trigger.newMap.get(a.Id).get(colApiName)).format()) : String.valueOf(Trigger.newMap.get(a.Id).get(colApiName));
|
}
|
ahList.add(ah);
|
}
|
}
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
|
// 更新の場合、点検の履歴はupsertにします、1レコードにまとめます
|
// upserMapのキーは Inspection_report_number__c です。
|
Map<String, AssetHistory__c> historyUpsertMap = OFSInsReportAssetHistoryController.astHistoryUpsertMap.get(a.Id);
|
Boolean isNeedUpsert = false;
|
Boolean isNeedDel = false;
|
//System.debug('old Inspection_report_number__c' + Trigger.oldMap.get(a.Id).get('Inspection_report_number__c'));
|
//System.debug('new Inspection_report_number__c' + a.Inspection_report_number__c);
|
if (Trigger.oldMap.get(a.Id).get('Inspection_report_number__c') != a.Inspection_report_number__c
|
&& isNeedUpsert == false) {
|
isNeedUpsert = true;
|
if (String.isBlank(a.Inspection_report_number__c)) {
|
isNeedDel = true;
|
}
|
}
|
if (Trigger.oldMap.get(a.Id).get('Inspection_Result__c') != a.Inspection_Result__c
|
&& isNeedUpsert == false) {
|
isNeedUpsert = true;
|
}
|
if (Trigger.oldMap.get(a.Id).get('Last_Inspection_staff__c') != a.Last_Inspection_staff__c
|
&& isNeedUpsert == false) {
|
isNeedUpsert = true;
|
}
|
if (Trigger.oldMap.get(a.Id).get('Final_Examination_Date__c') != a.Final_Examination_Date__c
|
&& isNeedUpsert == false) {
|
isNeedUpsert = true;
|
}
|
if (Trigger.oldMap.get(a.Id).get('Inspection_Comment__c') != a.Inspection_Comment__c
|
&& isNeedUpsert == false) {
|
isNeedUpsert = true;
|
}
|
if (isNeedDel) {
|
ahDelExtIdList.add(a.Id + '_' + Trigger.oldMap.get(a.Id).get('Inspection_report_number__c'));
|
} else if (isNeedUpsert
|
&& !String.isBlank(a.Inspection_report_number__c) && a.Inspection_report_number__c != 'OCM' && a.Inspection_report_number__c != 'OCM-'
|
&& a.Inspection_report_number__c != 'OCSM' && a.Inspection_report_number__c != 'OCSM-' // CHAN-BBLATV
|
) {
|
AssetHistory__c ah = new AssetHistory__c();
|
if (historyUpsertMap == null || historyUpsertMap.containsKey('Inspection_report_number__c') == false) {
|
newInspection_report_number_List.add(a.Inspection_report_number__c);
|
System.debug('newInspection_report_number_List.add:' + a.Inspection_report_number__c);
|
ah.AssetId__c = a.Id;
|
ah.Operator__c = System.Userinfo.getUserId();
|
ah.Asset_Inspection_ExtID__c = a.Id + '_' + a.Inspection_report_number__c;
|
ah.NewValue__c = a.Inspection_report_number__c;
|
if (a.Inspection_report_number__c == a.Inspection_Report__r.Name) {
|
ah.Inspection_Report__c = a.Inspection_Report__c;
|
}
|
ah.Field_Api_Name__c = 'Inspection_report_number__c';
|
ah.Field_Lable_Name__c = fsMap.get('Inspection_report_number__c').getDescribe().getLabel();
|
if (historyUpsertMap == null) {
|
historyUpsertMap = new Map<String, AssetHistory__c>();
|
OFSInsReportAssetHistoryController.astHistoryUpsertMap.put(a.Id, historyUpsertMap);
|
}
|
} else {
|
// newValueを更新
|
ah = historyUpsertMap.get('Inspection_report_number__c').clone();
|
}
|
historyUpsertMap.put('Inspection_report_number__c', ah);
|
ahUpsertList.add(ah);
|
ah.Account__c = a.AccountId;
|
ah.AssetId__c = a.Id;
|
ah.Asset_Inspection_ExtID__c = a.Id + '_' + a.Inspection_report_number__c;
|
ah.NewValue__c = a.Inspection_report_number__c;
|
if (a.Inspection_report_number__c == a.Inspection_Report__r.Name) {
|
ah.Inspection_Report__c = a.Inspection_Report__c;
|
}
|
if (String.isBlank(a.Inspection_Result__c) == false) {
|
ah.Inspection_Result__c = a.Inspection_Result__c;
|
}
|
if (String.isBlank(a.Last_Inspection_staff__c) == false) {
|
ah.Inspection_name__c = a.Last_Inspection_staff__c;
|
userIdList.add(a.Last_Inspection_staff__c);
|
}
|
if (a.Final_Examination_Date__c != null) {
|
ah.Examination_Date__c = a.Final_Examination_Date__c;
|
}
|
if (String.isBlank(a.Inspection_Comment__c) == false) {
|
ah.Inspection_Comment__c = a.Inspection_Comment__c;
|
}
|
}
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
|
//}
|
}
|
// AccountIdなど参照項目の場合Nameなどをセット
|
if (accIdList.size() > 0) {
|
Map<Id, Account> accMap = new Map<Id, Account> ([select Id, Name, Management_Code__c from Account where Id IN: accIdList]);
|
for (AssetHistory__c ah : ahList) {
|
if (ah.Field_Api_Name__c == 'AccountId') {
|
if (ah.OldValue__c.length() >= 15 && accMap.get(ah.OldValue__c) != null) ah.OldValue__c = accMap.get(ah.OldValue__c).Management_Code__c;
|
if (ah.NewValue__c.length() >= 15 && accMap.get(ah.NewValue__c) != null) ah.NewValue__c = accMap.get(ah.NewValue__c).Management_Code__c;
|
}
|
}
|
}
|
OFSInsReportAssetHistoryController.upsertAssetHistory(ahList);
|
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu start
|
// upsert用の場合、Userを逆引き
|
if (userIdList.size() > 0) {
|
Map<Id, User> userMap = new Map<Id, User> ([select Id, Alias, Province__c from User where Id IN: userIdList]);
|
for (AssetHistory__c ah : ahUpsertList) {
|
if (!String.isBlank(ah.Inspection_name__c)) {
|
ah.Inspection_Staff_State__c = userMap.get(ah.Inspection_name__c) == null ? '' : userMap.get(ah.Inspection_name__c).Province__c;
|
ah.Inspection_staff__c = userMap.get(ah.Inspection_name__c) == null ? '' : userMap.get(ah.Inspection_name__c).Alias;
|
}
|
}
|
}
|
OFSInsReportAssetHistoryController.upsertExtAssetHistory(ahUpsertList);
|
|
// 最後削除する
|
OFSInsReportAssetHistoryController.delAssetHistoryExtIdList(ahDelExtIdList);
|
|
// newInspection_report_number_List の分の中1つだけ、1にする
|
if (newInspection_report_number_List.size() > 0) {
|
List<AssetHistory__c> ahListFlag = new List<AssetHistory__c>();
|
String breakInspection = '';
|
Boolean isUpdateFlag = true;
|
System.debug('newInspection_report_number_List=' + newInspection_report_number_List);
|
List<AssetHistory__c> ahs = [Select Id, Inspection_report_number__c, Inspection_Times__c from AssetHistory__c where NewValue__c != null and NewValue__c IN :newInspection_report_number_List and Field_Api_Name__c = 'Inspection_report_number__c' order by NewValue__c];
|
for(AssetHistory__c ah : ahs) {
|
System.debug('ah.Inspection_report_number__c=' + ah.Inspection_report_number__c);
|
if (breakInspection != ah.Inspection_report_number__c) {
|
System.debug('breakInspection != ah.Inspection_report_number__c');
|
isUpdateFlag = true;
|
breakInspection = ah.Inspection_report_number__c;
|
if (ah.Inspection_Times__c != 1) {
|
ahListFlag.add(ah); // まずは処理対象
|
} else {
|
isUpdateFlag = false;
|
}
|
}
|
if (isUpdateFlag) {
|
if (ah.Inspection_Times__c == 1) {
|
isUpdateFlag = false;
|
System.debug('ah.Inspection_Times__c != 0');
|
ahListFlag.remove(ahListFlag.size() - 1);
|
}
|
}
|
}
|
if (ahListFlag.size() > 0) {
|
System.debug('ahListFlag.size()=' + ahListFlag.size());
|
for (AssetHistory__c ah : ahListFlag) {
|
ah.Inspection_Times__c = 1;
|
}
|
update ahListFlag;
|
}
|
}
|
// 20150119 TODO 削除 点検ロジックは 点検報告書にて実装 by katsu end
|
}
|
}
|