在 Salesforce 中测试 Class 的触发器
Test Class in Salesforce for a Trigger
大家好,
我正在尝试为我帮助编写的触发器编写测试 class。触发器使用一个名为 trigger_help__c 的字段,这是一个从添加机会类型和帐户 ID 派生的公式字段,如果在过去 90 天内在该帐户上创建了该类型的机会,则触发器会在插入之前触发。除非配置文件是系统管理员。这是我的触发器:
trigger leadDuplicatePreventer on opportunity(before insert) {
set<string> settgs = new set<string>();
list<opportunity> opps = [select id,Trigger_Help__c from opportunity WHERE CreatedDate = LAST_N_DAYS:90];
Profile p=[SELECT ID, Name FROM Profile WHERE Id=:userinfo.getProfileId() Limit 1];
for(opportunity opp : opps){
if(opp.Trigger_Help__c != null && p.Name <> 'System Administrator'){
settgs.add(opp.Trigger_Help__c);
}
}
for(opportunity op : trigger.new){
if(settgs.contains(op.Trigger_Help__c)){
op.adderror('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.');
}
}
}
我在编写测试时遇到了问题 class,而且我一如既往地一无所知。我写了以下内容,但我不知道我实际需要做什么:
@isTest
private class TestleadDuplicatePreventer {
@isTest static void TestleadDuplicatePreventerwithOneOpp() {
Account acct = new Account(Name='Test Account');
insert acct;
Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(1),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
AccountID=acct.Id);
insert opp;
Test.startTest();
opp= new Opportunity(Name='Opportunity Test',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(2),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
Account=[SELECT ID from Account WHERE Account.Name='Test Account']);
try
{
insert opp;
}
catch(Exception duplicate)
{
}
Test.stopTest();
}
}
感谢任何帮助!!
不确定您对项目的确切要求,您可能无需代码就可以完成此操作,而无需执行汇总总和字段,该字段计算与 [= 中使用的类型的帐户相关的机会34=] 然后验证当 ISNEW() if Account.Count_Of_Type__c > 0 导致触发验证或机会创建一个隐藏字段,该字段是唯一的,它是帐户 ID 和机会类型的串联,可以由工作流或流程构建器设置。这将防止为给定帐户添加重复类型。
但对于您最初的问题:在您的测试中,我没有看到您设置了 Trigger_Help__c 字段,除非它是由自动化设置的。此外,在创建第二个 opp 时,您不需要查询帐户,只需使用代码前面的 acct.Id。
对于您的捕获,您需要断言 e.getMessage() 是您期望从触发器中的添加错误中得到的消息。
您可能还需要单独执行 运行As(admin_user) 操作,其中 admin_user 是您使用系统管理员配置文件创建的用户,以确保您不会当您 运行 作为管理员用户时出现错误,如下所示:
@isTest
private class TestleadDuplicatePreventer {
@isTest static void TestleadDuplicatePreventerwithOneOpp() {
Account acct = new Account(Name='Test Account');
insert acct;
Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(1),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
AccountID=acct.Id);
insert opp;
Test.startTest();
opp= new Opportunity(Name='Opportunity Test',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(2),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
AccountId=acct.Id);
try
{
insert opp;
}
catch(Exception duplicate)
{
System.assertEquals('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.', duplicate.getMessage())
}
//this should probably be in a separate test method
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com');
System.runAs(u2) {
insert opp;
}
Test.stopTest();
}
}
大家好,
我正在尝试为我帮助编写的触发器编写测试 class。触发器使用一个名为 trigger_help__c 的字段,这是一个从添加机会类型和帐户 ID 派生的公式字段,如果在过去 90 天内在该帐户上创建了该类型的机会,则触发器会在插入之前触发。除非配置文件是系统管理员。这是我的触发器:
trigger leadDuplicatePreventer on opportunity(before insert) {
set<string> settgs = new set<string>();
list<opportunity> opps = [select id,Trigger_Help__c from opportunity WHERE CreatedDate = LAST_N_DAYS:90];
Profile p=[SELECT ID, Name FROM Profile WHERE Id=:userinfo.getProfileId() Limit 1];
for(opportunity opp : opps){
if(opp.Trigger_Help__c != null && p.Name <> 'System Administrator'){
settgs.add(opp.Trigger_Help__c);
}
}
for(opportunity op : trigger.new){
if(settgs.contains(op.Trigger_Help__c)){
op.adderror('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.');
}
}
}
我在编写测试时遇到了问题 class,而且我一如既往地一无所知。我写了以下内容,但我不知道我实际需要做什么:
@isTest
private class TestleadDuplicatePreventer {
@isTest static void TestleadDuplicatePreventerwithOneOpp() {
Account acct = new Account(Name='Test Account');
insert acct;
Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(1),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
AccountID=acct.Id);
insert opp;
Test.startTest();
opp= new Opportunity(Name='Opportunity Test',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(2),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
Account=[SELECT ID from Account WHERE Account.Name='Test Account']);
try
{
insert opp;
}
catch(Exception duplicate)
{
}
Test.stopTest();
}
}
感谢任何帮助!!
不确定您对项目的确切要求,您可能无需代码就可以完成此操作,而无需执行汇总总和字段,该字段计算与 [= 中使用的类型的帐户相关的机会34=] 然后验证当 ISNEW() if Account.Count_Of_Type__c > 0 导致触发验证或机会创建一个隐藏字段,该字段是唯一的,它是帐户 ID 和机会类型的串联,可以由工作流或流程构建器设置。这将防止为给定帐户添加重复类型。
但对于您最初的问题:在您的测试中,我没有看到您设置了 Trigger_Help__c 字段,除非它是由自动化设置的。此外,在创建第二个 opp 时,您不需要查询帐户,只需使用代码前面的 acct.Id。
对于您的捕获,您需要断言 e.getMessage() 是您期望从触发器中的添加错误中得到的消息。
您可能还需要单独执行 运行As(admin_user) 操作,其中 admin_user 是您使用系统管理员配置文件创建的用户,以确保您不会当您 运行 作为管理员用户时出现错误,如下所示:
@isTest
private class TestleadDuplicatePreventer {
@isTest static void TestleadDuplicatePreventerwithOneOpp() {
Account acct = new Account(Name='Test Account');
insert acct;
Opportunity opp = new Opportunity(Name=acct.Name + ' Opportunity',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(1),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
AccountID=acct.Id);
insert opp;
Test.startTest();
opp= new Opportunity(Name='Opportunity Test',
StageName='Open Opportunity',
CloseDate=System.today().addMonths(2),
Facility__c='Tacoma WA',
Oppty_Type__c='UCO Service',
AccountId=acct.Id);
try
{
insert opp;
}
catch(Exception duplicate)
{
System.assertEquals('An Opportunity of this type already exists on this Account. Please contact a system administrator with questions.', duplicate.getMessage())
}
//this should probably be in a separate test method
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
User u2 = new User(Alias = 'newUser', Email='newuser@testorg.com',
EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
LocaleSidKey='en_US', ProfileId = p.Id,
TimeZoneSidKey='America/Los_Angeles', UserName='newuser@testorg.com');
System.runAs(u2) {
insert opp;
}
Test.stopTest();
}
}