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
golla Anithagolla Anitha 

public class Accountupdate { public static void Mymethod(List<Account> acc) { //List<Account> addition=new List<Account>(); set<String> Collectstring=new set<String>(); for(Account collectzipcode:acc)

@istest
public class Accountupdatetest {
    @testsetup
    static void Fieldvalues()
    {
        Account accountList=new Account(name='bargavi',BillingPostalCode='56789');
         insert accountList ;
        Address__c adds=new Address__c(zip_code__c='56789',State_City__c='karntaka',Country__c='india',Street__c='dangal');
         insert adds;
    }
    @istest
   static void test1()
    {
         Account acc2=[select name,BillingPostalCode,BillingCity,BillingCountry,BillingStreet from Account ];
       // Address__C adds1=[select Street__c,Zip_code__c,State_City__c,Country__c from Address__C];

         test.startTest(); 
          acc2.BillingPostalCode='56789';
        try{
         update acc2; 
        }
        catch(DmlException exp)
        {
            system.assert(exp.getMessage().contains('no address zipcode matched to the billing postal code'));
        }
    
       test.stopTest(); 
    }
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public class Accountupdate {
public static void Mymethod(List<Account> acc)
          {
//List<Account> addition=new List<Account>();
           set<String> Collectstring=new set<String>();
                 for(Account collectzipcode:acc)
                     {
                           Collectstring.add(collectzipcode.BillingPostalCode);
                           system.debug(' collect All the Zip code ids'+Collectstring);
                      }
            List<Address__c> fetch=[select Street__c,Zip_code__c,State_City__c,Country__c  from Address__c where Zip_code__c in:Collectstring];
             system.debug(+fetch);
            Map<String,Address__c> maping=new Map<String,Address__c>();
                for(Address__c edit:fetch)
                    {
                       maping.put(edit.Zip_code__c,edit);
                        system.debug('maping'+maping);
                     }
                      for(Account acc1:acc)
                             {
                                 List<Address__c> lstAdd=new List<Address__c>();
                               Address__c lastoutput=maping.get(acc1.BillingPostalCode);
                                 lstadd.add(lastoutput);
                                 system.debug('lastoutput'+lastoutput);
                                        if(lstadd.size()<>null)
                                          {
                                              acc1.BillingCity=lastoutput.State_City__c;
                                               acc1.BillingCountry=lastoutput.Country__c;
                                               acc1.BillingStreet=lastoutput.Street__c;
                                           }
                           else
                           {
                                     acc1.adderror('no address zipcode matched to the billing postal code');
                           }

                              }
            }
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
error
//////////////////////
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, addressontrigger: execution of BeforeInsert

caused by: System.NullPointerException: Attempt to de-reference a null object

Class.Accountupdate.Mymethod: line 27, column 1
Trigger.addressontrigger: line 3, column 1: []
Best Answer chosen by golla Anitha
Suraj Tripathi 47Suraj Tripathi 47
Hi golla,

The issue is in the Test Setup Method. You are inserting the Account Record first and then the address record. Your trigger is running on before insert, so that means the trigger runs as soon as the account record is inserted. Since, the address record is not yet inserted, the query on Address Object in the Trigger return 0 records which in turn creates a null value in your map. 
So, all you need to do is you need to insert the address record first then the account record in the test setup method.

Please refer to below code:

Apex Class:
@istest
public class Accountupdatetest {
    @testsetup
    static void Fieldvalues()
    {
         Address__c adds=new Address__c(zip_code__c='56789',State_City__c='karntaka',Country__c='india',Street__c='dangal');
         insert adds;
        
        Account accountList=new Account(name='bargavi',BillingPostalCode='56789',BillingCity='karntaka',BillingCountry='india',BillingStreet='dangal');
         insert accountList ;
       
    }
    @istest
   static void test1()
    {
         Account acc2=[select name,BillingPostalCode,BillingCity,BillingCountry,BillingStreet from Account ];
       // Address__C adds1=[select Street__c,Zip_code__c,State_City__c,Country__c from Address__C];

         test.startTest(); 
          acc2.BillingPostalCode='56789';
        try{
         update acc2; 
        }
        catch(DmlException exp)
        {
            system.assert(exp.getMessage().contains('no address zipcode matched to the billing postal code'));
        }
    
       test.stopTest(); 
    }
}

Please mark it as the best answer, if it solves your problem.

Thanks and Regards
Suraj Tripathi

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi golla,

The issue is in the Test Setup Method. You are inserting the Account Record first and then the address record. Your trigger is running on before insert, so that means the trigger runs as soon as the account record is inserted. Since, the address record is not yet inserted, the query on Address Object in the Trigger return 0 records which in turn creates a null value in your map. 
So, all you need to do is you need to insert the address record first then the account record in the test setup method.

Please refer to below code:

Apex Class:
@istest
public class Accountupdatetest {
    @testsetup
    static void Fieldvalues()
    {
         Address__c adds=new Address__c(zip_code__c='56789',State_City__c='karntaka',Country__c='india',Street__c='dangal');
         insert adds;
        
        Account accountList=new Account(name='bargavi',BillingPostalCode='56789',BillingCity='karntaka',BillingCountry='india',BillingStreet='dangal');
         insert accountList ;
       
    }
    @istest
   static void test1()
    {
         Account acc2=[select name,BillingPostalCode,BillingCity,BillingCountry,BillingStreet from Account ];
       // Address__C adds1=[select Street__c,Zip_code__c,State_City__c,Country__c from Address__C];

         test.startTest(); 
          acc2.BillingPostalCode='56789';
        try{
         update acc2; 
        }
        catch(DmlException exp)
        {
            system.assert(exp.getMessage().contains('no address zipcode matched to the billing postal code'));
        }
    
       test.stopTest(); 
    }
}

Please mark it as the best answer, if it solves your problem.

Thanks and Regards
Suraj Tripathi
This was selected as the best answer
mukesh guptamukesh gupta
Hi Golla,

Please use below code for Apex class

Accountupdate
public class Accountupdate {
public static void Mymethod(List<Account> acc)
          {
//List<Account> addition=new List<Account>();
           set<String> Collectstring=new set<String>();
                 for(Account collectzipcode:acc)
                     {
                           Collectstring.add(collectzipcode.BillingPostalCode);
                           system.debug(' collect All the Zip code ids'+Collectstring);
                      }
            List<Address__c> fetch=[select Street__c,Zip_code__c,State_City__c,Country__c  from Address__c where Zip_code__c in:Collectstring];
             system.debug(+fetch);
            Map<String,Address__c> maping=new Map<String,Address__c>();
                for(Address__c edit:fetch)
                    {
                       maping.put(edit.Zip_code__c,edit);
                        system.debug('maping'+maping);
                     }
                      for(Account acc1:acc)
                             {
                                 List<Address__c> lstAdd=new List<Address__c>();
                               Address__c lastoutput=maping.get(acc1.BillingPostalCode);
                                 lstadd.add(lastoutput);
                                 system.debug('lastoutput'+lastoutput);
                                        if(lstadd.size()<>null)
                                          {
										   if(lastoutput.State_City__c != '')
                                              acc1.BillingCity=lastoutput.State_City__c;
										   if(lastoutput.Country__c != '')	  
                                               acc1.BillingCountry=lastoutput.Country__c;
										   if(lastoutput.Street__c != '')	
                                               acc1.BillingStreet=lastoutput.Street__c;
                                           }
                           else
                           {
                                     acc1.adderror('no address zipcode matched to the billing postal code');
                           }

                              }
            }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
mukesh guptamukesh gupta
Hi Golla,

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
golla Anithagolla Anitha
thanks mukesh your code is working