Created
August 28, 2012 01:22
-
-
Save mailtoharshit/3494087 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
public class MyDynamicSolution { | |
@future | |
public static void updateOwner(List<ID> objIds, ID newOwnerId) { | |
// Validate input | |
System.assert(objIds != null); | |
System.assert(objIds.size() > 0); | |
System.assert(newOwnerId != null); | |
// Get the sObject token from the first ID | |
// (the List contains IDs of sObjects of the same type). | |
Schema.SObjectType token = objIds[0].getSObjectType(); | |
// Using the token, do a describe | |
// and construct a query dynamically. | |
Schema.DescribeSObjectResult dr = token.getDescribe(); | |
String queryString = 'SELECT ownerId FROM ' + dr.getName() + | |
' WHERE '; | |
for(ID objId : objIds) { | |
queryString += 'Id=\'' + objId + '\' OR '; | |
} | |
// Remove the last ' OR' | |
queryString = queryString.subString(0, queryString.length() - 4); | |
sObject[] objDBList = Database.query(queryString); | |
System.assert(objDBList.size() > 0); | |
// Update the owner ID on the sObjects | |
for(Integer i=0;i<objDBList.size();i++) { | |
objDBList[i].put('ownerId', newOwnerId); | |
} | |
Database.SaveResult[] srList = Database.update(objDBList, false); | |
for(Database.SaveResult sr : srList) { | |
if (sr.isSuccess()) { | |
System.debug('Updated owner ID successfully for ' + | |
161 | |
Developer Console Enhancements Apex Code Enhancements | |
dr.getName() + ' ID ' + sr.getId()); | |
} | |
else { | |
System.debug('Updating ' + dr.getName() + ' returned the following errors.'); | |
for(Database.Error e : sr.getErrors()) { | |
System.debug(e.getMessage()); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment