初识Salesforce中的Apex和Trigger

//下面是创建了一个Apex类,来操作Book这个对象

public class HelloWorld0105 {
public static void applyDiscount(Book__c[] books){
for(Book__c b : books){
b.Price__c *= 0.9;
}
} }

//下面是创建了一个Trigger作用于Book这个对象,进行插入对象的时候

trigger HelloWorld0105Trigger on Book__c (before insert) {
Book__c[] books = Trigger.new;
HelloWorld0105.applyDiscount(books); }

//下面是一个测试类,用来测试上面的类和触发器的

@isTest
public class HelloWorld0105Test { static testMethod void
validateHelloWorld() {
//实例化一个新的Book对象
Book__c b = new Book__c(Name=’Behind the Cloud’, Price__c=100);
System.debug(‘Price before inserting new book: ’ + b.Price__c);

   // Insert book
   insert b;

   // Retrieve the new book
   b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id];
   System.debug('Price after trigger fired: ' + b.Price__c);

   // Test that the trigger correctly updated the price
   System.assertEquals(90, b.Price__c);
} }

这样就形成了在运行测试类的时候,先是触发了Trigger然后调用了Trigger里面的方法。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值