You need to sign in to do that
Don't have an account?
Mokshada
how to write test class for trigger if we create or update account then related contact should be created with same name as account name
Trigger:-
trigger AccountContactTrig on Account (after insert,after update) {
Set<Id> accIds= new Set<Id>();
If(Trigger.isinsert && Trigger.isafter){
List<Contact> cont = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact();
c.AccountId=acc.id;
c.LastName = acc.name;
cont.add(c);
}
insert cont;
}
if(Trigger.isupdate && Trigger.isafter){
Set<String> accnames= new Set<String>();
For(Account acc:Trigger.new){
Account oldacc= Trigger.oldmap.get(acc.id);
if(acc.name!= oldacc.Name){
accnames.add(acc.name);
}
}
Map<Id,Account> mapaccounts = new Map<Id,Account>([Select Name,(Select id, Lastname from contacts where Lastname in: accnames) from Account where id in:Trigger.new]);
List<Contact> cont = new List <Contact>();
if(mapaccounts.size()>0){
for(Account ac: Trigger.new){
if(Ac.Name != trigger.OldMap.get(Ac.Id).Name){
if(mapaccounts.containsKey(ac.id)){
if(mapaccounts.get(ac.id).Contacts.size()==0){
Contact c = new Contact();
c.AccountId=ac.id;
c.LastName = ac.name;
cont.add(c);
}
}
}
insert cont;
}
}
}
}
trigger AccountContactTrig on Account (after insert,after update) {
Set<Id> accIds= new Set<Id>();
If(Trigger.isinsert && Trigger.isafter){
List<Contact> cont = new List <Contact>();
for(Account acc : trigger.new){
Contact c = new Contact();
c.AccountId=acc.id;
c.LastName = acc.name;
cont.add(c);
}
insert cont;
}
if(Trigger.isupdate && Trigger.isafter){
Set<String> accnames= new Set<String>();
For(Account acc:Trigger.new){
Account oldacc= Trigger.oldmap.get(acc.id);
if(acc.name!= oldacc.Name){
accnames.add(acc.name);
}
}
Map<Id,Account> mapaccounts = new Map<Id,Account>([Select Name,(Select id, Lastname from contacts where Lastname in: accnames) from Account where id in:Trigger.new]);
List<Contact> cont = new List <Contact>();
if(mapaccounts.size()>0){
for(Account ac: Trigger.new){
if(Ac.Name != trigger.OldMap.get(Ac.Id).Name){
if(mapaccounts.containsKey(ac.id)){
if(mapaccounts.get(ac.id).Contacts.size()==0){
Contact c = new Contact();
c.AccountId=ac.id;
c.LastName = ac.name;
cont.add(c);
}
}
}
insert cont;
}
}
}
}
Can you try the below test class.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
I dont see any issue with the above code. I tried the same code in my org when trying to some other field than Name it is not creating new Contact.
There might be some automachine which is creating new Contact. Can you check deactivating all those and check.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
yes the trigger is working fine I was asking for test class.
Thanks,
Can you try the below test class.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
I want to test it for 100 accounts , how to do that?
Thanks,
Can you check below .
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Thank you It worked but I have one question why we have used "LastName='Contact0'" , what does it mean by contact0.
We are inserting 100 Accounts with names as Contact0,Contact1,Contact2 ... etc. So these 100 Contacts should be inserted with same Lastnames. So we are checking on it.
Thanks,