在Salesforce中的Trigger上编写测试类

时间:2012-10-03 18:53:51

标签: unit-testing triggers salesforce

我是一个完整的代码菜鸟,需要帮助在Salesforce中为触发器编写测试类。任何帮助将不胜感激。

这是触发器:

trigger UpdateWonAccounts on Opportunity(before Update) {
  Set < Id > accountIds = new Set < Id > ();

  //Collect End user Ids which has won Opportunities
  for (Opportunity o : Trigger.new) {
    if (o.isWon && o.EndUserAccountName__c != null) {
      accountIds.add(o.EndUserAccountName__c);
    }
  }

  List < Account > lstAccount = new List < Account > ();

  //Iterate and collect all the end user records
  for (Account a : [Select Id, Status__c From Account where Id IN : accountIds]) {
    lstAccount.add(new Account(Id = a.Id, Status__c = true));
  }

  //If there are any accounts then update the records
  if (!lstAccount.isEmpty()) {
    update lstAccount;
  }
}

1 个答案:

答案 0 :(得分:1)

阅读An Introduction to Apex Code Test MethodsHow To Write A Trigger Test

基本上,您希望创建一个新的测试方法,根据您的触发条件更新(插入,删除,取消删除等)记录或sObject。

看起来有点像这样:

public class myClass {
    static testMethod void myTest() {
       // Add test method logic to insert and update a new Opportunity here
    }
}