+ Start a Discussion
Selim BEAUJOURSelim BEAUJOUR 

Challenge Not yet complete... here's what's wrong: The 'AccountTrigger' Trigger does not appear to be working correctly. Inserted records did not have matching BillingState and ShippingState values.

Hi All,

I have a problem with this Challenge : https://developer.salesforce.com/trailhead/microsoft_dotnet/apex_basics_dotnet/execution_context

//AccountTriggerHandler

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

}
//AccountTrigger

trigger AccountTrigger on Account (before insert) 
{
    if (Trigger.isBefore && Trigger.isInsert) {
            AccountTriggerHandler.CreateAccounts(Trigger.new);
        }    
    }


//AccountTriggerTest
@isTest
public class AccountTriggerTest {
    
    @isTest static void TestCreate200Records(){
        
        // Test Setup data
        // Create 200 new Accounts
        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 acct;
            Test.stopTest();
            
            for (Account a:accts){
                
                System.assertEquals('CA', a.ShippingState, 'ERROR');
            }
            
    }

        
}
}

Can you help me please??
Thanks!
James LoghryJames Loghry
It sounds like you may have it backwards.  "For each Account record, before saving, ensure that the ShippingState field has the same value as the BillingState field."

Instead of:
 
a.BillingState=a.ShippingState;

Try:
 
a.ShippingState=a.BillingState;

 
Selim BEAUJOURSelim BEAUJOUR
Hi James,

Thank you but I already tried it and it doesn't work.
Amit Chaudhary 8Amit Chaudhary 8
Hi Selim BEAUJOUR,

Please updated  your Apex class and Test class like below

1) AccountTriggerHandler
public class AccountTriggerHandler 
{
    public static void CreateAccounts(List<Account> acclist)
    {
        for(Account a:acclist)
        {
            if(a.ShippingState!=a.BillingState)
            {
                a.ShippingState = a.BillingState; // you need to update ShippingState
            }
        }
    }
}
2) AccountTriggerTest
@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');
        }
        
    }
}

Let us know if this will help you

Thanks
Amit Chaudhary
 
Andrew EversleyAndrew Eversley
Thank you for your imput Amit Chaudhary. Your suggestion above worked out for me. I appreciate your prompt responses and your assistance. How do I submit your responses for best answer and making the question solved? I'm only able to mark it as "Liked" presently. Thanks.
Andrew EversleyAndrew Eversley
Please disregard my last post, I figured out how to solve and choose the best answer on my page. Thanks again.