Salesforce 用于更新的Apex触发器的测试类

Salesforce 用于更新的Apex触发器的测试类,salesforce,apex,test-class,apex-trigger,Salesforce,Apex,Test Class,Apex Trigger,我是Apex开发的新手。我想为Apex触发器编写一个TestClass 我正在与您共享我的代码: trigger ClosedOpportunityTrigger on Opportunity (before update) { for(Opportunity o:Trigger.New) { if(o.Probability==100 && o.Invoiced__c == true) {

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

有两件事需要做:- 1.在“Opportunity”对象中创建记录。使用代码更新它。
2.创建一个附件。以下是供您参考的链接

需要做两件事:- 1.在“Opportunity”对象中创建记录。使用代码更新它。 2.创建一个附件。这是供您参考的链接

Goodluck

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
}
祝你好运

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块有缺陷Hi Tim,我得到了错误“Error:Compile Error:Unexpected token'void.”在第2行第15列非常感谢你,我做了一些修改,它工作得很好。这更多的是伪代码,背后有逻辑。我希望你在最后看到我的笔记,说你的触发器中的catch块有缺陷