0

I am a bit of a Noob so this may be a Noob questions, but I am trying my first trigger to copy an address from the account shipping address into the account billing address fields when a custom checkbox, Billing_as_shipping__C is checked. Here is what I have and getting a compile error variable not found.

trigger AccountCopyBillingtoShipping on Account (before insert) {
   if (account.billing_as_Shipping__C = True){
       for (account accId : Trigger.new) {
          accId.ShippingState = accId.BillingState;
          accId.ShippingStreet = accId.BillingStreet;
          accId.ShippingCity = accId.BillingCity;
          accId.ShippingPostalCode = accId.BillingPostalCode;
          accId.ShippingCountry = accId.BillingCountry;
          Update accID;

       }
   }
}

4 Answers 4

1

I would recommend using the new process builder for this vs a trigger. With the process builder you can update multiple fields in the same action. I did notice you are attempting to do an update in your before trigger, since it is a before trigger you do not need to update as you are updating the record field values before it is actually saved.

2
  • I agree with Jenny, process builder is really cool and intuitive
    – EricSSH
    Commented Mar 30, 2015 at 20:50
  • Thank You, I will take a look at the process builder. I know that I can do this with workflow, but seemed like an easy trigger. :-) Commented Mar 30, 2015 at 21:35
0

As far as your compile error goes, you are attempting to reference "billing_as_Shipping__C" on the variable "account" and not the trigger records.

You can reference this field on the Account record that the trigger is referencing similarly to how you are for the other fields (e.g. BillingState):

for (account accId : Trigger.new) {
   if (accID.billing_as_Shipping__c) {
   // your code

Also, just based on your wording, it sounds like your use case is when someone checks this box on an existing Account? If yes, then you should change the trigger criteria to before update and not before insert.

As a side note, it would be much simpler to do this with a workflow.

7
  • Thank You for the input. This helps. Close, but still running into an error. The code not compiles, but I get a run time error when I try to test it. that says execution of BeforeUpdate caused by: System.SObjectException: DML statment cannot operate on trigger.new or trigger.old: Trigger.AccountCopyBillingtoShipping: line 9, column 1 Commented Mar 30, 2015 at 21:49
  • Here is the current code, I would like it to run on new accounts and when the Billing_as_shipping__C is checked on update., Commented Mar 30, 2015 at 21:49
  • trigger AccountCopyBillingtoShipping on Account (Before insert, Before update) { for (account accId : Trigger.new){ if (accId.billing_as_Shipping__C = True){ accId.ShippingState = accId.BillingState; accId.ShippingStreet = accId.BillingStreet; accId.ShippingCity = accId.BillingCity; accId.ShippingPostalCode = accId.BillingPostalCode; accId.ShippingCountry = accId.BillingCountry; Update accID; } } } Commented Mar 30, 2015 at 21:50
  • Remove the Update accId. The trigger will implicitly apply the changes you wish, you don't have to explicitly use a DML statement. Commented Mar 30, 2015 at 21:55
  • ok thank You. Not sure that I understand why, but I certainly will try it. Commented Mar 30, 2015 at 21:59
0

Thank You everyone for the help. This is the final trigger code:

trigger AccountCopyBillingtoShipping on Account (Before insert, Before update) {
    for (account accId : Trigger.new){
        if (accId.billing_as_Shipping__C == True){
            accId.ShippingState = accId.BillingState;
            accId.ShippingStreet = accId.BillingStreet;
            accId.ShippingCity = accId.BillingCity;
            accId.ShippingPostalCode = accId.BillingPostalCode;
            accId.ShippingCountry = accId.BillingCountry;  
        }
    }
}
6
  • Thank You Jenny,, quick question on formatting, what did you to so call out the code in Grey and section it out? Thank You Commented Mar 30, 2015 at 23:16
  • this can't be right ; line 3 should either be if (accId.billing_as_shipping__c) or if (accId.billing_as_shipping == true) - the former is preferred. Furthermore, the loop variable is of type Account and hence the variable name should be acc, not accId.
    – cropredy
    Commented Mar 31, 2015 at 1:40
  • So this functions well for copying the address values, but in testing I discovered that it sets the Billing_as_shipping__C checkbox to true on Insert and on Update. Any ideas? is there a chance that the If is being ignored and it is just taking that line as accId.Billing_as_shipping__C = True and updating that field?? Commented Apr 3, 2015 at 16:54
  • because the if statement: accId.billing_as_Shipping__C = True should be accId.billing_as_Shipping__C == True
    – cropredy
    Commented Apr 3, 2015 at 20:12
  • That Did the Trick. I guess I don't understand the difference between the two Equal functions @crop1645 Commented Apr 6, 2015 at 15:32
0

account is not an instance of any variable. You should check billing_as_Shipping__C for each inserted account. To do so, you should first iterate trigger inserted accounts.

List<Account> lstAccounts = Trigger.new;        

for(Account objAccount : lstAccounts) {
    if (objAccount.billing_as_Shipping__C = True) {
          objAccount.ShippingState = objAccount.BillingState;
          objAccount.ShippingStreet = objAccount.BillingStreet;
          objAccount.ShippingCity = objAccount.BillingCity;
          objAccount.ShippingPostalCode = objAccount.BillingPostalCode;
          objAccount.ShippingCountry = objAccount.BillingCountry;
    }
}
//update lstAccounts; DML Statements are not allowed in before triggers.
2
  • would that work on Update as well? Commented Mar 30, 2015 at 21:50
  • Yes, it will work on Update too.
    – ransommule
    Commented Mar 31, 2015 at 13:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .