简体   繁体   English

Apex触发器的测试类以进行更新

[英]Test class for Apex Trigger for update

I am new in Apex Development. 我是Apex开发的新手。 I want to write a TestClass for my Apex Trigger. 我想为我的Apex触发器编写一个TestClass。

I am sharing my code with you: 我正在与您分享我的代码:

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

There are 2 things that need to be done :- 1. Create a record in 'Opportunity' object. 有两件事需要做:-1.在“机会”对象中创建一条记录。 Update it using code. 使用代码更新它。 2. Create an attachment. 2.创建一个附件。 Here is the link for your reference https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ 这是供您参考的链接https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM