You need to sign in to do that
Don't have an account?

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: []
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: []
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:
Please mark it as the best answer, if it solves your problem.
Thanks and Regards
Suraj Tripathi
All Answers
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:
Please mark it as the best answer, if it solves your problem.
Thanks and Regards
Suraj Tripathi
Please use below code for Apex class
Accountupdate
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh