質問

I have a simple trigger that is supposed to create a new opportunity when the SD_Action__c field on my custom object is a certain value. The code has no errors, but nothing happens when I try update the field in sandbox or production. What am I missing to make this great an opportunity when 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);
    }
}   

Here is my test class:

@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;
    }
}
役に立ちましたか?

解決

Your test didn't work properly because of you use only insert operation, but your trigger works on update mode. So, you have to change test or trigger or both

@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'));
    }
}

I suggest to disable SeeAllData = true because it risky because some of org where your code might be deployed might have no needed data and as a result your tests will be broken

Also you can change the current trigger for working on insert and update

trigger MDwinning on MD_Meeting__c (after insert, after update) {

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top