You need to sign in to do that
Don't have an account?
M 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);
}
==========
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);
}
Can you explain about your trigger scenario?
Thanks!!
You can do this by using validation rule. try with below formula validation on contact object.
If this helps, Please mark it as best answer.
Thanks!!
thanks for replying me ankaiah
what you have tried? can you share the code will help you with test class.
Thanks!!
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);
}
}