You need to sign in to do that
Don't have an account?

Dynamic Insert possible? Fieldname in a variable
I need to do something pretty simple: adding contacts to the existing salesforce contacts like so:
Contact[] allContacts = [select firstName, lastName, Email, Birthdate, description from contact];
Contact newContact = new Contact(firstName = 'AAA', lastName='BBB', email='A@A.com' etc..);
allContacts.add(newContact);
upsert allContacts;
Problem is that the fields "firstName, lastName, Email, Birthdate, description" are not fixed. They will change depending on who uses the application.
Is there a way to use string variables instead of fieldnames to add a new contact?
Something along the lines of:
String field1 = 'first_name';
newContact.Fields[field1] = 'AAA';
Yes, here is the documentation on dynamic dml in apex.
The syntax is similar to:
sObj.put('Name','test Account name');
All Answers
Yes, here is the documentation on dynamic dml in apex.
The syntax is similar to:
sObj.put('Name','test Account name');
oooh.. Thanks!