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

Understanding Execution Context - Apex trigger Trailhead Issue.
Hello Community,
I'm having an issue with this particular challenge below:
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.
This is my Challenge Error:
Challenge Not yet complete... here's what's wrong:
The Trigger 'AccountTrigger' does not appear to be calling the AccountTriggerHandler class correctly or using isBefore or isInsert content variables.
These are my codings below :
AccountTriggerHandler

AccountTrigger - Trigger On Account Object

AccountTriggerTest

P.S. I'm pretty new to the developing with Apex so I'm learning and growing so the help is appreciated community.
I'm having an issue with this particular challenge below:
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.
This is my Challenge Error:
Challenge Not yet complete... here's what's wrong:
The Trigger 'AccountTrigger' does not appear to be calling the AccountTriggerHandler class correctly or using isBefore or isInsert content variables.
These are my codings below :
AccountTriggerHandler
AccountTrigger - Trigger On Account Object
AccountTriggerTest
P.S. I'm pretty new to the developing with Apex so I'm learning and growing so the help is appreciated community.
Please check below post for Test class level change also.
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBLFIA4
1) AccountTrigger Trigger 2) AccountTriggerHandler Apex class. 3) AccountTriggerTest Test class.
Please let me know if this will help you
Thanks
Amit Chaudhary
All Answers
AccountTrigger - Trigger On Account Object
If(Trigger.isBefore && Trigger.isInsert)
{
}
Please check below post for same issue. I hope that will help you.
1) https://developer.salesforce.com/forums/?id=906F0000000DBRhIAO
2)
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
Let us know if this will help you
Please check below post for Test class level change also.
1) https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBLFIA4
1) AccountTrigger Trigger 2) AccountTriggerHandler Apex class. 3) AccountTriggerTest Test class.
Please let me know if this will help you
Thanks
Amit Chaudhary
The 'AccountTriggerHandler' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge.
I have followed the above mentioned steps here, however, this error keeps coming up:
Challenge Not yet complete... here's what's wrong:
The 'AccountTriggerHandler' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge.
Any idea what could be wrong? Thank you.
Line: 6, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATES_DETECTED, Use one of these records?: []
replace this line of code -- If(Trigger.isBefore && Trigger.isInsert)
with If(Trigger.isBefore || Trigger.isInsert)
Instead of "and" operator for isBefore and isInsert one should use "or" operator.
I got error
Error:
Class.AccountHandler.CreateNewOpportunity: line 16, column 1
Stack Trace:
Class.AccountHandler.CreateNewOpportunity: line 16, column 1 Trigger.AccountTrigger: line 3, column 1
Here is my trigger test:
Hello, if you have an issue with:
The 'AccountTriggerHandler' class did not achieve 100% code coverage via your test methods. Make sure that you chose 'Run All' tests in the Developer Console at least once before attempting to verify this challenge.
Add a second test methode that the ShippingState is different from BillingState.
here is the AccountTriggerTest test classe:
@Amit Chaudhary
Hi Amit,
I followed the same code that you mentioned above, but I am getting error and not able to figure out what is causing this.
Please help me out.
Challenge not yet complete in shubham.kashyap1@tcs.com
There was an unexpected error in your org which is preventing this assessment check from completing: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.ShippingState Class.AccountTriggerHandler.CreateAccounts: line 22, column 1 Trigger.AccountTrigger: line 5, column 1: []
Thanks,
Shubham
I have read the post where almost everyone has use System.assertEquals('CA', a.ShippingState, 'ERROR'); as the way to verify the 200 Accounts with CA as billing state.
I have written in little different way and I guess that can be accepted as well.
My code here
It doesn't work for me. My code :
public class AccountTriggerHandler {
public static void CreateAccounts(List<Account> accounts) {
for(Account acc : accounts){
if(acc.BillingState != acc.ShippingState ){
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();
// Please query all record once again to check assert
List<Account> lstAccount = [select ShippingState from Account where BillingState = 'CA'];
System.assertEquals(200, lstAccount.size());
}
}
Error in trailhead challenge:
Triger 'AccountTrigger' is not working correctly. Make sure that the trigger inserts accounts with corresponding BillingState and ShippingState values.
I think you made a typo. Take a look at the test. You need a period, not a comma - Name='Test Account ' + i, BillingState = 'CA');