+ Start a Discussion
k practicek practice 

How to check comming fields is there or not in updating object?

Hi,
Here i was getting Contact object Custom fields.If suppose one custom field is not there in below comparing object(Person object).How to avoid this custom fields and same fields how to update?
  for (Schema.SObjectField objField: Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap().values()) {
                DescribeFieldResult fr = objField.getDescribe();
                if (fr.isUpdateable()) //isCreateable())
                {
                    if (fr.getName().endsWith('__c')) {
                        String str = fr.getName();
                        String contactcustomFields = str.replaceAll('__c', '__pc');
                        System.debug(contactcustomFields);
                        pAccount.put(contactcustomFields, Ocontact.get(str));
                        //update pAccount;
                        System.debug(pAccount.put(contactcustomFields, Ocontact.get(str)));
                    }
                
                }
            }

Help me.....
Hermes Yan 11Hermes Yan 11
Well if I'm understanding your requirement you want to only copy custom fields over if they exist?

You can try and leverage the isCustom() to determine if a field is custom.

The proper way to ensure if a field exists you could either do a describe on that second object also and check for it.

The lazy way would be to do a try catch around the line where you actually map the field. This would catch the failure of the invalid mapping and map the ones that exist.