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

Write an Apex trigger that modifies Account fields before inserting records.
Hi all,
I have a problem with this challenge :
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.
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.
and this is my classes :
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.
Anyone can help me please??
Thanks!
I have a problem with this challenge :
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.
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.
and this is my classes :
public class AccountTriggerHandler { public static void CreateAccounts(List<Account> acclist){ for(Account a:acclist){ if(a.ShippingState!=a.BillingState){ a.BillingState=a.ShippingState; } } } }
trigger AccountTrigger on Account (before insert) { if (Trigger.isBefore && Trigger.isInsert) { AccountTriggerHandler.CreateAccounts(Trigger.new); } }
@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'); } } } }And this error :
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.
Anyone can help me please??
Thanks!
Fr eg,
a.ShippingState = a.BillingState;
Please let me know if it helps.
All Answers
Fr eg,
a.ShippingState = a.BillingState;
Please let me know if it helps.
I'm having the same issue pertaining to this same challenge. I've included my coding below for the Account Trigger on Account object .
I'm not clear on where the Shipping State = a.BillingState should be included in the method. Could you, or someone, clarify for me. Thanx
AccountTriggerHandler is the class that has the "business logic". For this reason, you have to include the comparison in this method or class.
AccountTrigger occurs when a new account is created and is bind with the business logic class. I see this execirse / triggers in this way.
Here is the Apex class:
And trigger code is as below:
Hope this helps!!
Could someone clarify please? Thanks
The 'AccountTrigger' Trigger does not appear to be working correctly. Inserted records did not have matching BillingState and ShippingState values
My account trigger code is:
trigger AccountTrigger on Account (before insert)
{
if (Trigger.isBefore && Trigger.isInsert) {
AccountTriggerHandler.CreateAccounts(Trigger.new);
}
}
My Account Trigger Test code is:
@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');
}
}
}
}
My Account Trigger Handler code is:
public class AccountTriggerHandler
{
public static void CreateAccounts(List<Account> acclist)
{
for(Account account : acclist)
{
if(account.ShippingState!=account.BillingState)
{
account.ShippingState = account.BillingState = 'CA';
}
}
}
}
Please help. I'm really stuck on this one! My Account Trigger seems to be the same as the example above - not sure what I'm missing here. I definitely executed the Run Alll Tests before trying to complete the challenge.
Thanks,
Justin
System.AssertException: Assertion Failed: ERROR: Expected: CA, Actual: null
There are currently no Account/Customer records in my instance. I deleted them all for testing purposes of this Apex class.
Anyone can help me, please??
Thanks in advance.
Thanks and regards,
Samyuktha.
System.AssertException: Assertion Failed: ERROR: Expected: CA, Actual: null
Even I am also getting the error, while doing this excercise, Please find the below screenshot for your referece
Can anyone help me why I am getting this error
for(Account a:acclist){
I have seen it in several examples in Trailhead but I still don't know what is this for.
Tks in advance,
public class AccountTriggerHandler {
public static void CreateAccounts(List<Account> acclist)
{
for(Account account : acclist)
{
if(account.ShippingState!=account.BillingState)
{
account.ShippingState = account.BillingState; // you need to update ShippingState
}
}
}
}
Test Class
@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 accts;
Test.stopTest();
List<Account> verifyAccts = [SELECT id FROM Account WHERE ShippingState = 'CA' ];
System.assertEquals(200, verifyAccts.size());
}
}
Please let me know if it helps.
I'm getting this error.
I am getting this error for your code.
any suggestions please.
if(a.ShippingState!=a.BillingState){
a.ShippingState=a.BillingState;
}
Note: Remember that after creating the Test case in Developer Consol go to Test -> Run All and than only submit on the Trail Head.
Hi All, here is the solution:
+ Bruno Souza 7
for(Account a:acclist){ ... } represents a for-each loop - this is used to loop through elements in an array.
A for-each loop has the following syntax:
In our case we are looping for-each Account (type) from the List of Accounts collection (array).
Hope this helps!
Apex class -
Apex Trigger -
Apex Test Class -
Regards,
Parikhit Sarkar.
Apex class:
Apex Trigger: Apex Test Class:
For those who are getting this error is assert statement
"System.AssertException: Assertion Failed: ERROR: Expected: CA, Actual: null"
You need to re-query the account records to get the updated values.
---------------------------------------------------
Test Class
@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 accts;
Test.stopTest();
List<Account> verifyAccts = [SELECT id FROM Account WHERE ShippingState = 'CA' ];
System.assertEquals(200, verifyAccts.size()); b
}
}
if(account.ShippingState!=account.BillingState)
{
account.ShippingState = account.BillingState = 'CA';
}
corrected code is
if(account.ShippingState!=account.BillingState)
{
account.ShippingState = account.BillingState;//it directly updates the ShippingState, no need to put =CA coz shippingstate=CA will be handled in the test class
}
Hope it helps :-)
Hey folks adding the correct answer to the question-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.
Here is the below code-
AccountTriggerHandler apex class-
public class AccountTriggerHandler {
public static void CreateAccounts(List<Account> accnt){
List<Account> act = new List<Account>();
for(Account a: accnt){
if(a.ShippingState!=a.BillingState){
a.ShippingState = a.BillingState;
act.add(a);
}
}
}
}
AccountTrigger-trigger file
trigger AccountTrigger on Account (before insert, before update, before
delete, after insert, after update, after delete, after undelete) {
if (Trigger.isBefore && Trigger.isInsert)
AccountTriggerHandler.CreateAccounts(Trigger.new);
}
AccountTriggerTest -apex class
@isTest
public class AccountTriggerTest {
@isTest
public static void TestCreateNewAccount(){
//Create 200 new accounts for billing state as CA
List<Account> accnt = new List<Account>();
for(Integer i=0 ; i < 200 ; i++){
Account a = new Account(Name = 'test' +i);
a.BillingState = 'CA';
accnt.add(a);
}
//Perform test
test.startTest();
insert accnt;
test.stopTest();
//Verify test
List<Account> ant = [Select Id From Account where ShippingState = 'CA' ];
System.assertEquals(200, ant.size());
}
}
Kindly share and hit that below like button.
public 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' ]); } }
//I am still getting the same error, could anyone please help?
public class AccountTriggerHandler {
public static void CreateAccounts(List<Account> acclist){
for(Account a:acclist){
if(a.ShippingState != a.BillingState){
a.ShippingState = a.BillingState;
}
}
}
}
Trigger Class:
trigger AccountTrigger on Account (before insert) {
if (Trigger.isBefore && Trigger.isInsert) {
AccountTriggerHandler.CreateAccounts(Trigger.New);
}
}
Test Class:
@isTest
public class AccountTriggerTest {
@isTest static void testAccountTriggerHandler(){
// Create 200 test accounts with BillingState = 'CA'
List<Account> accList = new List<Account>();
for(integer i=0; i<200; i++){
accList.add (new Account(Name = 'Test Account' + i, BillingState = 'CA'));
}
// Insert the test accounts
Test.startTest();
insert accList;
Test.stopTest();
// Verify that all 200 accounts have ShippingState = 'CA'
List<Account> updatedAccList = [select id, name, BillingState, ShippingState from Account where id in:accList];
for(Account acc : updatedAccList){
System.assertEquals('CA', acc.ShippingState, 'ShippingState should be "CA" for account ' + acc.Name);
}
}
}
public class AccountTriggerHandler {
public static void CreateAccounts(List<Account> accList) {
for (Account acc : accList) {
if(acc.ShippingState!=acc.BillingState)
{
acc.ShippingState = acc.BillingState;
}
}
}
}
Trigger Class by Apoorva Pratap Singh:
trigger AccountTrigger on Account (before insert) {
if (Trigger.isBefore && Trigger.isInsert) {
AccountTriggerHandler.CreateAccounts(Trigger.New);
}
}
Test Class by Apoorva Pratap Singh :
@isTest
private class AccountTriggerTest {
@isTest static void TestCreate200NewRecords() {
// 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> verifyAccts = [SELECT Id FROM Account WHERE ShippingState = 'CA'];
System.assertEquals(200, verifyAccts.size());
}
}
This works fine if you get any error reply me.