1

I have a long text area field which is on the page layout along with other fields. User is changing a picklist field but when trigger compares old and new values, long text are field is also detected as changed even when user has not made any changes to it. In the below code if condition is satisfied, when I check old and new records using get method and passing field API Name,

for(CustomObj__c req: trigger.new)
    {
        CustomObj__c oldreq = trigger.oldMap.get(req.Id);
    String longtextareafieldAPIName = 'MyCustomField__c';
                if(req.get(longtextareafieldAPIName) != oldreq.get(longtextareafieldAPIName) )
                {
                    system.debug('changed field - ' + longtextareafieldAPIName);
                    system.debug('old value - ' + oldreq.get(longtextareafieldAPIName));
                    system.debug('new value - ' + req.get(longtextareafieldAPIName));
                }

    }

when I compare the system debug output of old and new value, they are exactly the same. Any reason why .get method is returning different values ?

4
  • Right now, you're comparing Object values. It might be different if you use the actual String values.
    – Adrian Larson
    Feb 16, 2017 at 21:31
  • @AdrianLarson , above was just a sample, how do I detect all fields that have changed in trigger dynamically ? I have 200 fields on the object. Do I have to find the type of each field and then cast them and compare them accordingly ?
    – apn
    Feb 16, 2017 at 21:43
  • Most fields work just fine with a dynamic get call, it was just a guess at what might be going on here.
    – Adrian Larson
    Feb 16, 2017 at 21:44
  • 1
    Hmm unfortunately not all field types can be cast to String. How about using JSON.serialize? So now your comparison would be: if(JSON.serialize(req.get(fieldName)) != JSON.serialize(oldreq.get(fieldName)))
    – Charles T
    Feb 16, 2017 at 22:11

3 Answers 3

2

As mentioned in Apex Developer Guide, Use the field describe result's getMap method to return a map that represents the relationship between all the field names (keys) and the field tokens (values) for an sObject.

Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();

Get all the Field API Names using the keySet() method of the Map. Then Loop through the Fields and compare the Field value in Old Map and New Map.

I tried the following example on a sObject containing Long Text Area field, but could not re-produce your case. The change is detected only when there is a change in the Long Text area.

trigger BookTrigger on Book__c (after update) {
    // Get all the Book fields 
    Map<String, Schema.SObjectField> bookFieldsMap = Schema.SObjectType.Book__c.fields.getMap();
    //Get all the field API Names
    Set <String> bookAPIFieldNames = bookFieldsMap.keySet(); 
    // For each Book in the trigger
    for (sObject bookRecord : trigger.new){
        // For each field
        for(String field : bookAPIFieldNames){
            // Check whether the new value is different from old value for the same record
            if (bookRecord.get(field) != trigger.oldMap.get(bookRecord.id).get(field) ){
                System.debug(field + ' Value changed.');
            }
        }
    }
}
1

Looks like a new line issue.

You might have imported the data from a Mac CSV via DataLoader. In this case new lines are shown as \n.

Windows line breaks, just as line breaks in Salesforce, are \r\n.

You might check

yourObject.yourLTAfield__c.escapeUnicode()

for both trigger.oldmap and trigger.new records.

0

use

(req.longtextareafieldAPIName__c != oldreq.longtextareafieldAPIName__c)

instead of

(req.get(longtextareafieldAPIName) != oldreq.get(longtextareafieldAPIName)
1
  • using of get method is necessary as above was just a sample, I am trying to find out all the fields that have changed on the record. my custom object has 200 fields on it and this change detection should be done dynamically
    – apn
    Feb 16, 2017 at 21:58

You must log in to answer this question.

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