【问题标题】:Test class for Apex Trigger for update用于更新的 Apex 触发器的测试类
【发布时间】:2018-05-25 20:12:58
【问题描述】:

我是 Apex 开发的新手。我想为我的 Apex 触发器编写一个 TestClass。

我正在与你分享我的代码:

trigger ClosedOpportunityTrigger on Opportunity (before update) {
    for(Opportunity o:Trigger.New) {
        if(o.Probability==100 && o.Invoiced__c == true)
        {            
            Attachment a = new Attachment();
            try {
                a = [Select Id, Name from Attachment where ParentId =:o.Id];
            }
            catch(Exception e) {
                a = null;
            }
            if (a == null)
                o.addError('Add an attachment before you close the Opportunity');
            }
        }
    }

【问题讨论】:

    标签: salesforce apex test-class apex-trigger


    【解决方案1】:

    有两件事需要做:- 1. 在“机会”对象中创建记录。使用代码更新它。 2. 创建附件。这是您的参考链接https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ

    【讨论】:

    • 触发器工作正常,但我不知道如何编写上述触发器的代码。
    【解决方案2】:

    祝你好运

    public static void testmethod(){
    
    opportunity opp = new opportunity()
    //change whatever fields u need to make probability 100%
    opp.stage = 'Closed Won';
    opp.Invoiced__c == true;
    
    try{
    
    insert opp
    }
    catch(Exception e){
    
    string errormessage = e.getMessage();
    
    }
    //now that u know how to do it, do a positive test where you also add an          
    //attachment
    //as a side note, your catch block will never fire, because you aren't     
    //catching any exceptions you are just getting a null string
    }
    

    【讨论】:

    • 嗨,蒂姆,我收到错误“错误:编译错误:意外的令牌 'void'。在第 2 行第 15 列”
    • 非常感谢,我做了一些更改,效果很好
    • 更多的是伪代码,背后有逻辑,我希望你也看到我最后的笔记,说你的触发器中的 catch 块有缺陷
    猜你喜欢
    • 1970-01-01
    • 2019-06-24
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    相关资源
    最近更新 更多