Skip to content

Instantly share code, notes, and snippets.

@Szandor72
Last active November 3, 2017 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szandor72/96f7f203d4f64e76c99e5216e1fdb550 to your computer and use it in GitHub Desktop.
Save Szandor72/96f7f203d4f64e76c99e5216e1fdb550 to your computer and use it in GitHub Desktop.
List<String> sObjectNames = new String[]{'Account','Contact','opportunity'};
List<sObject> queriedObjects = new List<Sobject>();
For (String sObjectName : sObjectNames) {
//Querying for objects in a loop is bad practice
sObject tempObject = Database.query('Select Id, Name From ' + sObjectName + ' Limit 1');
queriedObjects.add(tempObject);
}
For (sObject mysObject : queriedObjects) {
//fails for contact since contact has no updateable name attribute/field
try {
//dealing with generic sObject is different than dealing with e.g. Account. You can use Account.Name but not sobject.Name
mysObject.put('Name', 'New '+ mySobject.get('Name'));
system.debug(mySobject.get('Name'));
// => 'New Pyramid Constructions'
}
catch (exception e) {
//do nothing
}
}
update queriedObjects;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment