+ 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.
victor carcamovictor carcamo
Write an Apex trigger that modifies Account fields before inserting records.
Write an Apex trigger that fires before records are inserted and ensures that the ShippingState field has the same value as the BillingState field.
Create an Apex class named AccountTriggerHandler that contains a public static method called CreateAccounts to accept the List of Account objects
For each Account record, before saving, ensure that the ShippingState field has the same value as the BillingState field
Write an Apex trigger named AccountTrigger that fires before records are inserted
The Apex trigger must call the AccountTriggerHandler.CreateAccounts() method with the collection of new records
Make sure you use the isBefore and isInsert trigger context variables
Create a test class named AccountTriggerTest that inserts 200 Account records with a BillingState of CA. After the insert, test to ensure that all 200 records have a ShippingState of CA
Before verifying this challenge, run your test class at least once using the Developer Console Run All feature.


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

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


-------

public with sharing class AccountTriggerHandler {   
    public static void CreateAccounts (List<Account> accts) {
        List<Account> acc = new List<Account>();
        for (Account a : accts) {
                if (a.ShippingState != a.BillingState)
                {
                    a.ShippingState = a.BillingState;
                    acc.add(a);
                }
                acc.add(a);
            }// end foreach
        
        if (acc.size() > 0) {
            insert acc;
        }
    }
}

-----

@isTest
public class AccountTriggerTest {

    @isTest public static void TestCreateNewAccountInBulk(){
        // 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);
        }              
        // Perform Test
        Test.startTest();
        insert accts;                               
        Test.stopTest();
        // Verify that 200 new Accounts were inserted
        List<Account> lstAccount  = [SELECT  ShippingState FROM Account ];
        for (Account ac: lstAccount ) 
        {
          System.assertEquals('CA', ac.ShippingState, 'ERROR');
        }


          
        
    }
    
    
}

Please help