2015-01-11 2 views
0

아래의 코드 예는 고객 및 불만 대상의 전자 메일 및 연락처를 비교하고 싶습니다. 불만 및 고객이 조회 관계에 있습니다. 같은 준수를 등록하기 전에 동일한 전자 메일 주소를 확인해야하고 전화 번호가 고객 레코드에 있어야하는 것처럼 출력을 원합니다. (삽입 전) Complaint__c에Salesforce Apex Trigeer

트리거 데모 {

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

       } 
     } 

    } 

답변

0

여기에 몇 가지. 첫째, 삽입 전에 실행되도록이 트리거를 정의하는 경우 삽입 전에 중복되는지 여부를 확인하는 if 문이 필요합니다.

두 번째로, Trigger.new는 삽입되는 개체의 모음입니다.이 경우 저는 Shan__Complaint__c 유형의 개체라고 가정합니다. 따라서 마술처럼 다른 개체 유형을 컬렉션에서 가져올 수는 없습니다. 여기 Shan__bsnl_customer__c와 다시 시도해보십시오.

대신 수행해야 할 작업은 관련 Shan__bsnl_customer__c 개체에 대한 쿼리를 수행 한 다음이를 확인하는 것입니다.

다음과 같이 시작하면됩니다. 귀하의 질문에 그것을 지정하지 않은 것처럼 Shan__CustomerId__c 관계를 사용했습니다, 당신은 실제 필드 이름을 대체 할 수 있는지 확인하십시오. 당신이 그것을 넣어 가지고 당신이되고 싶은처럼 질문 오류 메시지를 읽는에서 나에게 소리를하지만 나는 또한, 최종 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 문의 조건도 검토 할 것입니다. 질문과 모순되는 것 같습니다. 그렇지 않으면이 코드 또는 이와 유사한 것이 원하는 것을 성취해야합니다.

나는 특히 다음 당신은 또한 트리거에 대한 더 읽기, 혜택을 누릴 것이라고 생각 :

Trigger Context Variables

Common Bulk Trigger Idioms

Trigger and Bulk Request Best Practices

+0

내가 고객의 조회 필드가 –

+0

원래 비어 있어야합니다 질문에 전자 메일 주소와 전화 번호가 고객 레코드에 있는지 확인하려는 경우 - 고객 조회 필드를 비워 두려면 그럼 틀림없이 이것이 항상 거짓일까요? 죄송 합니다만 원래 질문으로 읽는 것을 원하는대로 이해하지 못합니다. –

+0

전화 번호와 이메일 주소가 일치하면 이메일과 전화 번호와 같은 고객의 세부 정보를 확인하여 고객이 불만을 제기 할 수 있습니다 !!! –

관련 문제