| 1. |
How To Create Instance Of Sobject Dynamically? Normally The Sobject Is Created Like “account A = New Account();”. But If You Are In Situation That You Don’t Know Which Sobject Is Going To Be Instantiated? Means It Will Be Decided At Runtime, How You Will Handle It? |
|
Answer» PUBLIC SObject getNewSobject(String t){ // CALL global describe to get the map of string to token. Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); // Get the token for the sobject based on the type. Schema.SObjectType ST = gd.get(t); // Instantiate the sobject from the token. Sobject s = st.newSobject(); RETURN s; } public SObject getNewSobject(String t){ // Call global describe to get the map of string to token. Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); // Get the token for the sobject based on the type. Schema.SObjectType st = gd.get(t); // Instantiate the sobject from the token. Sobject s = st.newSobject(); return s; } |
|