Handler Class:
public class AccountTriggerHandler
{
public static void CreateAccounts(List<Account> acc)
{
List<Account> accts = new List<Account>();
for(Account a:acc)
{
if(a.BillingState.EQUALS(a.ShippingState))
{
accts.add(a);
}
}
insert accts;
}
}
Test Class :
@isTest
public class AccountTriggerTest
{
@isTest static void TestCreateNewAccountInBulk()
{
List<Account> accts = new List<Account>();
for(Integer i=0; i < 200; i++)
{
Account acct = new Account();
acct.Name='Test Account'+i;
acct.BillingState='CA';
acct.ShippingState='CA';
accts.add(acct);
}
// Perform Test
Test.startTest();
insert accts;
Test.stopTest();
// Verify that 200 new Accounts were inserted
List<Account> verifyAccts = [SELECT Id FROM Account];
System.assertEquals(200, verifyAccts.size());
}
}
Trigger:
trigger AccountTrigger on Account (before insert)
{
if (Trigger.isInsert) {
if(Trigger.isBefore){
AccountTriggerHandler.CreateAccounts(Trigger.New);
}
}
Hi Prema,Please try this code.
Please let me know if you have any query.Please mark it as best Answer if you find it helpful.Thank YouAjay Dubedipublic class AccountTriggerHandler {
public static void CreateAccounts (List<Account> accList)
{
for(Account acc : accList)
{
if(acc.ShippingState!=acc.BillingState)
{
acc.ShippingState = acc.BillingState;
}
}
}
}
trigger AccountTrigger on Account (before insert)
{
if (Trigger.isBefore && Trigger.isInsert) {
AccountTriggerHandler.CreateAccounts(Trigger.new);
}
}
@isTest
public class AccountTriggerTest {
@isTest static void TestCreate200Records(){
List<Account> accts = new List<Account>();
for(Integer i=0; i < 200; i++) {
Account acct = new Account(Name='Test Account ' + i, BillingState = 'CA');
accts.add(acct);
}
Test.startTest();
insert accts;
Test.stopTest();
System.assertEquals(200, [SELECT Count() FROM Account WHERE ShippingState = 'CA' ]);
}
}