@isTest private class AgencyOppUpdHandlerTest { public static Account account1 = new Account(); public static Account account2 = new Account(); public static Account account3 = new Account(); public static Opportunity opp = new Opportunity(); public static Agency_Opportunity__c agency_Opp = new Agency_Opportunity__c(); public static Agency_Hospital_Link__c aHosLink = new Agency_Hospital_Link__c(); public static User user = new User(); public static User user2 = new User(); public static Contact contact2 = new Contact(); public static Contact contact = new Contact(); static testMethod void test_method() { // 取引先 account1.Name = 'test1医院'; account1.RecordTypeId = '01210000000QemG'; insert account1; account2.Name = 'test1经销商'; account2.RecordTypeId = '01210000000Qem1'; insert account2; List accTestList = [SELECT Id, Name FROM Account order by Name]; System.assertEquals(10, accTestList.size()); System.assertEquals('test1医院', accTestList[0].Name); //经销商医院データを作る aHosLink.Name = 'test1经销商医院'; aHosLink.Hospital__c = account1.Id; aHosLink.Agency__c = account2.Id; insert aHosLink; //取引先責任者 contact2.AccountId = account2.Id; contact2.FirstName = '責任者'; contact2.LastName = 'test1经销商'; insert contact2; //System.assertEquals('test1经销商', co.LastName); //ユーザー Profile p = [SELECT Id FROM Profile where Name = '901_经销商周报']; user.ProfileId = p.Id; user.ContactId = contact2.Id; user.FirstName = 'ユーザー'; user.LastName = 'テスト'; user.Email = 'test_user@example.com'; user.emailencodingkey='UTF-8'; user.languagelocalekey='zh_CN'; user.localesidkey='ja_JP'; user.timezonesidkey='Asia/Shanghai'; user.Username = 'test_user@example.com'; user.Alias = 'テユ'; user.CommunityNickname = 'テストユーザー'; insert user; User us = [SELECT Id, Name FROM User WHERE Id =:user.Id]; System.assertEquals('test_user@example.com', user.Email); Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id; agency_Opp.RecordTypeId = rtId; agency_Opp.Name = '引合1'; agency_Opp.Hospital_Target__c = account1.Id; agency_Opp.Agency__c = account2.Id; agency_Opp.Agency_Hospital__c = aHosLink.Id; agency_Opp.StageName__c = '还没申请预算'; agency_Opp.NewOpportunity_Agency_Apply_Status__c = '批准'; //ownerId 設定 or System.runAs(user) agency_Opp.OwnerId = user.Id; //agency_Opp.Opportunity__c = aHosLink.Id; insert agency_Opp; agency_Opp.Agency_Hospital_Target__c = aHosLink.Id; update agency_Opp; opp.Agency_Opportunity__c = agency_Opp.Id; opp.Name = 'test询价'; opp.StageName = '目標'; opp.CloseDate = Date.newInstance(2222, 7, 15); Test.startTest(); insert opp; Oly_TriggerHandler.bypass('AgencyOppUpdHandler'); Oly_TriggerHandler.isBypassed('AgencyOppUpdHandler'); Oly_TriggerHandler.clearBypass('AgencyOppUpdHandler'); Oly_TriggerHandler.clearAllBypasses(); Test.stopTest(); Opportunity opptest = [SELECT Id, Name FROM Opportunity WHERE Id =:opp.Id]; System.assertEquals('::test询价', opptest.Name); } static testMethod void testAfterUpdate() { // 取引先 account1.Name = 'test1医院'; account1.RecordTypeId = '01210000000QemG'; insert account1; account2.Name = 'test1经销商'; account2.RecordTypeId = '01210000000Qem1'; insert account2; List accTestList = [SELECT Id, Name FROM Account order by Name]; System.assertEquals(10, accTestList.size()); System.assertEquals('test1医院', accTestList[0].Name); //经销商医院データを作る aHosLink.Name = 'test1经销商医院'; aHosLink.Hospital__c = account1.Id; aHosLink.Agency__c = account2.Id; insert aHosLink; //取引先責任者 contact2.AccountId = account2.Id; contact2.FirstName = '責任者'; contact2.LastName = 'test1经销商'; insert contact2; //System.assertEquals('test1经销商', co.LastName); //ユーザー Profile p = [SELECT Id FROM Profile where Name = '901_经销商周报']; user.ProfileId = p.Id; user.ContactId = contact2.Id; user.FirstName = 'ユーザー'; user.LastName = 'テスト'; user.Email = 'test_user@example.com'; user.emailencodingkey='UTF-8'; user.languagelocalekey='zh_CN'; user.localesidkey='ja_JP'; user.timezonesidkey='Asia/Shanghai'; user.Username = 'test_user@example.com'; user.Alias = 'テユ'; user.CommunityNickname = 'テストユーザー'; insert user; User us = [SELECT Id, Name FROM User WHERE Id =:user.Id]; System.assertEquals('test_user@example.com', user.Email); Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id; agency_Opp.RecordTypeId = rtId; agency_Opp.Name = '引合1'; agency_Opp.Hospital_Target__c = account1.Id; agency_Opp.Agency__c = account2.Id; agency_Opp.Agency_Hospital__c = aHosLink.Id; agency_Opp.StageName__c = '还没申请预算'; //ownerId 設定 or System.runAs(user) agency_Opp.OwnerId = user.Id; //agency_Opp.Opportunity__c = aHosLink.Id; agency_Opp.NewOpportunity_Agency_Apply_Status__c = '批准'; insert agency_Opp; agency_Opp.Agency_Hospital_Target__c = aHosLink.Id; update agency_Opp; opp.Agency_Opportunity__c = agency_Opp.Id; opp.Name = 'test询价'; opp.StageName = '目標'; //opp.AgencyOpp_FromOCM__c = true; opp.SAP_Send_OK__c = false; opp.CloseDate = Date.newInstance(2222, 7, 15); Test.startTest(); insert opp; opp.SAP_Send_OK__c = true; Test.stopTest(); Opportunity opptest = [SELECT Id, Name FROM Opportunity WHERE Id =:opp.Id]; System.assertEquals('::test询价', opptest.Name); } //Opportunity Owner変更 OCSM_Owner__cも変更 static testMethod void set_AgencyOpportunity_OCSMOwnerTest() { // 取引先 account1.Name = 'test1医院'; account1.RecordTypeId = '01210000000QemG'; account2.Name = 'test1经销商'; account2.RecordTypeId = '01210000000Qem1'; Id AgencyRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByDeveloperName().get('Agency').getRecordTypeId(); account3.Name = 'test2经销商'; account3.RecordTypeId = AgencyRecordTypeId; insert new List {account1, account2, account3}; list aaa = [select Id from Account where Hospital_Department_Class__c =:account1.Id]; System.assertEquals(aaa.size(), 8); // 取引先責任者 contact.AccountId = account1.Id; contact.FirstName = '責任者'; contact.LastName = 'test1医院'; contact2.AccountId = account2.Id; contact2.FirstName = '責任者'; contact2.LastName = 'test1经销商'; insert new List {contact,contact2}; //System.assertEquals('test1经销商', co.LastName); //ユーザー Profile p = [SELECT Id FROM Profile where Name = '901_经销商周报']; user.ProfileId = p.Id; user.ContactId = contact2.Id; user.FirstName = 'ユーザー'; user.LastName = 'テスト'; user.Email = 'test_user@example.com'; user.emailencodingkey='UTF-8'; user.languagelocalekey='zh_CN'; user.localesidkey='ja_JP'; user.timezonesidkey='Asia/Shanghai'; user.Username = 'test_user@example.com'; user.Alias = 'テユ'; user.CommunityNickname = 'テストユーザー'; Profile adminP1 = [select Id from Profile where id =:System.Label.ProfileId_SystemAdmin]; // ユーザー作成 User user2 = 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 = adminP1.id); insert new List {user, user2}; //经销商医院データを作る aHosLink.Name = 'test1经销商医院'; aHosLink.Hospital__c = account1.Id; aHosLink.Agency__c = account2.Id; aHosLink.OwnerId = user.Id; insert aHosLink; Id rtId = [select Id,DeveloperName from RecordType where IsActive = true and SobjectType = 'Agency_Opportunity__c' and DeveloperName = 'Opportunity'].Id; agency_Opp.RecordTypeId = rtId; agency_Opp.Name = '引合1'; agency_Opp.Department_Cateogy__c = 'GI'; agency_Opp.Hospital_Target__c = account1.Id; agency_Opp.Agency__c = account2.Id; agency_Opp.Agency_Hospital__c = aHosLink.Id; agency_Opp.StageName__c = '还没申请预算'; //ownerId 設定 or System.runAs(user) agency_Opp.OwnerId = user.Id; agency_Opp.Agency_Hospital_Target__c = aHosLink.Id; agency_Opp.OCM_Change_To_Opportunity_Flg__c = true; agency_Opp.NewOpportunity_Agency_Apply_Status__c = '批准'; insert agency_Opp; agency_Opp.NewOpportunity_Agency_Apply_Status__c = '填写完毕'; update agency_Opp; opp.Name = 'test询价'; opp.StageName = '目標'; opp.SAP_Send_OK__c = false; opp.CloseDate = Date.newInstance(2222, 7, 15); opp.SAP_Send_OK__c = true; insert opp; Test.startTest(); //updAgencyOpportunityMap opp.Agency_Opportunity__c = agency_Opp.Id; update opp; agency_Opp.NewOpportunity_Agency_Apply_Status__c = '批准'; agency_Opp.Change_To_Opportunity__c = opp.Id; update agency_Opp; //Owner変更 opp.owner_not_automatically_update__c = true; opp.OwnerId = user2.Id; update opp; Test.stopTest(); Agency_Opportunity__c agencyopportunity = [SELECT Id,Name,OCSM_Owner__c,Change_To_Opportunity__c, Department_Class_Opp__c FROM Agency_Opportunity__c WHERE Id =: agency_Opp.Id]; System.assertNotEquals(agencyopportunity.Department_Class_Opp__c, null); System.assertEquals(user2.Id, opp.OwnerId); System.assertEquals(user2.Id, agencyopportunity.OCSM_Owner__c); String rowCause = Schema.Agency_Opportunity__Share.RowCause.OCSM_Owner_c_User__c; List agencyopportunityShare = [SELECT RowCause,ParentId,UserOrGroupId FROM Agency_Opportunity__Share WHERE ParentId = :agencyopportunity.Id AND RowCause = :rowCause]; System.assertEquals(1, agencyopportunityShare.size()); System.assertEquals('OCSM_Owner_c_User__c', agencyopportunityShare[0].RowCause); System.assertEquals(user2.Id, agencyopportunityShare[0].UserOrGroupId); } }