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
Joseph DekisJoseph Dekis 

Displaying changes in field oldvalue vs new value apex in fieldset

Hello, I have been a bit perplexed by this issue and could use some help.  I have a method in before update trigger that is writing the changes of a changed field ( the field name, the oldvalue, and the newvalue) of a field changed within a fieldset.  

 

The output is just the first line which is "These fields are changed and need approval: " and then nothing else prints.  Also, debugging I find that I have this in a try/catch and I am seeing the "Error:  String validation Exception on name ID" which has me very confused as well. 

 

Here is the code 

 

public static void getChangedFieldValues(Map<Id, Account> newMap, Map<Id, Account> oldMap) {

        List<Schema.FieldSetMember> approvalFields = Util.getAddressApprovalFields();

        for (Account newAcc : newMap.values()) {
            Account oldAcc = oldMap.get(newAcc.Id);

            for (FieldSetMember fsm : approvalFields ) {
                String field = fsm.getFieldPath();
                try {
                    System.debug('newAcc.get(Field) ====== ' + newAcc.get(field));
                    System.debug('oldAcc.get(Field) ====== ' + oldAcc.get(field));
                    if (newAcc.get(field) != oldAcc.get(field)) {
                        newAcc.Start_Address_Approval_Description__c = 'These address fields are changed and need approval:\n';
                        newAcc.Start_Address_Approval_Description__c += fsm.getLabel() + ' changed from: ' + oldMap.get(fsm.getFieldPath()) + ' to: ' +
                                newAcc.get(fsm.getFieldPath()) + '\n';
                    }
                } catch (Exception e) {
                    System.debug('Address Error: ' + e);
                }
            }
}


Like i said, it is updating the Description field with the first line, "These address fields are changed and need approval" , but then nothing else is getting added to the field, and at the same time I am also seeing that weird error:

Error: System.StringException: Invalid id: Name

 

Any help on why the rest is not being printed or the error would help immensely.  

Best Answer chosen by Joseph Dekis
Dushyant SonwarDushyant Sonwar
Joseph,

The problem in the above code is you are accessing the field values directly , instead you need to first get account from the map and then it's field value.

Try below code,




                        newAcc.Start_Address_Approval_Description__c += fsm.getLabel() + ' changed from: ' + oldMap.get(newAcc.id).get(fsm.getFieldPath()) + ' to: ' +
                                newAcc.get(newAcc.id).get(fsm.getFieldPath()) + '\n';
 
public static void getChangedFieldValues(Map<Id, Account> newMap, Map<Id, Account> oldMap) {

        List<Schema.FieldSetMember> approvalFields = Util.getAddressApprovalFields();

        for (Account newAcc : newMap.values()) {
            Account oldAcc = oldMap.get(newAcc.Id);

            for (FieldSetMember fsm : approvalFields ) {
                String field = fsm.getFieldPath();
                try {
                    System.debug('newAcc.get(Field) ====== ' + newAcc.get(field));
                    System.debug('oldAcc.get(Field) ====== ' + oldAcc.get(field));
                    if (newAcc.get(field) != oldAcc.get(field)) {
                        newAcc.Start_Address_Approval_Description__c = 'These address fields are changed and need approval:\n';
                        newAcc.Start_Address_Approval_Description__c += fsm.getLabel() + ' changed from: ' + oldMap.get(newAcc.id).get(fsm.getFieldPath()) + ' to: ' +
                                newAcc.get(newAcc.id).get(fsm.getFieldPath()) + '\n';
                    }
                } catch (Exception e) {
                    System.debug('Address Error: ' + e);
                }
            }
}

Hope this helps!

All Answers

Dushyant SonwarDushyant Sonwar
Joseph,

The problem in the above code is you are accessing the field values directly , instead you need to first get account from the map and then it's field value.

Try below code,




                        newAcc.Start_Address_Approval_Description__c += fsm.getLabel() + ' changed from: ' + oldMap.get(newAcc.id).get(fsm.getFieldPath()) + ' to: ' +
                                newAcc.get(newAcc.id).get(fsm.getFieldPath()) + '\n';
 
public static void getChangedFieldValues(Map<Id, Account> newMap, Map<Id, Account> oldMap) {

        List<Schema.FieldSetMember> approvalFields = Util.getAddressApprovalFields();

        for (Account newAcc : newMap.values()) {
            Account oldAcc = oldMap.get(newAcc.Id);

            for (FieldSetMember fsm : approvalFields ) {
                String field = fsm.getFieldPath();
                try {
                    System.debug('newAcc.get(Field) ====== ' + newAcc.get(field));
                    System.debug('oldAcc.get(Field) ====== ' + oldAcc.get(field));
                    if (newAcc.get(field) != oldAcc.get(field)) {
                        newAcc.Start_Address_Approval_Description__c = 'These address fields are changed and need approval:\n';
                        newAcc.Start_Address_Approval_Description__c += fsm.getLabel() + ' changed from: ' + oldMap.get(newAcc.id).get(fsm.getFieldPath()) + ' to: ' +
                                newAcc.get(newAcc.id).get(fsm.getFieldPath()) + '\n';
                    }
                } catch (Exception e) {
                    System.debug('Address Error: ' + e);
                }
            }
}

Hope this helps!
This was selected as the best answer
Joseph DekisJoseph Dekis

Hey Dushyant oldMap.get(newAcc.id).get(fsm.getFieldPath()) was the answer!  

 

However if any others have an issue, newAcc.get(newAcc.id).get(fsm.getFieldPath()) returns nothing, you just need newAcc.get(fsm.getFieldPath()) works

 

Thankyou!

Dushyant SonwarDushyant Sonwar
Sorry, my bad! Actually i want to write newMap.get(newAcc.id).get(fsm.getFieldPath())  but did some typing error, newAcc.get(fsm.getFieldPath()) will also work as newAcc is Account Object 

and i think oldAcc.get(fsm.getFieldPath()) this will also work

Glad that your issue has been resolved :)