@isTest private class UserViolationsTriggerTest { @isTest static void test_method_one() { List dealerIE = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer']; if (dealerIE.size() == 0) { return; } List olyCompany = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'OlympusCompany']; if (olyCompany.size() == 0) { return; } List loa_individual = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_个体管理']; if (loa_individual.size() == 0) { return; } List loa_number = [select Id from RecordType where IsActive = true and SobjectType = 'Asset' and Name = '样机_数量管理']; if (loa_number.size() == 0) { return; } List rectLapp = [select Id from RecordType where IsActive = true and SobjectType = 'loaner_application__c' and Name = 'BS']; if (rectLapp.size() == 0) { return; } //代理商用户 List rectDealer = [select Id from RecordType where IsActive = true and SobjectType = 'Account' and Name = 'Dealer']; Profile prof = [select Id from Profile where Name ='SSBG DEALER BS']; Account dealer = new Account(); dealer.Name = 'test dealer'; dealer.RecordTypeId = rectDealer[0].Id; dealer.ProductSegment__c = 'BS'; dealer.PostCode__c='000000'; insert dealer; Contact core = new Contact(email='jplumber@salesforce.com', firstname='Joe',lastname='Plumber',accountid=dealer.id); insert core; User testUser = New User(ContactId = core.id,Alias = 'newUser',Email='newuser@testorg.com',EmailEncodingKey='UTF-8', LastName='TestUser', LanguageLocaleKey='zh_CN',LocaleSidKey='zh_CN', ProfileId = prof.Id,TimeZoneSidKey='Asia/Shanghai', UserName='testUser01@prectech.com'); insert testUser; //样机客户 Account acc = new Account(); acc.Name = 'test dealer'; acc.RecordTypeId = olyCompany[0].Id; acc.ProductSegment__c = 'BS'; acc.PostCode__c='000000'; insert acc; Loaner_Set__c loanerSet = new loaner_Set__c(); loanerSet.Equipment_Type__c = 'BS'; loanerSet.Set_No__c = 'IETEST0001'; loanerSet.Name = 'IETEST0001'; insert loanerSet; // 产品 Product2 prd1 = new Product2(); prd1.Product_ECCode__c = 'testSBG001'; prd1.ProductCode = 'testSBG001'; prd1.Name = 'testSBG001'; prd1.IsActive = true; Product2 prd2 = new Product2(); prd2.Product_ECCode__c = 'testSBG002'; prd2.ProductCode = 'testSBG002'; prd2.Name = 'testSBG002'; prd2.IsActive = true; insert new Product2[] {prd1, prd2}; // 保有设备 (数量管理) Asset asset = new Asset(); asset.RecordTypeId = loa_number[0].Id; asset.SerialNumber = 'asset'; asset.Name = 'asset'; asset.AccountId = acc.Id; asset.Product2Id = prd2.Id; asset.Quantity = 1; asset.Status = '在库'; asset.Equipment_Type__c = 'BS'; asset.loaner_place__c = '广州办'; asset.total_number__c = 20; asset.Remarks2__c = 'asset'; // 保有设备 (个体管理) Asset assetC1 = new Asset(); assetC1.RecordTypeId = loa_individual[0].Id; assetC1.SerialNumber = 'assetC1'; assetC1.Name = 'assetC1'; assetC1.AccountId = acc.Id; assetC1.Product2Id = prd1.Id; assetC1.Quantity = 1; assetC1.Status = '在库'; assetC1.Equipment_Type__c = 'BS'; assetC1.loaner_place__c = '广州办'; assetC1.Remarks2__c = 'assetC1'; // 保有设备 (数量管理) Asset assetC2 = new Asset(); assetC2.RecordTypeId = loa_number[0].Id; assetC2.SerialNumber = 'assetC2'; assetC2.Name = 'assetC2'; assetC2.AccountId = acc.Id; assetC2.Product2Id = prd2.Id; assetC2.Quantity = 1; assetC2.Status = '在库'; assetC2.Equipment_Type__c = 'BS'; assetC2.loaner_place__c = '广州办'; assetC2.total_number__c = 20; assetC2.Remarks2__c = 'asset'; insert new Asset[] {asset,assetC1,assetC2}; Loaner_Set_Detail__c lSD1 = new Loaner_Set_Detail__c(); lSD1.Loaner_Set__c = loanerSet.Id; lSD1.Asset__c = asset.Id; Loaner_Set_Detail__c lSD2 = new Loaner_Set_Detail__c(); lSD2.Loaner_Set__c = loanerSet.Id; lSD2.Asset__c = assetC1.Id; insert new Loaner_Set_Detail__c[] {lSD1,lSD2}; loaner_application__c loanerApp = new loaner_application__c(); loanerApp.Name = 'TESTYJ0001'; loanerApp.Approval_Step__c = 1; loanerApp.Status__c = '申请中'; loanerApp.Loaner_Ser__c = 'test'; loanerApp.Equipment_Type__c = 'BS'; loanerApp.RecordTypeId = rectLapp[0].Id; loanerApp.Demo_purpose__c = '全国性展会'; loanerApp.Request_shipping_Date__c = Date.today().addDays(1); loanerApp.Request_return_Date__c = Date.today().addDays(5); loanerApp.ownerId = testUser.Id; loanerApp.ProductNameNum1__c = prd2.Id; loanerApp.pickup_time__c = Date.today(); insert loanerApp; User_Violations__c uv = new User_Violations__c(); uv.Is_locking__c = true; uv.Is_effective__c = true; uv.Locking_Start_Date__c = Date.today(); uv.Violations_User__c =testUser.Id; uv.Violations_Loaner__c = loanerApp.id; insert uv; User_Violations__c uv1 = new User_Violations__c(); uv1.Is_locking__c = false; uv1.Is_effective__c = true; uv1.Locking_Start_Date__c = Date.today(); uv1.Violations_User__c =testUser.Id; uv1.Violations_Loaner__c = loanerApp.id; insert uv1; uv.Is_locking__c = false; uv.Is_effective__c = false; system.runAs(testUser){ update uv; } } }