我有一个简单的触发器,当我的自定义对象上的SD_Action__c字段是某个值时,它应该创建一个新的机会。代码没有错误,但是当我尝试更新沙箱或生产中的字段时没有任何反应。在m.SD_Action__c=='Generate Opportunity'
时,我错过了什么才能成为一个很好的机会?
trigger MDwinning on MD_Meeting__c (after update) {
List <Opportunity> oppToInsert = new List <Opportunity> ();
for (MD_Meeting__c m : Trigger.new) {
if (m.SD_Action__c == 'Generate Opportunity') {
Opportunity o = new Opportunity ();
// o.Owner = m.Sales_Director__c,
o.Market_Developer__c = m.Market_Developer__c;
//o.Account = m.Account__c;
oppToInsert.add(o);
}//end if
}//end for o
try {
insert oppToInsert;
} catch (system.Dmlexception e) {
system.debug (e);
}
}
这是我的测试类:
@isTest (SeeAllData = true)
public with sharing class MDwinningTest {
static testMethod void MDwinningTest() {
MD_Meeting__c m = new MD_Meeting__c(
Account__c = 'test Account',
Desired_Meeting__c = 'Call',
Name = 'Meeting name',
Sales_Director__c = 'SD Name',
Market_Developer__c = 'MD Name',
Meeting_Date__c = Date.today(),
Contact__c = 'Test Contact',
Title__c = 'Boss',
Functional_Role__c = 'eCommerce - VP',
Contact_Email__c = 'test@aol.com',
SD_Action__c = 'Generate Opportunity',
Primary_URL__c = 'http://www.google.com/'
);
insert m;
}
}
答案 0 :(得分:0)
由于您仅使用insert
操作,您的测试无法正常运行,但您的触发器可在update
模式下运行。因此,您必须更改测试或触发器或两者
@isTest
private class MDwinningTest {
@isTest
static void MDwinningTest() {
// prepare test data
MD_Meeting__c m = new MD_Meeting__c(
Account__c = 'test Account',
Desired_Meeting__c = 'Call',
Name = 'Meeting name',
Sales_Director__c = 'SD Name',
Market_Developer__c = 'MD Name',
Meeting_Date__c = Date.today(),
Contact__c = 'Test Contact',
Title__c = 'Boss',
Functional_Role__c = 'eCommerce - VP',
Contact_Email__c = 'test@aol.com',
Primary_URL__c = 'http://www.google.com/'
);
insert m;
m.SD_Action__c = 'Generate Opportunity';
update m;
System.assertEquals(1, (Integer)[SELECT Count(Id) FROM Opportunity][0].get('Expr0'));
}
}
我建议禁用SeeAllData = true,因为它存在风险,因为代码可能位于某些组织中 部署可能没有所需的数据,因此您的测试将被破坏
此外,您可以更改当前触发器以进行插入和更新
trigger MDwinning on MD_Meeting__c (after insert, after update) {