0

我有一个简单的触发器,当我的自定义对象上的 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;
    }
}
4

1 回答 1

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) {

于 2014-01-21T05:34:30.197 回答