+ Start a Discussion
Shantanu MahajanShantanu Mahajan 

Salesforce Apex trigger

in below code example i want to compare the e-mail and contact number of customer and complaint object. complaint and customer are in lookup relationship. i want output like if before registering a compliant it should check the same e-mail address and phone number must be in customer record.

trigger Demo on Complaint__c (before insert) {
    
    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');
                    
                    }
            }
        
        }
Chitral ChaddaChitral Chadda

what i understand is there are 2 object :
customer and complaint 

now before enetring the complaint you want the field( phone,email) on complaint object to match with fields on customer object if so , only then enter complaimnt record else .. add error
is this the requirement ?

Chitral ChaddaChitral Chadda
TRYTHIS:
trigger MatchPhone on Complaint (before insert)
{
    set<id> accIdset = new set<id>();
    for(Complaint c :trigger.new)
    {
        if( c.Customer__c != null)
            {
             accIdset.add(c.Customer__c);
            }
    }
     map<id,Customer__c> accMap = new map<id,Customer__C>([Select id,Email__c , Phone from Customer__c where id in : accIdset]);
    for(Complaint__c ct : trigger.new)
    {
         if(ct.Email != null && ct.Phone != null&& accMap != null)
         {
         
            if(ct.Email != accMap.get(ct.Customer__c).Email__c &&  ct.Phone != accMap.get(ct.Customer__c).Phone)
            {
             ct.adderror('Your phone and Email does not exists in out database .');
            }
         }
    
    }
}

Match the variable name according to you requiremnts :
and let me know .
Thanks :)
Shantanu MahajanShantanu Mahajan
@Chitral Chadda: I got an error when trying code which is given.... it showing me error " Invalid field bsnl_customer__c for SObject Complaint__c at line 8 column 27"