You need to sign in to do that
Don't have an account?
Anitha J J 2
Trailhead ApexTrigger unit1/2 Executing the trigger did not work as expected.
Hi All,
Below are the requirements to complete Challenge
The Account object will need a new custom checkbox that should have the Field Label 'Match Billing Address' and Field Name of 'Match_Billing_Address'. The resulting API Name should be 'Match_Billing_Address__c'.
With 'AccountAddressTrigger' active, if an Account has a Billing Postal Code and 'Match_Billing_Address__c' is true, the record should have the Shipping Postal Code set to match on insert or update.
I have created new custom Checkbox(MatchBillingAddress) and Below is my trigger:
trigger AccountAddressTrigger on Account (before update,before insert)
{
List<Account> list1=[SELECT Id,Match_Billing_Address__c,ShippingPostalCode,BillingPostalCode from Account];
for(Integer i=0;i<list1.size();i++)
{
if(list1[i].Match_Billing_Address__c=true && list1[i].BillingPostalCode!=NULL)
{
list1[i].ShippingPostalCode=list1[i].BillingPostalCode;
update list1[i];
}
}
}
I got Error like : Executing the trigger did not work as expected.
Can anyone please help me to achieve this. Thanks
Below are the requirements to complete Challenge
The Account object will need a new custom checkbox that should have the Field Label 'Match Billing Address' and Field Name of 'Match_Billing_Address'. The resulting API Name should be 'Match_Billing_Address__c'.
With 'AccountAddressTrigger' active, if an Account has a Billing Postal Code and 'Match_Billing_Address__c' is true, the record should have the Shipping Postal Code set to match on insert or update.
I have created new custom Checkbox(MatchBillingAddress) and Below is my trigger:
trigger AccountAddressTrigger on Account (before update,before insert)
{
List<Account> list1=[SELECT Id,Match_Billing_Address__c,ShippingPostalCode,BillingPostalCode from Account];
for(Integer i=0;i<list1.size();i++)
{
if(list1[i].Match_Billing_Address__c=true && list1[i].BillingPostalCode!=NULL)
{
list1[i].ShippingPostalCode=list1[i].BillingPostalCode;
update list1[i];
}
}
}
I got Error like : Executing the trigger did not work as expected.
Can anyone please help me to achieve this. Thanks
list1[i].Match_Billing_Address__c=true, you are assigning value as true instead of comparing it.
You should consider following points in this implementation:
You dont need to query account records, as you can get current set of records through trigger.new. And also dont explicitly update account list as you are populating field in before insert or update trigger.
Can update code to below:
for(Account acc: Trigger.new)
{
if(acc.Match_Billing_Address__c && acc.BillingPostalCode!=NULL)
{
acc.ShippingPostalCode=acc.BillingPostalCode;
}
}
All Answers
list1[i].Match_Billing_Address__c=true, you are assigning value as true instead of comparing it.
You should consider following points in this implementation:
You dont need to query account records, as you can get current set of records through trigger.new. And also dont explicitly update account list as you are populating field in before insert or update trigger.
Can update code to below:
for(Account acc: Trigger.new)
{
if(acc.Match_Billing_Address__c && acc.BillingPostalCode!=NULL)
{
acc.ShippingPostalCode=acc.BillingPostalCode;
}
}
I m a newbie to Salesforce.Thank you so much it worked. Thanks for your immediate reply.
Trigger.new is there, so we dont have to update seperately by DML update statement. Is this correct?
Setup --> Customize--> Accounts-->Fields and Under Account Custom Fields & Relationships section ..you can create Checkbox filed by using Checkbox datatype.
In this trigger .. label of check box filed is 'Match Billing Address' and API Name of check box filed i.e ' Match_Billing_Address__c ' which is created automatically by the system. You can see that API Name used in trigger for validating purpose.
i m getting this error while creating trigger for Account!
please help me
Apex Trigger Code :
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account a:Trigger.New){
If (a.Match_Billing_Address__c && a.BillingPostalCode!= Null) {
a.BillingPostalCode= a.ShippingPostalCode ;
}
}
}
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account a:Trigger.New) {
If (a.Match_Billing_Address__c==true && a.BillingPostalCode!= Null) {
a.ShippingPostalCode= a.BillingPostalCode ;
}
}
}
Setting 'Match_Billing_Address__c' to true did not update the records as expected.]
for(Account a : trigger.new)
{
If (a.Match_Billing_Address__c == true && a.BillingPostalCode!=Null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}