0

I need to update all contact records from one record to another record which has 333 fields. It takes time to update this. I thought of updating the sobject field dynamically, but I'm getting "Variable does not exist: fieldvalue" while saving the class.

Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();
Contact existingContact;
List<Schema.SObjectField> fieldsList = new List<Schema.SObjectField>();
Schema.SObjectType s = m.get('Contact');
Schema.DescribeSObjectResult r = s.getDescribe();
Map<String,Schema.SObjectField> fields = r.fields.getMap();
for(Contact con : ConList)
{
    for(Schema.SObjectField fieldItems : fields.values()) {
        Schema.DescribeFieldResult dfield = fieldItems.getDescribe();
        if (dfield.isCreateable() && !dfield.isCalculated()) {
            
            if(con.Id != existingContact.Id)
            {
                
                existingContact.fieldItems = String.isNotBlank(con.fieldItems ) ? con.fieldItems : existingContact.fieldItems ;
            }
        }
    }
}

How can it resolved?

1 Answer 1

1

Replace this line:

existingContact.fieldvalue= String.isNotBlank(con.fieldvalue) ? con.fieldvalue : existingContact.fieldvalue;

with:

String f = dfield.getName();
if (String.isNotBlank(con.get(f))) {
    existingContact.put(f, con.get(f));
}

or better:

SObjectField f = fieldItems;
if (String.isNotBlank(con.get(f))) {
    existingContact.put(f, con.get(f));
}

because the SObjectField can be used directly as Apex has map-like syntax for dynamic access of SObject fields. See the SObject Class documentation.

(JavaScript does offer the syntax you used.)

I also removed the assignment of the existingContact value to itself as that seems pointless.

Note that doing this dynamically will likely be slower at runtime than hard coding the field values. But it does have the advantage of getting the set of fields right in the first place and automatically adapting when fields are added. If the fields are unlikely to change in the future, and performance is really important, you could run similar code to this in the Developer Console and output the field API names and hard code those in this logic.

9
  • 1
    do you mean existingContact.put(dfield.getName(),con.get(dfield.getName()) ) ? Dec 6, 2022 at 9:42
  • still getting error "Method does not exist or incorrect signature: void getName() from the type Schema.SObjectType","Method does not exist or incorrect signature: void isNotBlank(Object) from the type String" ,"Expression cannot be assigned", Dec 6, 2022 at 9:42
  • You are right @PranayJaiswal - will change.
    – Keith C
    Dec 6, 2022 at 9:44
  • @KeithC i was just testing the above code on ChatGpt and it found the bug.. Dec 6, 2022 at 9:46
  • 1
    @KeithC very very good.. Its taking world by storm.. almost half of the SE question could be answered by AI. Dec 6, 2022 at 9:49

You must log in to answer this question.

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