0

Learning Apex here. I am trying to get this issue resolved where in I have moved the comparing logic to check if the billing city has got changed and if so do something with related contacts of that account.

My trigger handler class is

public static void updateContactBillingAddressNewWay(Map<Id,Account> OldMap, Map<Id,Account> NewMap){
    Set<Id> accIdsThatGotBillingAddressChangedNewWay = new Set<Id>();
    for(Account Acc: newMap.values()){
        **if(OldMap.get(Acc.BillingCity != NewMap.get(Acc.BillingCity))){**
            accIdsThatGotBillingAddressChangedNewWay.add(Acc.Id);
        }
    }
    
    // Query all contacts of the accounts that got their billing address changed.
    List<Account> accWithContacts = [SELECT id,name,BillingCity,(SELECT id, name FROM Contacts) FROM Account WHERE Id in: accIdsThatGotBillingAddressChangedNewWay];
    List<Contact> conListToUpdate = new List<Contact>();
    for(Account acc: accWithContacts){
        List<Contact> consOfTheLoopedAccount = acc.Contacts;
        for(Contact con : consOfTheLoopedAccount){
            con.MailingCity = acc.BillingCity;
            conListToUpdate.add(con);
        }
    }
    if(conListToUpdate.size() > 0)
        update conListToUpdate;
}

It throws error saying

Comparison arguments must be compatible types: String, Account (48:27)

Can I get some guidance on how to compare in trigger handler class?

Trigger

if(Trigger.isAfter && Trigger.isUpdate){
     AccountTriggerHandler.updateContactBillingAddressNewWay(Trigger.oldMap,Trigger.newMap);
}
1
  • 1
    Separate from the issue in your querstion. You do not need to check if a list is empty before performing DML on it. Salesforce is smart enough to handle that situation automatically. Also, a common "code style" is to have variable names start with lower case letters (so OldMap and NewMap should be oldMap and newMap). I would advise to never omit curly braces for for, if, else if, or else. Field names in queries should have a space after the comma. Whatever code style you use, the most important thing is to be consistent.
    – Derek F
    Commented Jul 25, 2023 at 1:14

1 Answer 1

1

The Map data type has two parts to it, the key and the value. In your case the key is an Id (specifically an account id here), and the value is the Account.

When using a map, you give it the key (the Id), and it returns the value associated with that key (the Account).

So this is wrong
OldMap.get(Acc.BillingCity != NewMap.get(Acc.BillingCity))

And proper syntax would be
OldMap.get(Acc.Id).BillingCity != NewMap.get(Acc.Id).BillingCity

Almost all programming languages evaluate things in left-to-right order. The thing to realize here is that lines of code with multiple "things" in them get partially evaluated before the next "thing" is evaluated.

To illustrate, in pseudocode...

// Start
OldMap.get(Acc.Id).BillingCity != NewMap.get(Acc.Id).BillingCity

// Step 1, evaluate OldMap.get(), given an Account Id
// results in an Account
<The old Account>.BillingCity != NewMap.get(Acc.Id).BillingCity

// Step 1.5
// Account has a BillingCity field, so <The old Account>.BillingCity is
//   valid syntax

// Step 2, we see the != operator
// We've only evaluated one side of the operator
// We need to continue evaluating before we can resolve this operator

// Step 3, evaluate NewMap.get(), given an Account Id
// results in an Account
<The old Account's BillingCity> != <The new Account>.BillingCity

// Step 3.5
// Account has a BillingCity field, so <The new Account>.BillingCity is
//   valid syntax

// Step 4, now we can evaluate the != operator
// The thing on the left is a string
// The thing on the right is a string
// These two types can be compared together
// The result is a boolean
<The old Account's BillingCity> != <The new Account's BillingCity>

Since you're looping over the values in NewMap though, you don't need to do NewMap.get(Acc.Id).BillingCity. You can just use Acc directly, and replace that bit with Acc.BillingCity.

So the final form of this if statement would be
if(OldMap.get(Acc.Id).BillingCity != Acc.BillingCity)

1
  • Thanks @Derek F for coding format suggestions. Really helpful as beginner. Commented Jul 25, 2023 at 1:47

You must log in to answer this question.

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