@isTest private class AccountSetOwnerBatchTest { static testMethod void test_method_one() { //病院 List rectCo = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '病院'] ; if (rectCo.size() == 0) { return; } //戦略科室分類 RecordType rectSct1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 普外科']; RecordType rectSct2 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '戦略科室分類 呼吸科']; //科室分類 RecordType rectDpt1 = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = '診療科 普外科']; RecordType rectDpt2 = [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 comOwner = new User(Test_staff__c = true, LastName = 'com', FirstName = 'owner', Alias = 'com', CommunityNickname = 'comOwner', Email = 'olympus_comowner@sunbridge.com', Username = 'olympus_comowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id); insert comOwner; //战略科室普外科所有人 科室呼吸科所有人 User deptPOwner = new User(Test_staff__c = true, LastName = 'deptP', FirstName = 'owner', Alias = 'deptP', CommunityNickname = 'deptPOwner', Email = 'olympus_deptpowner@sunbridge.com', Username = 'olympus_deptpowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id); insert deptPOwner; //战略科室呼吸科所有人 科室普外科所有人 User deptHOwner = new User(Test_staff__c = true, LastName = 'deptH', FirstName = 'owner', Alias = 'deptH', CommunityNickname = 'deptHOwner', Email = 'olympus_depthowner@sunbridge.com', Username = 'olympus_depthowner@sunbridge.com', IsActive = true, EmailEncodingKey = 'ISO-2022-JP', TimeZoneSidKey = 'Asia/Tokyo', LocaleSidKey = 'ja_JP', LanguageLocaleKey = 'ja', ProfileId = p.id); insert deptHOwner; // 医院 Account company = new Account(); company.RecordTypeId = rectCo[0].Id; company.Name = 'HPテスト1'; company.OwnerId = comOwner.Id; insert company; //战略科室 普外科 Account sct1= [Select Id, Name, Department_Class_Label__c, Hospital__c,OwnerId from Account where Parent.Id = :company.Id and RecordTypeId =: rectSct1.Id] ; sct1.OwnerId = deptPOwner.Id; //战略科室 呼吸科 Account sct2= [Select Id, Name, Department_Class_Label__c, Hospital__c,OwnerId from Account where Parent.Id = :company.Id and RecordTypeId =: rectSct2.Id] ; sct2.OwnerId = deptHOwner.Id; List hp1 = new Account[] {sct1, sct2}; update hp1; //科室 普外科 Account dpt1 = new Account(); dpt1.RecordTypeId = rectDpt1.Id; dpt1.Name = '普外外科'; dpt1.Department_Class__c =sct1.Id; dpt1.ParentId = sct1.Id; dpt1.Department_Name__c = '診療科1'; dpt1.Hospital__c =company.Id; dpt1.OwnerId = deptHOwner.Id; Account dpt2 = new Account(); dpt2.Name = '呼吸科'; dpt2.Hospital__c =company.Id; dpt2.Department_Class__c =sct2.Id; dpt2.Department_Name__c = '診療科2'; dpt2.RecordTypeId = rectDpt2.Id; dpt2.ParentId = sct2.Id; dpt2.OwnerId = deptPOwner.Id; List hp2 = new Account[] {dpt1, dpt2}; insert hp2; Contact con1 = new Contact(); con1.Isactive__c = '有效'; con1.FirstName='san'; con1.LastName='zhang'; con1.Strategic_dept_Class__c = sct1.Id; con1.OwnerId = deptHOwner.Id; insert con1; Contact con2 = new Contact(); con2.Isactive__c = '有效'; con2.FirstName='liu'; con2.LastName='zhao'; con2.Strategic_dept_Class__c = sct2.Id; con2.OwnerId = deptPOwner.Id; insert con2; System.Test.StartTest(); Id execBTId = Database.executeBatch(new AccountSetOwnerBatch(),20); System.Test.StopTest(); dpt1 = [select id,OwnerId from Account where id =:dpt1.id]; dpt2 = [select id,OwnerId from Account where id =:dpt2.id]; con1 = [select id,Name,OwnerId from Contact where id=:con1.id]; con2 = [select id,Name,OwnerId from Contact where id=:con2.id]; system.assertEquals(dpt1.OwnerId, deptPOwner.Id); system.assertEquals(con1.OwnerId, deptPOwner.Id); system.assertEquals(dpt2.OwnerId, deptHOwner.Id); system.assertEquals(con2.OwnerId, deptHOwner.Id); } }