How to convert a string to sObjectType

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.

Answer

An alternate way to do this without going through Schema.getGlobalDescribe():

String myString = 'Account';
SObjectType sObjType = ((SObject) Type.forName(myString).newInstance())
                                  .getSObjectType();

speed comparison between above and getXXDescribe methods

Attribution
Source : Link , Question Author : Anu , Answer Author : TheLandoCal

Leave a Comment