2017-12-12 10 views
0

私はApex Developmentの新機能です。私は私のApex TriggerのためのTestClassを書いています。アップデートのためのApexトリガーのテストクラス

私はあなたと私のコードを共有しています:

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

グッドラック

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 
} 
+0

こんにちはTim、私はエラーが発生しました: "エラー:コンパイルエラー:予期しないトークン 'void'。行2の列15 \t" –

+0

ありがとう、私はいくつかの変更を行ったし、うまく機能していた –

+0

それはロジックそれの後ろに、あなたのトリガーのキャッチブロックに欠陥があると言って、最後に私のメモを見てもらいたいと思います –

2

が行われる必要がある2つのことがあります: - 1.「オポチュニティ」オブジェクトのレコードを作成します。コードを使用して更新してください。 2.添付ファイルを作成します。ここでhttps://developer.salesforce.com/forums/?id=906F00000008yzKIAQ

+0

トリガーが正常に動作しているが、IIは、上記トリガのコードを書く方法を知らないあなたの参照のためのリンクです。 –

関連する問題