Find your content:

Search form

You are here

Creating sobject using if i know object api name in string

 
Share

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();
System.debug(gd.get('c2g__codaInvoice__c'));
System.debug(gd.get('chttrunfollow__UnfollowRule__c'));

Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3959

My Block Status

My Block Content