@isTest private class OpportunityBefInsUpdTriggerTest { private static Product2 product = null; private static Id pricebookId = ControllerUtil.getStandardPricebook().Id; static { product = new Product2( Name='テスト商品'); insert product; } private static PricebookEntry buildPB( String input) { PricebookEntry entry = new PricebookEntry( Pricebook2Id=pricebookId, Product2Id=product.Id); entry.UnitPrice = 0; entry.IsActive = true; entry.UseStandardPrice = false; entry.CurrencyIsoCode = input; insert entry; return entry; } private static Opportunity buildOppInstance( String inputTrade, String CurrencyIsoCode) { Opportunity target = new Opportunity( Name='aiueo', StageName='引合', CloseDate=Date.today()); target.Trade__c = inputTrade; target.CurrencyIsoCode = CurrencyIsoCode; return target; } private static void insertOppLineItem( Opportunity input, Pricebookentry entry) { OpportunityLineItem target = new OpportunityLineItem(); target.OpportunityId = input.Id; target.Quantity = 1; target.UnitPrice = 100; target.PricebookEntryId = entry.Id; insert target; } static testMethod void test10() { PricebookEntry entry = buildPB( 'USD'); List opps = new List(); opps.add( buildOppInstance( '外貿', 'USD')); insert opps; insertOppLineItem( opps[0], entry); } static testMethod void test01() { // 商談を複数レコード insertしてみます List opps = new List(); // テストデータ生成 opps.add( buildOppInstance( '外貿', 'USD')); opps.add( buildOppInstance( '外貿', 'CNY')); opps.add( buildOppInstance( '内貿', 'USD')); opps.add( buildOppInstance( '内貿', 'CNY')); insert opps; // 結果発表 List selected = [SElECT Id, Name, Trade__c, CurrencyIsoCode FROM Opportunity WHERE Id IN :opps]; System.assertEquals( '外貿', selected[0].Trade__c); System.assertEquals( 'USD', selected[0].CurrencyIsoCode); System.assertEquals( '外貿', selected[1].Trade__c); System.assertEquals( 'USD', selected[1].CurrencyIsoCode); System.assertEquals( '内貿', selected[2].Trade__c); System.assertEquals( 'CNY', selected[2].CurrencyIsoCode); System.assertEquals( '内貿', selected[3].Trade__c); System.assertEquals( 'CNY', selected[3].CurrencyIsoCode); // データを更新してみる selected[0].Trade__c = '内貿'; selected[0].CurrencyIsoCode = 'USD'; selected[1].Trade__c = '内貿'; selected[1].CurrencyIsoCode = 'CNY'; selected[2].Trade__c = '外貿'; selected[2].CurrencyIsoCode = 'USD'; selected[3].Trade__c = '外貿'; selected[3].CurrencyIsoCode = 'CNY'; update selected; // 結果発表 selected = [SElECT Id, Name, Trade__c, CurrencyIsoCode FROM Opportunity WHERE Id IN :opps]; System.assertEquals( '内貿', selected[0].Trade__c); System.assertEquals( 'CNY', selected[0].CurrencyIsoCode); System.assertEquals( '内貿', selected[1].Trade__c); System.assertEquals( 'CNY', selected[1].CurrencyIsoCode); System.assertEquals( '外貿', selected[2].Trade__c); System.assertEquals( 'USD', selected[2].CurrencyIsoCode); System.assertEquals( '外貿', selected[3].Trade__c); System.assertEquals( 'USD', selected[3].CurrencyIsoCode); // 子レコードを作ってみる PricebookEntry entryUSD = buildPB( 'USD'); PricebookEntry entryCNY = buildPB( 'CNY'); insertOppLineItem( selected[0], entryCNY); insertOppLineItem( selected[1], entryCNY); insertOppLineItem( selected[2], entryUSD); insertOppLineItem( selected[3], entryUSD); // データを更新してみる selected = [SElECT Id, Name, Trade__c, CurrencyIsoCode FROM Opportunity WHERE Id IN :opps]; selected[0].Trade__c = '外貿'; selected[0].CurrencyIsoCode = 'CNY'; selected[1].Trade__c = '外貿'; // selected[1].CurrencyIsoCode = 'USD'; selected[2].Trade__c = '内貿'; // selected[2].CurrencyIsoCode = 'CNY'; selected[3].Trade__c = '内貿'; selected[3].CurrencyIsoCode = 'USD'; update selected; } static testMethod void testOwner() { Opportunity opp = new Opportunity(Name='testOwner', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today()); insert opp; opp = [select id, ownerid, Owner_System__c from opportunity where id = :opp.id]; system.assertequals(opp.ownerid, opp.Owner_System__c); opp.Owner_System__c = null; update opp; opp = [select id, ownerid, Owner_System__c from opportunity where id = :opp.id]; system.assertequals(opp.ownerid, opp.Owner_System__c); } static testMethod void testCity() { RecordType rt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; RecordType rt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科']; RecordType rt3 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科']; Address_Level__c al1 = new Address_Level__c( Name = '四川省' ); insert al1; Address_Level2__c al2 = new Address_Level2__c( Name = '成都市', Address_Level__c = al1.Id ); insert al2; Account company = new Account(); company.Name = 'aiueo病院'; company.RecordTypeId = rt1.Id; company.State_Master__c = al1.Id; company.City_Master__c = al2.Id; insert company; Account sct = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company.Id and RecordTypeId = :rt2.Id]; Account depart = new Account(); depart.RecordTypeId = rt3.Id; depart.Name = '*'; depart.Department_Name__c = 'aiueo診療科'; depart.ParentId = sct.Id; depart.Department_Class__c = sct.Id; depart.Hospital__c = company.Id; insert depart; Opportunity opp = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), AccountId = depart.Id); insert opp; opp = [select City__c, City_Text__c from Opportunity where Id = :opp.Id]; System.assertequals('成都市', opp.City_Text__c); System.assertequals('成都市', opp.City__c); } static testMethod void testShippingDate() { Opportunity opp = new Opportunity(Name='testOwner', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today(),Shipping_Finished_Day__c =Date.today().addDays(-2)); insert opp; opp = [select id, ownerid, Owner_System__c,ShippingDate_For_Report__c,StageName,Shipping_Finished_Day__c from opportunity where id = :opp.id]; System.assertEquals('contact',opp.StageName); System.assertEquals(Date.today(),opp.ShippingDate_For_Report__c); System.assertEquals(Date.today().addDays(-2),opp.Shipping_Finished_Day__c); opp.StageName = '出荷'; opp.CloseDate = Date.today().addDays(+1); update opp; opp = [select id, ownerid, Owner_System__c,ShippingDate_For_Report__c,StageName,Shipping_Finished_Day__c from opportunity where id = :opp.id]; System.assertEquals('出荷',opp.StageName); System.assertEquals(Date.today().addDays(-2),opp.Shipping_Finished_Day__c); System.assertEquals(Date.today().addDays(-2),opp.ShippingDate_For_Report__c); } static testMethod void testQuote() { Opportunity opp = new Opportunity(Name='testOwner', StageName='contact', CurrencyIsoCode='CNY', CloseDate=Date.today(),Shipping_Finished_Day__c =Date.today().addDays(-2)); insert opp; Quote q = new Quote( Name = 'quote', OpportunityId = opp.Id ); insert q; opp = [select id, ownerid, Owner_System__c,ShippingDate_For_Report__c,StageName,Shipping_Finished_Day__c from opportunity where id = :opp.id]; System.assertEquals('contact',opp.StageName); System.assertEquals(Date.today(),opp.ShippingDate_For_Report__c); System.assertEquals(Date.today().addDays(-2),opp.Shipping_Finished_Day__c); opp.StageName = '引合'; opp.CloseDate = Date.today().addDays(+1); opp.Estimation_Id__c = q.id; opp.Estimation_Decision__c = true; opp.SAP_Send_OK__c = true; opp.Authorized_DB_No__c = '11111'; opp.Contract_DB_SalesDept_complite_day__c = Date.today(); opp.NotesApprovedNo__c = '11111'; update opp; opp = [select id, ownerid, Owner_System__c,ShippingDate_For_Report__c,StageName,Shipping_Finished_Day__c from opportunity where id = :opp.id]; System.assertEquals('引合',opp.StageName); System.assertEquals(Date.today().addDays(-2),opp.Shipping_Finished_Day__c); //System.assertEquals(Date.today().addDays(-2),opp.ShippingDate_For_Report__c); } static testMethod void testConsumable() { RecordType rt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約']; RecordType rt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店']; //RecordType rt3 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科']; Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin]; // ユーザー作成 User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id); insert hpOwner; Address_Level__c al1 = new Address_Level__c( Name = '四川省' ); insert al1; Address_Level2__c al2 = new Address_Level2__c( Name = '成都市', Address_Level__c = al1.Id ); insert al2; Account company = new Account(name='aiueo经销商',RecordTypeId = rt2.Id,State_Master__c = al1.Id,City_Master__c = al2.Id); insert company; Account contact = new Account(name='aiueo契约',RecordTypeId = rt1.Id,SpecialDealerName__c = 'aiueo经销商',ParentId = company.Id,Agent_Ref__c = company.Id,Delete_Flag__c = false,Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),Contract_Decide_End_Date__c = Date.today().addDays(1)); insert contact; contact.ownerId = hpOwner.Id; update contact; contact = [select ownerId from Account WHERE Id = :contact.Id]; System.assertequals(contact.ownerId, hpOwner.Id); Opportunity opp = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), AccountId = contact.Id); insert opp; opp = [select ownerid,Account.RecordType.DeveloperName, City__c, City_Text__c,Custmor_pic_ID__c from Opportunity where Id = :opp.Id]; System.assertequals(hpOwner.Id, opp.Custmor_pic_ID__c); System.assertequals(hpOwner.Id, opp.ownerid); } static testMethod void testConsumableupdate() { RecordType rt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '契約']; RecordType rt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '販売店']; Profile p = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin]; // ユーザー作成 User hpOwner = new User(Test_staff__c = true, LastName = 'hp', FirstName = 'owner', Alias = 'hp', CommunityNickname = 'hpOwner', Email = 'olympus_hpowner@sunbridge.com', Username = 'olympus_hpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id); insert hpOwner; Address_Level__c al1 = new Address_Level__c( Name = '四川省' ); insert al1; Address_Level2__c al2 = new Address_Level2__c( Name = '成都市', Address_Level__c = al1.Id ); insert al2; Account company = new Account(name='aiueo经销商',RecordTypeId = rt2.Id,State_Master__c = al1.Id,City_Master__c = al2.Id); insert company; Account contact = new Account(name='aiueo契约',RecordTypeId = rt1.Id,SpecialDealerName__c = 'aiueo经销商',ParentId = company.Id,Agent_Ref__c = company.Id,Delete_Flag__c = false,Contract_Department_Class__c = 'ET',Contract_Decide_Start_Date__c = Date.today().addDays(-1),Contract_Decide_End_Date__c = Date.today().addDays(1)); insert contact; System.Test.startTest(); Opportunity opp = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), AccountId = contact.Id); insert opp; contact.ownerId = hpOwner.Id; update contact; contact = [select ownerId from Account WHERE Id = :contact.Id]; System.assertequals(hpOwner.Id, contact.ownerId); opp.Name = 'aiueo商談1'; update opp; opp = [select ownerid,Account.RecordType.DeveloperName, City__c, City_Text__c,Custmor_pic_ID__c from Opportunity where Id = :opp.Id]; System.assertequals(hpOwner.Id, opp.Custmor_pic_ID__c); System.assertequals(hpOwner.Id, opp.ownerid); System.Test.StopTest(); } static testMethod void testHosptil() { RecordType rt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院']; RecordType rt2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科']; RecordType rt3 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 呼吸科']; Address_Level__c al1 = new Address_Level__c( Name = '四川省' ); insert al1; Address_Level2__c al2 = new Address_Level2__c( Name = '成都市', Address_Level__c = al1.Id ); insert al2; Account company = new Account(); company.Name = 'aiueo病院'; company.RecordTypeId = rt1.Id; company.State_Master__c = al1.Id; company.City_Master__c = al2.Id; insert company; Account sct = [Select Id, Name, Department_Class_Label__c, Sys_Dept_Name_Change_Chk__c from Account where Parent.Id = :company.Id and RecordTypeId = :rt2.Id]; Account depart = new Account(); depart.RecordTypeId = rt3.Id; depart.Name = '*'; depart.Department_Name__c = 'aiueo診療科'; depart.ParentId = sct.Id; depart.Department_Class__c = sct.Id; depart.Hospital__c = company.Id; insert depart; Opportunity opp = new Opportunity(Name = 'aiueo商談', StageName = 'contact', CloseDate=Date.today(), AccountId = depart.Id,Hospital__c = company.Id); insert opp; opp = [select ownerid,owner_not_automatically_update__c, City__c, City_Text__c,Custmor_pic_ID__c from Opportunity where Id = :opp.Id]; System.assertequals('00510000005sEEM', opp.ownerid); System.assertequals(false, opp.owner_not_automatically_update__c); System.assertequals('00510000005sEEM', opp.Custmor_pic_ID__c); System.assertequals('成都市', opp.City_Text__c); System.assertequals('成都市', opp.City__c); } //付晓坤 2021-1-15 测试:询价借用备品提醒修改客户将影响回库 Start static testMethod void testOpportunityUpdate(){ // Opportunity opIds = [Select Id From Opportunity]; //Rental_Apply__c ra = [Select Id,Follow_UP_Opp__c From Rental_Apply__c Where Status__c != '取消' and Follow_UP_Opp__c IN :opIds]; //询价的创建 //1、记录类型 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 from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 消化科']; if (rectDpt.size() == 0) { return; } //2.创建医院 Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'NFM007TestCompany'; upsert company; //3.创建科室 Account section = new Account(); section.RecordTypeId = rectSct[0].Id; section.Name = '*'; section.Department_Class_Label__c = '消化科'; section.ParentId = company.Id; section.Hospital_Department_Class__c = company.Id; upsert section; //4.创建部门 Account depart = new Account(); depart.RecordTypeId = rectDpt[0].Id; depart.Name = '*'; depart.Department_Name__c = 'NFM007TestDepart'; depart.ParentId = section.Id; depart.Department_Class__c = section.Id; depart.Hospital__c = company.Id; upsert depart; //5.创建询价(业务机会) Opportunity opp = new Opportunity(); opp.AccountId = depart.Id;//客户名 opp.Department_Class__c = section.Id;//战略科室分类 opp.Hospital__c = company.Id;//医院 opp.SAP_Send_OK__c = false;//SAP上传(WIN) opp.Name = 'GZ-SP-NFM007_1';//业务机会名 opp.Trade__c = '内貿';//内贸/外贸 opp.StageName = '引合';//阶段 opp.CloseDate = Date.today();// 结束日期 opp.Stock_apply_status__c = '申请中';//备货申请状态 insert opp; // 创建备品 //1.取OPD的Id OPDPlan__c opd = new OPDPlan__c(); opd.Account_Laboratory__c = depart.Id; opd.OPDPlan_ImplementDate__c = Date.today()+60; opd.NoOpp_Reason__c = 'HCP对应'; opd.Status__c = '计划中'; insert opd; //2.添加一条状态为取消的备品 /*Rental_Apply__c ra = new Rental_Apply__c(); ra.Follow_UP_Opp__c = opp.Id; ra.Status__c = '取消'; ra.Cancel_Reason__c = '主动取消'; ra.Name = 'aaa'; ra.Person_In_Charge__c= UserInfo.getUserId(); ra.applyUser__c = UserInfo.getUserId(); ra.Demo_purpose1__c = '产品试用'; ra.demo_purpose2__c = '试用(无询价)'; ra.Product_category__c = 'GI'; ra.Demo_purpose_text__c = '111'; ra.Request_shipping_day__c = date.newinstance(2022, 11, 30); ra.Loaner_received_staff__c = 'www'; ra.direct_send__c = '上门自提'; ra.pickup_time__c = date.newinstance(2022, 11, 30); ra.Loaner_received_staff_phone__c = '12312312312'; ra.OPDPlan__c = opd.Id; ra.Account__c = depart.Id; ra.Strategic_dept__c = section.Id; ra.Hospital__c = company.Id;//医院 insert ra;*/ //2.1 添加一条状态不为取消的备品 /*Rental_Apply__c ra1 = new Rental_Apply__c(); ra1.Follow_UP_Opp__c = opp.Id; ra1.Status__c = '草案中'; ra1.Name = 'aaa'; ra1.Person_In_Charge__c= UserInfo.getUserId(); ra1.applyUser__c = UserInfo.getUserId(); ra1.Demo_purpose1__c = '产品试用'; ra1.demo_purpose2__c = '试用(无询价)'; ra1.Product_category__c = 'GI'; ra1.Demo_purpose_text__c = '111'; ra1.Request_shipping_day__c = Date.today(); ra1.Loaner_received_staff__c = 'www'; ra1.direct_send__c = '上门自提'; ra1.pickup_time__c = Date.today(); ra1.Loaner_received_staff_phone__c = '12312312312'; ra1.OPDPlan__c = opd.Id; ra1.Account__c = depart.Id; ra1.Strategic_dept__c = section.Id; ra1.Hospital__c = company.Id;//医院 insert ra1;*/ //更新询价客户名 //1、记录类型 List rectSct1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 泌尿科']; if (rectSct.size() == 0) { return; } List rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 泌尿科']; if (rectDpt.size() == 0) { return; } //2.创建科室 Account section1 = new Account(); section1.RecordTypeId = rectSct1[0].Id; section1.Name = 'aaa'; section1.Department_Class_Label__c = '泌尿科'; section1.ParentId = company.Id; section1.Hospital_Department_Class__c = company.Id; upsert section1; //3.创建部门 Account depart1 = new Account(); depart1.RecordTypeId = rectDpt1[0].Id; depart1.Name = 'aaa'; depart1.Department_Name__c = 'NFM006TestDepart'; depart1.ParentId = section1.Id; depart1.Department_Class__c = section1.Id; depart1.Hospital__c = company.Id; upsert depart1; //4.修改客户 opp.AccountId = depart1.Id;//客户名 update opp; //测试 List raList = [Select Id,Follow_UP_Opp__c From Rental_Apply__c Where Status__c != '取消' and Follow_UP_Opp__c = :opp.Id]; System.assertEquals(0, raList.size()); } //付晓坤 2021-1-15 测试:询价借用备品提醒修改客户将影响回库 End }