插入前的Apex Trigger-Salesforce

时间:2015-03-24 08:11:23

标签: salesforce apex-code apex apex-trigger

我有两个自定义对象1.客户2.投诉,它们是彼此的查询关系。

我有类似下面的触发器无效的问题。 在插入投诉之前我的要求就是这样,首先它会检查电子邮件ID和联系电话号码,然后投诉会注册。

trigger Demo on Complaint__c (before insert) {
    Set<Id> customerIds = new Set<Id>();
    for (Shan__Complaint__c complaint : Trigger.new) {
        customerIds.add(complaint.Shan__customer__c);
    }
    Map<String, Shan__cust__c> customers =
        new Map<String, Shan__cust__c>([SELECT Shan__cust_contact__c, Shan__cust_email__c
                                        FROM Shan__cust__c WHERE id IN: customerIds]);
    for (Shan__Complaint__c complaint : Trigger.new) {
        Shan__cust__c customer = customers.get(complaint.Shan__customer__c);
        if (customer == null || complaint.Shan__E_mail__c == customer.Shan__cust_email__c
            && complaint.Shan__Phone_Number__c == customer.Shan__cust_contact__c) {
            complaint.adderror('Your phone and Email does not exists in out database ');
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根本不应编译此触发器。

你应该看到错误

  

循环变量必须是Complaint__c

类型

Trigger.newList<Complaint__c>

所以,有2个错误:

for (Shan__Complaint__c complaint : Trigger.new) {
    customerIds.add(complaint.Shan__customer__c);
...

for (Shan__Complaint__c complaint : Trigger.new) {
    Shan__cust__c customer = customers.get(complaint.Shan__customer__c);
...