You need to sign in to do that
Don't have an account?
AVINASH UPADHYA
Apex Triggers
Hi All,
Can anyone please help me with bellow challenge.
Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field.
For this challenge, you need to create a trigger that, before insert or update, checks for a checkbox, and if the checkbox field is true, sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).The Apex trigger must be called 'AccountAddressTrigger'.
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.
For above challenge i tried with following code but looks like it doesnt mach the requirement.
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c = true) {
/*ShippingPostalCode = BillingPostalCode;*/
}
}
}
Please help me
Can anyone please help me with bellow challenge.
Create an Apex trigger for Account that matches Shipping Address Postal Code with Billing Address Postal Code based on a custom field.
For this challenge, you need to create a trigger that, before insert or update, checks for a checkbox, and if the checkbox field is true, sets the Shipping Postal Code (whose API name is ShippingPostalCode) to be the same as the Billing Postal Code (BillingPostalCode).The Apex trigger must be called 'AccountAddressTrigger'.
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.
For above challenge i tried with following code but looks like it doesnt mach the requirement.
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c = true) {
/*ShippingPostalCode = BillingPostalCode;*/
}
}
}
Please help me
Try this code:-
Thanks
Anil.B
All Answers
Try this code:-
Thanks
Anil.B
In your code a.ShippingPostalCode means ShippingPostalCode is a field from Account Std. object right? If so when i check the fields(Setup --> Customize --> Accounts --> Fields) i couldnt find ShippingPostalCode in the list of fields. Can you please help me to find the list of fields for Accounts Std objects!
Thanks in advance
You can find list of apex available fields from the developer console
Open Developer Console > File > Open > Objects > double click on Account
Thanks
trigger AccountAddressTrigger on Account (before insert,before update) {
for(Account a : Trigger.new){
if(a.Match_Billing_Address__c = True)
a.ShippingPostalCode = a.BillingPostalCode;
}
}
Regards,
Ghanshyam
a.Match_Billing_Address__c = True should be a.Match_Billing_Address__c == True
Hi I tried Anil's code and the error is:
Error: Compile Error: Variable does not exist: ShippingPostalCode at line 5 column 13
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account acc : Trigger.new){
If (acc.Match_Billing_Address__c = true && acc.BillingPostalCode!=Null) {
acc.ShippingPostalCode = acc.BillingPostalCode;
}
}
}
After puttinhg the code.Go to accounts tab,pick up any record and edit.
set TRUE to Match_Billing_Address__c ,fill both BillingPostalCode, ShippingPostalCode with some different text.Click Save.
Now see the text of BillingPostalCode and ShippingPostalCode is same.
It's working with below code
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;
}
}
}
Setup->customize->Accounts->fields->Custom fields and relationships
Select custom field --> field type (Checkbox) and enter name as "Match Billing Address" , checkbox (checked)
Save.
Now write the below code, it is working without any errors
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a: Trigger.new)
{
if(a.Match_Billing_Address__c==True)
{
a.ShippingPostalCode=a.BillingPostalCode;
}
}
}
ShippingPostalCode updated..
If(act.Match_Billing_Address__c && act.BillingPostalCode!= null)
Its working fine for me :-
Challenge Not yet complete... here's what's wrong:
Setting 'Match_Billing_Address__c' to false updated the records anyway. The trigger should only act when Match_Billing_Address__c is true.
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c == True && a.BillingPostalCode!=Null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
Try it.. it works.. 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'. Create this custom field and it should be unchecked while creating this custom field.
trigger AccountAddressTrigger on Account (before insert, before update)
{
for(Account a : Trigger.new)
{
If (a.Match_Billing_Address__c == true)
{
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
Anil's IF conditions are the most solid, there is just a typo in the a.Match_Billing_Address__c = true.
One = sign is trying to assign the value of true, but this can't be done in a conditional. You need to use two == to represent a comparison:
You can use code below to solve your problem:
Hope this helps!!
when i update the billing postal code then it works...
but when i update the shipping postal code then it doesn't work.
anybody tell me..
Hi, It might sound silly but why did we use
for (Account acc : Trigger.new)
and why not
for (Account acc : Trigger.old) ?
I am asking this because I tried with and the latter and got stuck for half an hour.
Appreciate if anyone clarifies, Thanks in advance! :)
if u work on new account so go for Trigger.new
and trigger.old we are used for old account..
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c ==true && a.BillingPostalCode!=Null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
Also, once I've checked the Match Account Billing checkbox, completed the Billing Zip field, and saved, if I go back in and delete the Billing Zip but leave the Match Billing Address checkbox checked, when I save it automatically unchecks the Match Account Billing checkbox; why?
Thanks for your time and help.
Best,
Brooks
Trigger Coding :- Challenge 1
trigger AccountAddressTrigger on Account (before insert, before update)
{
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c == true) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
--->Error: Compile Error: Invalid field Match_Billing_Address__c for SObject Account at line 3 column 4
Thats my code:
trigger AccountAddressTrigger on Account (before insert,before update) {
for(Account a : Trigger.new){
if(a.Match_Billing_Address__c = True)
a.ShippingPostalCode = a.BillingPostalCode;
}
}
did anyone get this error ?!
Challenge Not yet complete... here's what's wrong:
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, AddRelatedRecord: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Discount_Percent__c, Discount_Percent_Status__c]: [Discount_Percent__c, Discount_Percent_Status__c] Trigger.AddRelatedRecord: line 23, column 1: []
Here is my code ( i already created the custom checkbox )
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account act: Trigger.new)
{
if(act.Match_Billing_Address__c==True)
{
act.ShippingPostalCode=act.BillingPostalCode;
}
}
}
Error: Apex trigger ParentTrigger caused an unexpected exception, contact your administrator: ParentTrigger: execution of AfterUpdate
caused by: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_ACCESS_LEVEL, (ACC_SHARE.ACC_ACCESS_LEVEL, ACC_SHARE.OPP_ACCESS_LEVEL, ACC_SHARE.CASE_ACCESS_LEVEL, ACC_SHARE.CON_ACCESS_LEVEL) (Account, Opportunity, Case Levels, Con Levels (Read, Read, Edit, Read) are below organization levels (Edit, Edit, Edit, ControlledByParent)): []: Class.Accountsharingtrigger.recordshare: line 12, column 1
Got a fix for this?
in best answer
a.Match_Billing_Address__c = true
a.Match_Billing_Address__c == true
trigger AccountAddressTrigger on Account (before insert, before update) {
for (Account a : Trigger.new) {
if (a.ShippingPostalCode == null) {
a.ShippingPostalCode = ' ';
}
if (a.Match_Billing_Address__c==true && a.BillingPostalCode != null) {
a.ShippingPostalCode = a.BillingPostalCode;
system.debug('shipping postal code is ' + a.ShippingPostalCode + 'Billing postal code ' + a.BillingPostalCode);
}
}
}
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c ==true && a.BillingPostalCode!=Null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
for(Account acc:Trigger.New){
if(acc.Match_Billing_Address__c && (acc.BillingPostalCode != null || acc.BillingPostalCode!=' ' )){
acc.ShippingPostalCode = acc.BillingPostalCode;
}
}
}
for(Account acc : Trigger.new) {
If (acc.Match_Billing_Address__c = true && acc.BillingPostalCode!=Null) {
acc.ShippingPostalCode = acc.BillingPostalCode;
}
}
}
for(Account ac:Trigger.new)
{
if(ac.Match_Billing_Address__c ==true && ac.BillingPostalCode!=Null)
{
ac.ShippingPostalCode =ac.BillingPostalCode;
}
else{
ac.BillingPostalCode=ac.BillingPostalCode;
}
}
}
for (Account acc:Trigger.new){
if(ac.Match_Billing_Address__c ==true && ac.BillingPostalCode!=Null)
{
ac.ShippingPostalCode =ac.BillingPostalCode;
}
}
}
This code works perfectly fine.
trigger AccountAddressTrigger on Account (before insert, before update)
{
for(Account acc:trigger.new)
{
if(acc.match_Billing_Address__c==True)
{
acc.ShippingPostalCode = acc.BillingPostalCode;
}
}
}
#lopsact
The following code is working fine for me.
Regars,
Venkat Kattamuru.
Best answer chosen is almost correct as it is missing '=' symbol,
trigger AccountAddressTrigger on Account (before insert, before update)
{
for(Account a : Trigger.new)
{
If (a.Match_Billing_Address__c == true)
{
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
Error Message:
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: REQUIRED_FIELD_MISSING, Required fields are missing: [First_Name__c, Last_Name__c]: [First_Name__c, Last_Name__c]
Module: Apex Triggers
Sub Module: Get Started with Apex Triggers
Link: https://trailhead.salesforce.com/content/learn/modules/apex_triggers/apex_triggers_intro
*/
trigger AccountAddressTrigger on Account (before insert, before update) {
for(Account accObj: Trigger.new){
if(accObj.Match_Billing_Address__c && String.isNotBlank(String.valueOf(accObj.BillingPostalCode))){
accObj.ShippingPostalCode = accObj.BillingPostalCode;
}
}
}
you will be able to get all the fields name from developer console by follwing below steps
Open Developer Console > File > Open > Objects > double click on Account
Thanks
The longer answer is that the ShippingPostalCode / BillingPostalCode field is a part of BillingAddress'compound' field (introduced in API v30.0, aka the Spring '14 release), which has a bunch of limitations which are outlined in the documentation on compound fields. One of those limitations is that compound fields are read-only. If you want to set values for an address, you need to set values for the individual fields (e.g. BillingStreet, BillingCity, BillingPostalCode, etc...)
Personally, I've attempted to use compound fields in the past, but found it to be more trouble than it was worth.
Thanks
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;
}
}
}
then try this code
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;
a.ShippingCity=a.BillingCity;
a.ShippingStreet=a.BillingStreet;
a.ShippingState=a.BillingState;
a.ShippingCountry=a.BillingCountry;
}
}
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: FIELD_CUSTOM_VALIDATION_EXCEPTION, Duplicate record found: [Name]
for(Account a:Trigger.new){
if(a.Match_Billing_Address__c==true){
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
We updated an account that had 'Match_Billing_Address__c' set to false. We expected the trigger not to fire, but it did. Make sure the trigger fires only if 'Match_Billing_Address__c' is true.
Please first create checkbox field "Match_Billing_Address__c"and the follow below refernce code in apex trigger
trigger AccountAddressTrigger on Account (before insert, before update) {
if(Trigger.isInsert){
for(Account a : Trigger.new){
If (a.Match_Billing_Address__c == true && a.BillingPostalCode!=Null) {
a.ShippingPostalCode = a.BillingPostalCode;
}
}
}
}
getting this error can anyone help me
this is code below
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;
}
}
}