function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
M SreekanthM Sreekanth 

Any can write me for this trigger

HandlerClass:-
==========
public class PreventConDuplicatePhoneByAccount {
    public static void fetchDupCon(list<contact> conList){
        for(contact c:conList){
            integer count=[select count() From Account WHERE id=:c.accountId and Phone=:c.Phone];
            if(count!=1){
                c.Phone.addError('Phone No Should Be Same As Linked Account');
            }
        }
    }
}

Trigger  Code:-
==========
trigger PreventConDuplicatePhoneByAccountTrigger on Contact (before insert,before update) {
PreventConDuplicatePhoneByAccount.fetchDupCon(Trigger.new);
}
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sreekanth,

Can you explain about your trigger scenario?

Thanks!!
M SreekanthM Sreekanth
When ever contact record is inserted or updated it would be same as contact phone number based on linked account phone number,if not phone number not same as linked account through an error message
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sreekanth,

You can do this by using validation rule. try with below formula validation on contact object.
Account.Phone <> Phone

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
M SreekanthM Sreekanth
That's ok but i tried this example for how to write test class i am not getting how can I write if you know please write for me

thanks for replying me ankaiah
AnkaiahAnkaiah (Salesforce Developers) 
Hi Sreekanth,

what you have tried? can you share the code will help you with test class.

Thanks!!
SOURAV GUHA 9SOURAV GUHA 9
Trigger:

trigger ContactTriggerTest on Contact (before insert, before update) {
    
    ContactTriggerTestHandler.fetchDupCon(trigger.new);

}

Handler class:

public class ContactTriggerTestHandler {
    
    public static void fetchDupCon(list<contact> conList)
    {
        list <account> relatedAcc = new list<account>();
        for (contact con:conList )
        {
            if (con.accountid != null)
            {
                relatedAcc= [select id, name, phone from account where id = : con.accountid];
                if (relatedAcc[0].phone!=null)
                {
                    con.phone=relatedAcc[0].phone;
                }
                
            }
        }
    }

}


Test class:

@isTest
public class ContractTriggerTestClass {
    
    @isTest
    
    public static void TestContactTrigger()
    {
        list<account> parentAcc= new list<account>();
        list<contact> childCon= new list<contact>();
        account acc = new account();
        acc.name='test contact trigger';
        acc.phone='987654';
        
        insert acc;
        
        contact con = new contact();
        con.AccountId=acc.id;
        con.LastName='test trigger';
        
        insert con;
        
        parentAcc=[select id, name, phone from account where id =:con.AccountId];
        childCon= [select id, lastname, phone from contact where id =: con.id];

         system.assertEquals(parentAcc[0].phone, childCon[0].phone);
    }

}