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
victor carcamovictor carcamo 

The 'AccountTrigger' Trigger does not appear to be working correctly. Inserted records did not have matching BillingState and ShippingState values.

User-added image

trigger AccountTrigger on Account (after 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' ]);            
    }
}


public class AccountTriggerHandler {
    public static void CreateAccounts(List<Account> acclist)
    {
        for(Account acc:acclist)
        {
            if(acc.ShippingState!=acc.BillingState)
            {
                acc.ShippingState = acc.BillingState;
            }

        }

    }

}


I've tried test--> run all and still have the same error, i dont know what else to do

 
Maharajan CMaharajan C
Hi Victor,

Change the trigger event to Before Insert.
trigger AccountTrigger on Account (before insert)


Apex Trigger: ( Before Insert )
 
trigger AccountTrigger on Account (before insert)      
{
	if ( Trigger.isBefore ) 
	{
		if(Trigger.isInsert)
		{
			AccountTriggerHandler.CreateAccounts(Trigger.new);
		}	
	}	
}


Apex Class:
 
public class AccountTriggerHandler 
{
    public static void CreateAccounts(List<Account> acclist)
    {
        for(Account a:acclist)
        {
            if(a.ShippingState!=a.BillingState)
            {
                a.ShippingState = a.BillingState; 
            }
        }
    }
}

Test Class:
 
@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();
        // Please query all record once again to check assert
        List<Account> lstAccount = [select ShippingState from Account];
        for (Account a: lstAccount )
        {
            System.assertEquals('CA', a.ShippingState, 'ERROR');
        }
        
    }
}

Thanks,
Maharajan.C
victor carcamovictor carcamo
thanks for real @Maharajan.C , after change that  the trailhead has shown me ther error, i did Test--> Run All  again and challenge passed, so the validation does not run becaese the tigger was execute after the insert???