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
aravind k 4aravind 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;

    }


}
Andrew GAndrew G

 
First, this is a terrible construct
for(Contact c : con){     
    for(Account a : trigger.new){
        for(Account ac :trigger.old){
            // do some stuff
        }
    }
}
if we think bulkification and you update 5 Account records, that inner loop will run 5 x 5 times.  Lets say each account had 2 contacts, therefore that inner Loop is going to run 250 times to process 5 account records.  That's quite inefficient.

And this:
List<Account> alist = [SELECT Id,BillingStreet,BillingCity,BillingCountry,BillingState,BillingPostalCode from Account Where Name LIKE 'Demandblue%' AND id IN: trigger.new];
why run a query?  You already have the records in the Trigger.  What would be wrong with:
List<Id> accIds = new List<Id>();
for (Account a : Trigger.new) {
  //first check that account name is one we are chasing
  if a.Name.containsIgnoreCase('demandblue') {
    //now check to see if anything has changed between old and new
    Account oldAccount = Trigger.oldMap.get(a.Id);
    if( a.BillingStreet     != oldAccount.BillingStreet  || 
        a.BillingCity       != oldAccount.BillingCity    || 
        a.BillingCountry    != oldAccount.BillingCountry || 
        a.BillingState      != oldAccount.BillingState   ||
        a.BillingPostalCode != oldAccount.BillingPostalCode){
       
      accIds.add(a.Id);
    }
  }
}
/*now the list of Id (accIds) only contains the Id of Accounts
 * where the Account Id is in the Trigger
 * the Account name is LIKE 'demandblue'
 * and some aspect of the address has changed 
 */
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:
List<Contact> listofcontacts = [SELECT Name, AccountId, Id,Account.Name,Account.Id,Email,MailingStreet,MailingCity,MailingCountry,MailingState,MailingPostalCode FROM Contact WHERE AccountId IN :accIds];
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
List<String> infoStrings = new List<String>();

for( Contact c : listofcontacts) {
  if(trigger.newMap.containskey(c.AccountId) {
    Account aa = trigger.newMap.get(c.AccountId);
    //now you can decide how you do things but...
   if ( c.MailingStreet<> aa.MailingStreet) { 
      infoString.add(c.lastname + ' had mailing street changed from ' + c.MailingStreet + ' to ' + aa.MailingStreet);
      c.billingStreet == aa.billingStreet;
  }
  //and so on

} //end contact loop
update listofcontacts;

//then construct your email message and include the contents of the infoStrings list

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 



 
ravi soniravi soni
hi aravind,
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.
trigger insertupdatecopy1 on Account (after insert,after Update) {
    if(trigger.isAfter){
    if(trigger.isInsert){
    List<Contact> lstCon = 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;
        lstCon.add(c);
        }
    }
    if(!lstCon.isEmpty()){
       insert(lstCon); 
    }
    }
    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> lstMail = new List<String>();
    String preval;
    List<Messaging.Email> emaillist = new List<Messaging.Email>();
    List<Contact> listCon = [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 : listCon){     
        for(Account a : trigger.new){
        //for(Account ac :trigger.old){
       if((a.BillingStreet != trigger.oldMap.get(a.Id).BillingStreet 
       ||  a.BillingCity != trigger.oldMap.get(a.Id).BillingCity 
       ||  a.BillingCountry != trigger.oldMap.get(a.Id).BillingCountry 
       ||  a.BillingState != trigger.oldMap.get(a.Id).BillingState 
       ||  a.BillingPostalCode != trigger.oldMap.get(a.Id).BillingPostalCode) 
       && (c.AccountId == a.Id && c.AccountId == trigger.oldMap.get(a.Id).Id)){
        c.MailingStreet = a.BillingStreet;
        c.MailingCity = a.BillingCity;
        c.MailingCountry = a.BillingCountry;
        c.MailingState = a.BillingState;
        c.MailingPostalCode = a.BillingPostalCode;
        conlist.add(c);
        lstMail.add('aarok3115@gmail.com');
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(lstMail); 
        mail.setSubject('Updations on Account');
        mail.setPlainTextBody(trigger.oldMap.get(a.Id).BillingCity +'is updated to '+ a.BillingCity);
        emaillist.add(mail);
        Messaging.sendEmail(emaillist);
      }
     }
   }
        if(conlist.size() > 0){
    update conlist;        
        }
    }
    }
}
//Test Class
@isTest
public class insertupdatecopy1Test {
@isTest
    public static void insertDataForTesting(){
        
        Account Acc = new Account();
        Acc.Name = 'Demandblue-Test'; 
        Acc.BillingStreet = '1600 Amphitheatre Parkway';
        Acc.BillingCity = 'Mountain View';
        Acc.BillingState = 'CA';
        //Acc.Email__c = 'test@1234gmail.com';
        Acc.BillingPostalCode = '94043';
        Acc.BillingCountry = 'USA';
        Acc.BillingLatitude = 37.4219493;
        Acc.BillingLongitude = -122.0847727;
        insert Acc;
         Acc.BillingPostalCode = '1234';
        update Acc;
        
        
    }
}

Let me know if it helps you and marking it as best so that it could help to others in future.
Thank you
 
Andrew GAndrew G
@veer soni .... do we do asserts or not?

and do we not understand how to make efficient code?