【问题标题】:Apex Trigger-Salesforce before insert插入前的 Apex 触发器-Salesforce
【发布时间】:2015-03-24 08:11:22
【问题描述】:

我有两个自定义对象 1. Customer 2.Complaint 并且它们是相互查找关系。

我遇到的问题是下面的触发器不起作用。 我的要求是在插入投诉之前,首先会检查电子邮件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 ');
        }
    }
}

【问题讨论】:

    标签: salesforce apex-code apex apex-trigger


    【解决方案1】:

    根本不应该编译这个触发器。

    你应该会看到错误

    循环变量必须是 Complaint__c 类型

    Trigger.newList&lt;Complaint__c&gt;

    所以,有两个错误:

    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);
    ...
    

    【讨论】:

    • 这一行也是错误的。 Map&lt;String, Shan__cust__c&gt; customers = new Map&lt;String, Shan__cust__c&gt;([SELECT Shan__cust_contact__c, Shan__cust_email__c FROM Shan__cust__c WHERE id IN: customerIds]); 实例化这样的地图会将 Shan_Cust__c.Id 映射到 Shan_Cust__c
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多