-3

I have a below code to check the filed name inside the set to filed name inside the map.

 Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Test__JobApp__c.fields.getMap();
    set<string> fieldToSync = new Set<string>{'Id','Name','Test__order__c','Test__Name__c','Test__status__c'};

    for( Id jobAppId : NewMap.keySet()) {
        Thiru_Test__JobApp__c newjobApp = NewMap.get(jobAppId);
        Thiru_Test__JobApp__c oldjobApp = OldMap.get(jobAppId);
        List<String> changedFieldSet = new List<String>();

        for (String fieldName : fieldMap.keyset()) {
            try {
                if(newjobApp.get(fieldName) != oldjobApp.get(fieldName)){
                    if(fieldToSync.contains(fieldName)) {
                       changedFieldSet.add(fieldName);
                    }
                }
            } catch (Exception e) {
                System.debug('Error: ' + e);
            }
        }
        fieldChangedMap.put(jobAppId, changedFieldSet);
    }

But it is not working. Below part is getting failed'

 if(fieldToSync.contains(fieldName)) {
     changedFieldSet.add(fieldName);
 }

But if I hard code the value for field name its working fine. Could anyone please tell me whats wrong in my code?

1

The field map returns keys in all lowercase, but you're using proper-casing. This is what's causing the problem. A better alternative would be to make sure the field is in the describe map. This eliminates the need for the try-catch block.

    for (String fieldName : fieldToSync) {
      if(fieldMap.containsKey(fieldName) && newjobApp.get(fieldName) != oldjobApp.get(fieldName)){
        changedFieldSet.add(fieldName);
      }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.