Last active
November 3, 2017 10:35
-
-
Save Szandor72/96f7f203d4f64e76c99e5216e1fdb550 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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