Salesforce顶点触发器

Salesforce顶点触发器,salesforce,Salesforce,在下面的代码示例中,我想比较客户和投诉对象的电子邮件和联系电话。投诉和客户处于查找关系中。我希望在注册一个符合要求的产品之前,它应该检查客户记录中的相同电子邮件地址和电话号码 插入前触发投诉演示{ if(trigger.isBefore) { if(trigger.isInsert) { for(Shan__Complaint__c a:Trigger.new) { for(Shan__bsnl_customer__c b:

在下面的代码示例中,我想比较客户和投诉对象的电子邮件和联系电话。投诉和客户处于查找关系中。我希望在注册一个符合要求的产品之前,它应该检查客户记录中的相同电子邮件地址和电话号码

插入前触发投诉演示{

if(trigger.isBefore)
{
    if(trigger.isInsert)
    {
        for(Shan__Complaint__c a:Trigger.new)
        {
            for(Shan__bsnl_customer__c b:Trigger.new)
               if(a.Shan__Phone_Number_del__c== b.Shan__cust_contact__c && a.Shan__E_mail_del__c==b.Shan__cust_email__c)
                {
                        a.adderror('Customer is not in Database');

                }
        }

    }

这里有两件事。首先,如果您只定义这个触发器在插入之前运行,那么检查它是否在插入之前运行的if语句是多余的

其次,Trigger.new是一个被插入的对象集合,在本例中,我假设对象类型为Shan\uu投诉\uu c,因此您将无法神奇地从集合中获取任何其他对象类型,就像您在这里尝试Shan\uu bsnl\u customer\uu c一样

您需要做的是查询相关的Shan_ubsnl_customer_uc对象,然后检查它们

类似这样的内容应该足以让您开始。我已经使用Shan_uuCustomerID_uC作为关系,因为您在问题中没有指定它,请确保您将它替换为真实的字段名。我还保留了您在最后的if语句中所说的逻辑,尽管从阅读您的问题和错误消息来看,它听起来很简单我就像你希望的那样!=而不是==

Set<Id> customerIds = new Set<Id>();

for (Shan__Complaint__c complaint : Trigger.new)
{
    //get a list of Customer Ids to query
    customerIds.add(complaint.Shan__CustomerId__c);   // replace field name here
}

//query the customer objects
Map<Id, Shan__bsnl_customer__c> customers = new Map<Id, Shan__bsnl_customer__c>(
    [SELECT Id, Shan__cust_contact__c, Shan__cust_email__c 
      FROM Shan__bsnl_customer__c 
      WHERE Id IN :customerIds];    

for (Shan__Complaint__c complaint : Trigger.new) 
{ 
    //get the right customer -- replace field name with correct value
    Shan__bsnl_customer__c customer = customers.get(complaint.Shan__CustomerId__c);

    //add null check in case no customer found
    if (customer == null ||
         (complaint.Shan__Phone_Number_del__c == customer.Shan__cust_contact__c 
         && complaint.Shan__E_mail_del__c == customer.Shan__cust_email__c))
    {
          complaint.addError('Customer is not in Database');
    }
}
我假设如果没有找到客户,例如,客户查找关系字段为空,那么这也是一个错误状态,根据您的问题,情况似乎就是这样

正如我所说的,我也会检查你最后的if语句中的条件,它们似乎与问题相矛盾,否则这个代码或类似的东西应该可以实现你想要的

我认为您还可以进一步阅读触发器,特别是以下内容:


我希望客户查找字段应为空您的原始问题是,您希望检查客户记录中是否有电子邮件地址和电话号码-如果您希望客户查找字段为空,那么这肯定是错误的?很抱歉,我真的不明白您在阅读原始问题时想要什么n、 我想检查客户的详细信息,如电子邮件和电话号码如果电话号码和电子邮件地址匹配,则只有客户可以注册投诉!!!Daniel Bernsons我想检查客户是否已经存在如果他存在,则只有客户可以记录投诉