function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
awoelfelawoelfel 

Tigger Help, Please

I am new to trigger writing having only written a few and I haven't found a good example to try and understand how to write this trigger. I am trying to write a trigger to update a lookup field (Zipcode_Lookup__c) on our Account object with the Account BillingPostalCode field if it is populated.

 

Here is what I have so far:

 

trigger territoryZip on Account (after update){
    if(trigger.isUpdate){
    for(Account a : trigger.new){
    a.Zipcode_Lookup__c = Account.getBillingPostalCode ();
    }
   }
  }

 

I've tried various things such as complicated mapping values to this very simple trigger and I consistantly receive Compile Errors with the Zipcode_Lookup__c = Account.BillingPostalCode entries.

 

Here is another version I've tried:

 

trigger

territoryZip onAccount (afterinsert, afterupdate){

 

for (Account acc : Trigger.New){

List<

Account> ListAccount = [SELECT BillingPostalCode from Account where AccountID =: acc.id];

 

for (Accountacc2 : listAccount2){

acc2.Zip_Code_Lookup = acc.BillingPostalCode;

}

updatelistAccount2;

}

}

 

I would appreciate any advice as I would really like to learn this concept of updating lookup fields on the same object.

 

Thank you!

 

VJSFDCVJSFDC

hi,

 

You missed couple of this.

 

 

Please find the below script:

 

for (Account acc : Trigger.New){

Map<Id, Account> ListAccount = new Map<Id, Account>([SELECT BillingPostalCode from Account where AccountID =: acc.id]);

}

 

 


for (Account acc2 : listAccount2){
acc2.Zip_Code_Lookup = ListAccount.get(acc2.Id).BillingPostalCode;

}
update listAccount2;

 

 

hope it helps.

 

Starz26Starz26
First, you cannot update a field in the trigger context if the trigger is an after update.

Second, the offered solution place a select statement inside a for loop and is not best practice.....It also declares the map each time effectively setting the vales to one record and you will end up with just the last record in the map and not a list. Basically you get a bunch of SQL executions and end up with just the final record in ListAccount

Try
 
trigger territoryZip on Account (before update){
    if(trigger.isUpdate){
    for(Account a : trigger.new){
    If(isnull(a.BillingPostalCode)==false)
zipcode_lookup__c = billingpostalcode
    }
   }
  }
 
If this must be an after trigger then I will help you with that but the before is the best way if possible in this case
awoelfelawoelfel

Thank you so much for the reply. I ran into a couple of errors when saving this trigger variation and changed it a little to resolve the "field doesn't exist" errors. But, I am receiving a compile error on the isnull(String) I haven't been able to research a fix for:

 

trigger territoryZip on Account (before update) {

     if(trigger.isUpdate) {

     for (Account a : trigger.new) {

     If (isnull (a.BillingPostalCode) == false)

     a.Zipcode_Lookup__c = a.BillingPostalCode;

     }

  }

}

 

The Error is: Compile Error: Method does not exist or incorrect signature: isnull(String) at line 4 column 8

 

Thank you again for any help.

 

 

awoelfelawoelfel

Thank you so much for your reply. I tried the code you've suggested:

 

trigger territoryZip on Account (after insert, after update){
for (Account acc : Trigger.New){
Map<Id,Account> ListAccount = new Map<Id,Account>([SELECT BillingPostalCode FROM Account WHERE AccountID =: acc.id]);
}

for(Account acc2:listAccount2){
acc2.Zipcode_Lookup__c=ListAccount.get(acc2.Id).BillingPostalCode;
}
update listAccount2;
}

 

And received a compile error while saving - Variable does not exist: LlistAccount2 at line 6 column 18

 

 

Then I've fooled around with your concepts and wrote this:

 

 

trigger territoryZip on Account (before insert, before update){

Set<Id> accountIds = new Set<Id>();

for (Account acc : Trigger.New)

accountIds.add(acc.ID);

 

Map <Id,Account> ListAccount = new Map<Id,Account> (

[SELECT BillingPostalCode FROM Account WHERE id in : accountIds]);

 

for (Account acc : Trigger.New)

acc.Zipcode_Lookup__c = ListAccount.get(acc.ID).BillingPostalCode;

}

 

This trigger saves fine but I recieve an error both with a new record and when trying to update an existing record:

 

Apex trigger territoryZip caused an unexpeced exception: execution of BeforeUpdate caused by: System.STrigException: Invalid id: 55124: Trigger.territoryZip: line 10, column 1

 

So I made sure that 55124 was in the Zipcode lookup oject and it is. I think this is so close but I'm not sure what to try next.

 

Thanks again for your help.

 

 

VJSFDCVJSFDC

hi

 

Might be this code will help if its not bulk trigger you are calling:

 

trigger UpdatingZipCode on Account (before insert,before update) {
if(Trigger.isBefore)
{
    if(Trigger.isInsert)
    {
        for(Account acnt: Trigger.new)
        {
            if(acnt.BillingPostalCode!= null)
            {
                acnt.ShippingPostalCode = acnt.BillingPostalCode;
            }
        }
    }
    
    if(Trigger.isUpdate)
    {
          for(Account acnt: Trigger.new)
        {
            if(acnt.BillingPostalCode!= null)
            {
                acnt.ShippingPostalCode = acnt.BillingPostalCode;
            }
        }
      
    
    }

awoelfelawoelfel

This works both for the insert and update using the ShippingPostalCode field as the one to populate from the BillingPostalCode. When I tried it with my custom lookup field Zipcode_Lookup__c, I received this error when saving the Account record:

 

Apex trigger UpdatingZipCode2 caused an unexpected exception, contact you administrator: UpdatingZipCode: execution of AfterInsert caused by: System.StringException: Invalid id: 32401: Trigger.UpdatingZipCode: line 4, column 1

 

trigger UpdatingZipCode on Account (after insert){
for(Account acnt: Trigger.new){
if(acnt.BillingPostalCode!=null)
acnt.Zipcode_Lookup__c = acnt.BillingPostalCode;
}
}

 

I really appreciate your help. Thank you.

 

awoelfelawoelfel

Here is the code that worked to populate my lookup field:

 

trigger ZipcodeLookup2 on Account (before insert, before update) {
List<String> BillingPostalCodes = new List<String>();
for (Account a:Trigger.new)
{
BillingPostalCodes.add(a.BillingPostalCode);
}
List <Zip_Code__c> ZipCodeList = [Select ID from Zip_Code__c where Name in :BillingPostalCodes];
for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (Trigger.new[i].BillingPostalCode != null)
{
Trigger.new[i].Zipcode_Lookup__c = ZipcodeList[i].ID;
}
else
{
Trigger.new[i].Zipcode_Lookup__c = null;
}
}
}

 

Thanks again to Jake Gmerek who advised that Lookup fields store the ID of the record that they are looking up, but display the name.