简体   繁体   English

在Salesforce中的触发器上编写测试类

[英]Writing a test class on a Trigger in Salesforce

I am a complete code noob and need help writing a test class for a trigger in Salesforce. 我是一个完整的代码菜鸟,需要帮助为Salesforce中的触发器编写测试类。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Here is the trigger: 这是触发器:

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

Read An Introduction to Apex Code Test Methods and How To Write A Trigger Test . 阅读Apex代码测试方法简介以及如何编写触发测试

Basically, you want to create a new testmethod that updates (inserts, deletes, undeletes, etc. depending on your trigger conditions) a record or sObject. 基本上,您想创建一个新的测试方法来更新(插入,删除,取消删除等,具体取决于触发条件)记录或sObject。

It looks somewhat like this: 它看起来像这样:

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

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

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