You need to sign in to do that
Don't have an account?
aravind k 4
can any one help this for test class
trigger insertupdatecopy1 on Account (after insert,after Update) {
if(trigger.isInsert){
List<Contact> clist = new List<Contact>();
for(Account acc :trigger.new){
if(acc.name.contains('Demandblue')){
Contact c = new Contact();
c.lastname = acc.name;
c.Email = acc.Email__c;
c.Phone=acc.Phone;
c.AccountId=acc.Id;
c.MailingStreet = acc.BillingStreet;
c.MailingCity = acc.BillingCity;
c.MailingCountry = acc.BillingCountry;
c.MailingState = acc.BillingState;
c.MailingState = acc.BillingPostalCode;
c.MailingLatitude = acc.BillingLatitude;
c.MailingLongitude = acc.BillingLongitude;
clist.add(c);
}
}
if(!clist.isEmpty()){
insert(clist);
}
}
if(trigger.isUpdate){
List<Contact> conlist = new List<Contact>();
List<Account> alist = [SELECT Id,BillingStreet,BillingCity,BillingCountry,BillingState,BillingPostalCode from Account Where Name LIKE 'Demandblue%' AND id IN: trigger.new];
List<String> sta = new List<String>();
String preval;
List<Messaging.Email> emaillist = new List<Messaging.Email>();
List<Contact> con = [SELECT Name, AccountId, Id,Account.Name,Account.Id,Email,MailingStreet,MailingCity,MailingCountry,MailingState,MailingPostalCode FROM Contact where AccountId IN: alist and Account.Name Like '%Demandblue%'];
for(Contact c : con){
for(Account a : trigger.new){
for(Account ac :trigger.old){
if((a.BillingStreet != ac.BillingStreet || a.BillingCity != ac.BillingCity || a.BillingCountry != ac.BillingCountry || a.BillingState != ac.BillingState || a.BillingPostalCode != ac.BillingPostalCode) && (c.AccountId == a.id && c.AccountId == ac.id)){
c.MailingStreet = a.BillingStreet;
c.MailingCity = a.BillingCity;
c.MailingCountry = a.BillingCountry;
c.MailingState = a.BillingState;
c.MailingPostalCode = a.BillingPostalCode;
conlist.add(c);
sta.add('aarok3115@gmail.com');
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(sta);
mail.setSubject('Updations on Account');
mail.setPlainTextBody(ac.BillingCity +'is updated to '+ a.BillingCity);
emaillist.add(mail);
Messaging.sendEmail(emaillist);
}
}
}
}
update conlist;
}
}
if(trigger.isInsert){
List<Contact> clist = new List<Contact>();
for(Account acc :trigger.new){
if(acc.name.contains('Demandblue')){
Contact c = new Contact();
c.lastname = acc.name;
c.Email = acc.Email__c;
c.Phone=acc.Phone;
c.AccountId=acc.Id;
c.MailingStreet = acc.BillingStreet;
c.MailingCity = acc.BillingCity;
c.MailingCountry = acc.BillingCountry;
c.MailingState = acc.BillingState;
c.MailingState = acc.BillingPostalCode;
c.MailingLatitude = acc.BillingLatitude;
c.MailingLongitude = acc.BillingLongitude;
clist.add(c);
}
}
if(!clist.isEmpty()){
insert(clist);
}
}
if(trigger.isUpdate){
List<Contact> conlist = new List<Contact>();
List<Account> alist = [SELECT Id,BillingStreet,BillingCity,BillingCountry,BillingState,BillingPostalCode from Account Where Name LIKE 'Demandblue%' AND id IN: trigger.new];
List<String> sta = new List<String>();
String preval;
List<Messaging.Email> emaillist = new List<Messaging.Email>();
List<Contact> con = [SELECT Name, AccountId, Id,Account.Name,Account.Id,Email,MailingStreet,MailingCity,MailingCountry,MailingState,MailingPostalCode FROM Contact where AccountId IN: alist and Account.Name Like '%Demandblue%'];
for(Contact c : con){
for(Account a : trigger.new){
for(Account ac :trigger.old){
if((a.BillingStreet != ac.BillingStreet || a.BillingCity != ac.BillingCity || a.BillingCountry != ac.BillingCountry || a.BillingState != ac.BillingState || a.BillingPostalCode != ac.BillingPostalCode) && (c.AccountId == a.id && c.AccountId == ac.id)){
c.MailingStreet = a.BillingStreet;
c.MailingCity = a.BillingCity;
c.MailingCountry = a.BillingCountry;
c.MailingState = a.BillingState;
c.MailingPostalCode = a.BillingPostalCode;
conlist.add(c);
sta.add('aarok3115@gmail.com');
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(sta);
mail.setSubject('Updations on Account');
mail.setPlainTextBody(ac.BillingCity +'is updated to '+ a.BillingCity);
emaillist.add(mail);
Messaging.sendEmail(emaillist);
}
}
}
}
update conlist;
}
}
And this: why run a query? You already have the records in the Trigger. What would be wrong with: a loop, yes, but no query
A singular loop where we make use of the Trigger.oldMap where we pull the element from the oldMap using a .get() method.
it will then feed better into you query for the contacts: There is no need to query the Account name as a wild card as you have already filtered that the first time
Now we have the contacts, loop them and use the trigger.newMap context
As a side note, rather than bombarding yourself with emails because someone made a record change, consider if field tracking would solve the issue of tracking changes in records. Unless this is simply some test exercise for your Apex skills.
Supply an updated trigger and i might consider some help on the test class.
regards
Andrew
p.s. i don't do test classes when the code is obviously in need of help
p.p.s. all code is supplied uncompiled and as-is
I did some change in your trigger don't worry logic are same but I removed some unnecessary code and correct variable's Name.
Try Following Trigger And test class With 100% code coverage. //Test Class
Let me know if it helps you and marking it as best so that it could help to others in future.
Thank you
and do we not understand how to make efficient code?