Find your content:

Search form

You are here

Creating sobject using if i know object api name in string


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

How can I create a reference to it?

Attribution to: doga

Possible Suggestion/Solution #1

Id devRecordTypeId = Schema.getGlobalDescribe().get('Case').getDescribe().getRecordTypeInfosByName().get('Billing').getRecordTypeId();
system.debug('**devRecordTypeId*' + devRecordTypeId);

Attribution to: Debadyuti Sil

Possible Suggestion/Solution #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();

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content