1

I am new to Apex coding any help would be greatly appreciated. This is an update trigger and I am trying to check if the hard-coded field lists were updated.

However, I am getting an error in the initialization of the fieldList (the fields I am hard-coding here are the ones I want to check if they were updated).

Error Line

sObjectField[] fieldList = new sObjectField[]{
    Account.Name,Account.Industry
};

Error Message

Error received: Initial expression is of incorrect type, expected: Schema.SObjectField but was: String.

Complete Code

trigger UpdateChildOrg on Account (after update) {
    Set<id> AccountIdSet=new Set<id>();
    Schema.sObjectField[] changedFields = new list<Schema.sObjectField> ();
    for(Account account:trigger.new) {
        accountIdSet.add(account.id);
        sObjectField[] fieldList = new sObjectField[] {
            Account.Name, Account.Industry
        };
        system.debug(fieldList);
        SObject o1 = Trigger.oldMap.get(account.id);
        SObject o2 = Trigger.newMap.get(account.id);
        system.debug('o1 is '+ o1);
        system.debug('o2 is '+ o2);

        for (Schema.sObjectField field : fieldList) {
            system.debug('field is : '+field);
            Object v1 = o1.get(field);
            Object v2 = o2.get(field);  
                    
            if (v1 != v2) {
                changedFields.add(field);
            }
        }
        system.debug('changed fields are'+changedFields);
        if(changedFields.size()>0){
         HTTPAccountCallout.updateOrder(account.id,changedFields);
        }
    }
}

What am I doing wrong? How can I fix it?

1 Answer 1

3

The issue is that your choice of variable name is shadowing the Account SObject type.

Most things in Apex are case-insensitive, so Account, account, and ACCOUNT (for example) are all equivalent. The main things in apex that are case-sensitive are things that rely on hashes such as Sets and the keys of a Map.

In your for loop for(Account account :trigger.new){ ..., Account.Name is referring to the value of the Name field on your account loop variable. If you were outside of the for loop (or had chosen a different name for your variable), Account.Name would instead give you a Schema.SObjectField.

The quick way to handle this would be to use the fully qualified identifier to get at the metadata of the Account SObject, e.g. Schema.Account.Name

There also appears to be little reason for you to declare and initialize that List inside of your loop (since it does not change from Account to Account that you process). It would be good practice to remove that from your loop and put it on some line above the loop.

You must log in to answer this question.

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