@isTest private class OpportunityHpDeptUpdTriggerTest { static testMethod void testUpdate() { // recode type を取得 List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { return; } List rectSct = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科']; if (rectSct.size() == 0) { return; } List rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc]; if (rectDpt.size() == 0) { return; } // insert Account company1 = new Account(); Account company2 = new Account(); company1.RecordTypeId = rectCo[0].Id; company1.Name = '病院テスト1'; company2.RecordTypeId = rectCo[0].Id; company2.Name = '病院テスト2'; List hps = new Account[] {company1, company2}; insert hps; List dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id order by Department_Class_Label__c]; List dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id order by Department_Class_Label__c]; Account depart1 = new Account(); depart1.RecordTypeId = rectDpt[0].Id; depart1.Name = '*'; depart1.Department_Name__c = '診療科1'; depart1.ParentId = dc1s[0].Id; depart1.Department_Class__c = dc1s[0].Id; depart1.Hospital__c = company1.Id; Account depart2 = new Account(); depart2.RecordTypeId = rectDpt[1].Id; depart2.Name = '*'; depart2.Department_Name__c = '診療科2'; depart2.ParentId = dc2s[1].Id; depart2.Department_Class__c = dc2s[1].Id; depart2.Hospital__c = company2.Id; insert new Account[] {depart1, depart2}; Opportunity opp1 = new Opportunity(Name='testOwner', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today(),AccountId=depart1.Id); Opportunity opp2 = new Opportunity(Name='testOwner2', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today(),AccountId=depart2.Id); insert new Opportunity[] {opp1, opp2}; //まずひとつ追加 //まずひとつ Opportunity oppChanged = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp1.Id ]; System.assertEquals(oppChanged.Department_Class__c, dc1s[0].Id); System.assertEquals(oppChanged.Hospital__c , company1.Id); Opportunity oppChange2 = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp2.Id]; System.assertEquals(oppChange2.Department_Class__c, dc2s[1].Id); System.assertEquals(oppChange2.Hospital__c , company2.Id); opp1.AccountId = depart2.Id; opp2.AccountId = depart1.Id; update new Opportunity[] {opp1, opp2}; //まずひとつ // oppChanged = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp1.Id ]; // System.assertEquals(oppChanged.Department_Class__c, dc2s[1].Id); // System.assertEquals(oppChanged.Hospital__c , company2.Id); // oppChange2 = [Select Id, AccountId, Department_Class__c, Hospital__c from Opportunity where Id = :opp2.Id]; // System.assertEquals(oppChange2.Department_Class__c, dc1s[0].Id); // System.assertEquals(oppChange2.Hospital__c , company1.Id); } @isTest static void testLookupIds() { // OlympusCalendar にデータを追加 OlympusCalendar__c cal11 = new OlympusCalendar__c( Date__c = Date.today() ); OlympusCalendar__c cal12 = new OlympusCalendar__c( Date__c = Date.today().addDays(1) ); OlympusCalendar__c cal13 = new OlympusCalendar__c( Date__c = Date.today().addDays(2) ); OlympusCalendar__c cal14 = new OlympusCalendar__c( Date__c = Date.today().addDays(3) ); insert new OlympusCalendar__c[] {cal11, cal12, cal13, cal14}; // recode type を取得 List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; if (rectCo.size() == 0) { throw(new ControllerUtil.myException('Not Exist RecordType 病院')); } List rectDpt = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN ('診療科 消化科', '診療科 呼吸科') order by Name desc]; if (rectDpt.size() == 0) { throw(new ControllerUtil.myException('Not Exist RecordType 診療科 消化科, 診療科 呼吸科')); } // insert Account company1 = new Account(); Account company2 = new Account(); company1.RecordTypeId = rectCo[0].Id; company1.Name = 'HPテスト1'; company2.RecordTypeId = rectCo[0].Id; company2.Name = 'HPテスト2'; List hps = new Account[] {company1, company2}; insert hps; List dc1s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company1.Id and Department_Class_Label__c = '消化科']; List dc2s = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company2.Id and Department_Class_Label__c = '呼吸科']; Account depart1 = new Account(); depart1.RecordTypeId = rectDpt[0].Id; depart1.Name = '*'; depart1.Department_Name__c = '診療科1'; depart1.ParentId = dc1s[0].Id; depart1.Department_Class__c = dc1s[0].Id; depart1.Hospital__c = company1.Id; Account depart2 = new Account(); depart2.RecordTypeId = rectDpt[1].Id; depart2.Name = '*'; depart2.Department_Name__c = '診療科2'; depart2.ParentId = dc2s[0].Id; depart2.Department_Class__c = dc2s[0].Id; depart2.Hospital__c = company2.Id; insert new Account[] {depart1, depart2}; Opportunity opp1 = new Opportunity(Name='testOwner', Opportunity_Category__c='GI', StageName='contact', CurrencyIsoCode='USD', Close_Forecasted_Date__c = Date.today().addDays(-28), Bid_Planned_Date__c = Date.today().addDays(1), CloseDate=Date.today(), AccountId=depart1.Id); Opportunity opp2 = new Opportunity(Name='testOwner2', Opportunity_Category__c='GS', StageName='contact', CurrencyIsoCode='USD', Close_Forecasted_Date__c = Date.today().addDays(-26), Bid_Planned_Date__c = Date.today().addDays(10), CloseDate=Date.today(), AccountId=depart2.Id); System.Test.startTest(); insert new Opportunity[] {opp1, opp2}; System.Test.stopTest(); opp1 = [Select Calendar_Created_Date_ID__c, CloseDate, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c from Opportunity where Id =: opp1.Id]; System.assertEquals(cal11.Id, opp1.Calendar_Created_Date_ID__c); // 20220518gzw 预测优化fix System.assertEquals(cal13.Date__c, opp1.CloseDate); // Close_Forecasted_Date__c + 30 //System.assertEquals(cal13.Id, opp1.Calendar_Forecast_Shipping_Date_ID__c); System.assertEquals(null, opp1.Calendar_Forecast_Date_ID__c); // System.assertEquals(cal12.Id, opp1.Calendar_Forecast_Tender_Date_ID__c); opp2 = [Select Calendar_Created_Date_ID__c, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c from Opportunity where Id =: opp2.Id]; System.assertEquals(cal11.Id, opp2.Calendar_Created_Date_ID__c); //System.assertEquals(null, opp2.Calendar_Forecast_Shipping_Date_ID__c); // Close_Forecasted_Date__c + 30 System.assertEquals(null, opp2.Calendar_Forecast_Date_ID__c); // System.assertEquals(null, opp2.Calendar_Forecast_Tender_Date_ID__c); // 更新テスト opp1.Opportunity_Category__c = 'GS'; opp1.Calendar_Created_Date_ID__c = cal12.Id; opp1.Close_Forecasted_Date__c = Date.today().addDays(2); opp2.Opportunity_Category__c = 'ET'; opp2.CloseDate = Date.today(); opp2.Bid_Planned_Date__c = Date.today().addDays(2); update new Opportunity[] {opp1, opp2}; opp1 = [Select Calendar_Created_Date_ID__c, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c from Opportunity where Id =: opp1.Id]; System.assertEquals(cal12.Id, opp1.Calendar_Created_Date_ID__c); //System.assertEquals(cal13.Id, opp1.Calendar_Forecast_Shipping_Date_ID__c); System.assertEquals(cal13.Id, opp1.Calendar_Forecast_Date_ID__c); // System.assertEquals(cal12.Id, opp1.Calendar_Forecast_Tender_Date_ID__c); opp2 = [Select Calendar_Created_Date_ID__c, Calendar_Forecast_Shipping_Date_ID__c, Calendar_Forecast_Date_ID__c from Opportunity where Id =: opp2.Id]; System.assertEquals(cal11.Id, opp2.Calendar_Created_Date_ID__c); //System.assertEquals(cal11.Id, opp2.Calendar_Forecast_Shipping_Date_ID__c); System.assertEquals(null, opp2.Calendar_Forecast_Date_ID__c); // System.assertEquals(cal13.Id, opp2.Calendar_Forecast_Tender_Date_ID__c); } }