I have an sObject which I know it's api name (without namespace prefix).

How can I create a reference to it?

2 Answers 2


Check out the documentation for Dynamic DML and family of Describe() methods

String typeName = 'Account';

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
sObject obj = targetType.newSObject();

// now either cast it to Account and proceed
// Account a = (Account) obj;
// a.Name = 'Abc';

// or use sObject's generic get() and put() methods
obj.put('Name', 'Abc'); // use targetType.getDescribe().fields.getMap() to obtain map of all available field names
insert obj;

Global describe should return all objects visible to you (except if you don't have a license for managed package etc. standard visibility stuff), I have no problems retrieving these 2 installed in my org:

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
  • Will it also work for managed package? Actually I am already having namespace prefix.
    – doga
    Nov 1, 2012 at 7:44
  • It should! check out the updated answer. Just iterate over the map throwing stuff out to System.debug()? Essentially it's not different than what Eclipse IDE, Data Loader, Apex Explorer etc tools do... In fact I think when you write a managed package and you need to work on "not your" objects you're encouraged to use dynamic DML. Make no assumptions that even Accounts exist in the target org because somebody might want to install your app on Platform Edition which I think doesn't have all these SFA objects.
    – eyescream
    Nov 1, 2012 at 7:54
  • 4
    It will work for managed packages, but BE WARNED, if your code is in Namespace 1 (NS1), and has an object called NS1__ObjA__c, and in a customer's local org there is an object called ObjA__c, then NS1__ObjA__c will not show up in the result of getGlobalDescribe() from within NS1 --- it will be overwritten by ObjA__c. Even Schema.getGlobalDescribe().get('NS1__ObjA__c') will return a reference to ObjA__c in the local namespace! So beware.
    – zachelrath
    Nov 1, 2012 at 11:38
Id devRecordTypeId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('Billing').getRecordTypeId();
system.debug('**devRecordTypeId*' + devRecordTypeId);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .