Apex Trigger-Salesforce перед вставкой

У меня есть два настраиваемых объекта: 1. Клиент 2. Жалоба, и они взаимодействуют друг с другом.

У меня такая проблема, что нижеприведенный триггер не работает. Мое требование таково: прежде чем подавать жалобу, сначала она проверит идентификатор электронной почты и контактный номер, а затем жалоба будет зарегистрирована.

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

person Shantanu Mahajan    schedule 24.03.2015    source источник


Ответы (1)


Этот триггер вообще не должен компилироваться.

Вы должны увидеть ошибку

Переменная цикла должна быть типа Complaint__c

Trigger.new is a List<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);
...
person Pavel Slepiankou    schedule 24.03.2015
comment
эта строка также неверна. 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]); создание экземпляра такой карты сопоставит Shan_Cust__c.Id с Shan_Cust__c - person willard; 08.04.2015