+ Start a Discussion
Chitral ChaddaChitral Chadda 

triger contact

triger on account which matches email and phone of the associated account, if both match then only insert contact record else throww error
trigger MatchPhoneonAccount on Contact (before insert)
{
    set<id> accIdset = new set<id>();
    for(Contact c :trigger.new)
    {
        if( c.accountid!=null)
            {
             accIdset.add(c.AccountId);
            }
    }
     map<id,account> accMap = new map<id,account>([Select id,Email__c , Phone from account where id in : accIdset]);
     
     
    for(contact c : trigger.new)
    {
         if(c.Email != null && c.Phone != null && accMap != null)
         {
          system.debug('*******Email'+c.Email);
          system.debug('*******Phone'+c.Phone);
            if(c.Email != accMap.get(c.accountid).Email__c &&  c.Phone != accMap.get(c.accountid).Phone)
            {
            system.debug('*******Dosent Matched'+c.Phone);
             c.adderror('Your phone and Email does not exists in out database .');
            }
         }
    
    }
}

no debug logs are generate and even though account and contact email, phone are different , the trigger dosent fire .
JayOlayJayOlay
Hi Chitral -- Your code is working for me on a developer instance. Please double-check that the Email__c field on Account exists, that you are requesting Debug Logs for the right user, and that your trigger is marked as "Is Active".
Chitral ChaddaChitral Chadda
hi dear 

is the trigger working absolutely fine fr you ? wer u able to test this
means only if accounts email and phone MATCH  to contacts email and phone then only insert that contact record .
is it working on ur org ?

yes i checkd triger is active .. bt dnt knw wats wrong
dbakedbake
Have you checked that you have created a log for your (or the running user's) User record before saving the Contact? Also, are you editing an existing contact, because I noticed that this trigger is only set to fire on before insert.
Chitral ChaddaChitral Chadda
Yes trigger is on before insert only If it matches then only insert else dont insert record