@isTest private class SetPersonalTargetBatchTest { //20200618 为SetPersonalTargetBatch添加整个test class byljh static private String currentPeriod(Integer i) { Date dateNow = Date.today(); Integer year = dateNow.year(); Integer month = dateNow.month(); if (month < 4) { year -= 1; } return String.valueOf(year + i - 1867 + 'P'); } static private Date oppCloseDate() { Date nowDate = Date.today(); Integer year = nowDate.year(); Integer month = nowDate.month(); if (month < 4) { nowDate = nowDate.addYears(-1); } return nowDate; } // 主流程 @isTest static void myUnitTest() { // システム管理者 User u3 = new User(Test_staff__c = true); u3.LastName = '_サンブリッジ'; u3.FirstName = 'う'; u3.Alias = 'う'; u3.Email = 'olympusTest03@sunbridge.com'; u3.Username = 'olympusTest03@sunbridge.com'; u3.CommunityNickname = 'う'; u3.IsActive = true; u3.EmailEncodingKey = 'ISO-2022-JP'; u3.TimeZoneSidKey = 'Asia/Tokyo'; u3.LocaleSidKey = 'ja_JP'; u3.LanguageLocaleKey = 'ja'; u3.ProfileId = System.Label.ProfileId_SystemAdmin; u3.Job_Category__c = '销售推广'; u3.Province__c = '上海市上海市'; u3.Use_Start_Date__c = Date.today().addMonths(-6); insert u3; System.runAs(u3) { Oly_TriggerHandler.bypass('PowerBIBaseHandler'); Product2 product = new Product2( Name = 'テスト商品', Important_Rroduct_1GI__c = true, Important_Rroduct_2GI__c = true, Important_Rroduct_3GI__c = true, Important_Rroduct_4GI__c = true, Important_Rroduct_5GI__c = true ); insert product; Pricebook2 pricebook = ControllerUtil.getStandardPricebook(); PricebookEntry entry = new PricebookEntry( Pricebook2Id = pricebook.Id, Product2Id = product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = 'CNY'; insert entry; RecordType[] rt = [select Id from RecordType where SobjectType = 'Opportunity' and IsActive = true and DeveloperName = 'Target']; Profile p = [select id from Profile where id = :System.Label.ProfileId_SystemAdmin]; Map> proportion = new Map>(); String strObjectiveProportionGI = System.Label.ObjectiveProportionGI; List objectiveProportionGI = strObjectiveProportionGI.split(','); List doubleGI = new List(); for (String strGI : objectiveProportionGI) { doubleGI.add(Double.valueOf(strGI)); } proportion.put('GI', doubleGI); User u1 = new User(Test_staff__c = false); u1.LastName = '_サンブリッジ7'; u1.FirstName = 'あ'; u1.Alias = 'あ9'; u1.Email = 'olympusTest01@sunbridge.com'; u1.Username = 'olympusTest91@sunbridge.com'; u1.CommunityNickname = 'あ9'; u1.IsActive = true; u1.EmailEncodingKey = 'ISO-2022-JP'; u1.TimeZoneSidKey = 'Asia/Tokyo'; u1.LocaleSidKey = 'ja_JP'; u1.LanguageLocaleKey = 'ja'; u1.ProfileId = System.Label.ProfileId_SystemAdmin; u1.Job_Category__c = '销售推广'; u1.Province__c = '上海市上海市'; u1.Post__c = '一般'; u1.Sales_Speciality__c = '产品担当'; u1.Use_Start_Date__c = Date.today().addMonths(-24); insert u1; User u2 = new User(Test_staff__c = false); u2.LastName = '_サンブリッジ6'; u2.FirstName = 'い'; u2.Alias = 'い'; u2.Email = 'olympusTest02@sunbridge.com'; u2.Username = 'olympusTest02@sunbridge.com'; u2.CommunityNickname = 'い'; u2.IsActive = true; u2.EmailEncodingKey = 'ISO-2022-JP'; u2.TimeZoneSidKey = 'Asia/Tokyo'; u2.LocaleSidKey = 'ja_JP'; u2.LanguageLocaleKey = 'ja'; u2.ProfileId = System.Label.ProfileId_SystemAdmin; u2.Job_Category__c = '销售推广'; u2.Province__c = '上海市上海市'; u2.Post__c = '一般'; u2.Sales_Speciality__c = '产品担当'; u2.Use_Start_Date__c = Date.today().addMonths(-24); insert u2; // 插入 中间表 Num_Major_Product 的两个数据 list TestData = new list(); Date dateNow = Date.today(); Integer year = dateNow.year(); Amount_Major_Product__c testdatum1 = new Amount_Major_Product__c(); string key = u1.Id + '_' + 'GI'; testdatum1.key__c = key; testdatum1.Amount__c = 100.00; testdatum1.user_Alias__c = u1.Alias; testdatum1.Use_Start_Date__c = u1.Use_Start_Date__c; testdatum1.SAP_Province__c = u1.Province__c; testdatum1.iYear__c = year; testdatum1.Is_Processing__c = true; TestData.add(testdatum1); insert(TestData); TestData = [select id from Amount_Major_Product__c]; System.Test.StartTest(); Id execBTId = Database.executeBatch(new SetPersonalTargetBatch(), 10); System.Test.StopTest(); // 设置既存opportunities数据 Integer y = year; Integer m = 4 ; String syear = String.valueOf(y); String smonth = String.valueOf(m); if (m < 10) { smonth = '0' + smonth; } String sTagetDay = syear + '-' + smonth + '-01'; Date tagetDay = Date.valueOf(sTagetDay); list Opportunities = new list(); Opportunity newopp = new Opportunity(); newopp.Name = u2.Alias + ' ' + '重点产品01' ; newopp.StageName = '目標'; newopp.OwnerId = u2.Id; // トリガをスルーのため、ここでやります newopp.Owner_System__c = u2.Id; newopp.Opportunity_Category__c = 'GI'; String tem_depName = ('GI' == null || 'GI' == '') ? 'GI' : 'GI'; newopp.Proportion__c = proportion.get(tem_depName)[0]; newopp.CloseDate = tagetDay; newopp.Amount = 100.00; newopp.Target_category__c = '担当重点产品目标'; newopp.Important_Key_product_category__c = '重点产品01'; newopp.SAP_Province__c = u2.Province__c; newopp.RecordTypeId = rt[0].ID; newopp.OCM_Target_period__c = currentPeriod(0); upsert newopp; Amount_Major_Product__c testdatum2 = new Amount_Major_Product__c(); key = u2.Id + '_' + 'GI'; testdatum2.key__c = key; testdatum2.Amount__c = 333.00; testdatum2.user_Alias__c = u2.Alias; testdatum2.Use_Start_Date__c = u2.Use_Start_Date__c; testdatum2.SAP_Province__c = u2.Province__c; testdatum2.iYear__c = year; testdatum2.Is_Processing__c = true; insert(testdatum2); execBTId = Database.executeBatch(new SetPersonalTargetBatch(), 10); Opportunities = [select Name, StageName, Owner_System__c, Opportunity_Category__c, Important_Key_product_category__c from Opportunity]; system.assertEquals(13, Opportunities.size()); } } }