I have a object A where I am saving the name of the object as Strings and the fields also as String. Eg: If I have object Account, contact and opp objects. I am saving object Account as object Name field in A obj as string and field Name to another field as string. same as with contact and opp. Here when I loop through the records from the A object I need to display the Account on to my VF page. The datatype of the field should be same so that it can display as it is. Is anyone over here can help me out for this issue.
Here is the way:
// I have Account name in String String objectStr = 'Account'; // Convert to schema.sObjectType Schema.SObjectType convertType = Schema.getGlobalDescribe().get(objectStr); // Create an instance of that type Sobject genericObject = convertType.newSObject(); // or if I know it is Account !! why making Sobject ? Account acc = (Account)convertType.newSObject();
An alternate way to do this without going through Schema.getGlobalDescribe():
SObjectType sObjType = ((SObject) Type.forName(myString).newInstance()) .getSObjectType());