You need to sign in to do that
Don't have an account?
Nandhini s 11
Need help with triggers
Hi Guys,
I'm doing a hands-on exercise on triggers. The requirement is:
When a Billing Address is modified in accounts, get the new Postal Code. Then check which Contacts on the Account are outside that Postal Code. If 1 or more Contacts are outside of the Postal Code, mark Out_of_Zip as TRUE.
I've written the below trigger but the Out_of_Zip field is not getting updated. I'm not sure how to check if the trigger is firing or not.
Can someone tell me where to check the logs for triggers ?
trigger OutOfZipTrigger on Account (before update) {
for(Account a: trigger.new){
Account oldAccount = Trigger.oldMap.get(a.id);
if(a.BillingAddress != oldAccount.BillingAddress )
{
List<contact> con = [select id,MailingPostalCode from contact where accountId =: a.id] ;
for(contact c: con){
if(a.BillingPostalCode != c.MailingPostalCode){
a.Out_of_Zip__c = True;
//acc.add(a);
}
}
}
}
}
I'm doing a hands-on exercise on triggers. The requirement is:
When a Billing Address is modified in accounts, get the new Postal Code. Then check which Contacts on the Account are outside that Postal Code. If 1 or more Contacts are outside of the Postal Code, mark Out_of_Zip as TRUE.
I've written the below trigger but the Out_of_Zip field is not getting updated. I'm not sure how to check if the trigger is firing or not.
Can someone tell me where to check the logs for triggers ?
trigger OutOfZipTrigger on Account (before update) {
for(Account a: trigger.new){
Account oldAccount = Trigger.oldMap.get(a.id);
if(a.BillingAddress != oldAccount.BillingAddress )
{
List<contact> con = [select id,MailingPostalCode from contact where accountId =: a.id] ;
for(contact c: con){
if(a.BillingPostalCode != c.MailingPostalCode){
a.Out_of_Zip__c = True;
//acc.add(a);
}
}
}
}
}
Try the below trigger:
Address is the CompounD Field try to compare address field like Below:
trigger OutOfZipTrigger on Account (before update) {
Set<Id> accIds = new Set<Id>();
List<Contact> conList = new List<Contact>();
for(Account a: trigger.new){
if(a.BillingStreet != trigger.oldMap.get(a.id).BillingStreet || a.BillingCity!= trigger.oldMap.get(a.id).BillingCity ||
a.BillingState != trigger.oldMap.get(a.id).BillingState || a.BillingPostalCode != trigger.oldMap.get(a.id).BillingPostalCode)
{
accIds.add(a.Id);
}
if(!accIds.isEmpty())
{
conList = [Select Id,AccountId,MailingPostalCode from contact where AccountId IN: accIds];
}
for(contact con : conList)
{
Account accrec = trigger.newMap.get(con.AccountId);
if(con.MailingPostalCode != accrec.BillingPostalCode)
{
accrec.Out_of_Zip__c = True;
}
}
}
}
Thanks,
Maharajan.C
Please Try the below trigger.
Though Maharajan's solution was perfect ,but the trigger was not bulkified which is not the best practice as far as apex is considered.
here is Also if the newMap does not work ..
make a new map of account and add the details in them.
Thank you
Akshat Ajmera