function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anand-agrawal@hotmail.comanand-agrawal@hotmail.com 

How to instantiate custom/standard object using describe call.

Hi,

 

I have a requirement in which I need to instantiate an object (Custom or standard) by the objectName (A String).


for example : If I send the "Account" string as parameter, I want an object (Account object) of this type should be instantiate and get assigned to SObject variable.

 

 

Is there any way we can archive this ?

 

Thanks & Regards,

Anand Agrawal.

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

Good idea but I'd leverage our describe and dynamic semantics so you can get out of trying to keep this code consistent with your org metadata. Here's the sample with tests :

 

 

public class SObjectFactory {
    public static SObject buildObjectStatic(String objName) {
        if(objName == 'Account')          { return new Account();}
        else if(objName == 'Opportunity') { return new Opportunity();}
        else                              { return null; }
    } 
    
    public static SObject buildObjectDynamic(String name) {
        Schema.SObjectType t = Schema.getGlobalDescribe().get(name);
        System.assert(t != null, 'SObject name provided: "' + name + '" did not match anything in global describe.');
        return t.newSObject();
    }
       
    static testmethod void buildObjectStaticTest() {
        Sobject a = SObjectFactory.buildObjectStatic('Account');
        System.assert(a instanceOf Account, 'Whoops, buildobjectstatic method did not return an Account. Instead it returned: ' + a);
    }  
    
    static testmethod void buildObjectDynamicTest() {
        Sobject a = SObjectFactory.buildObjectDynamic('Account');
        System.assert(a instanceOf Account, 'Whoops, buildobjectdynamic method did not return an Account. Instead it returned: ' + a);
    } 
}

Check out the Dynamic Apex chapter in the Apex documentation for more info.

 

 

 

All Answers

Cory CowgillCory Cowgill

You can achieve this by using a Factory pattern.

 

public class SObjectFactory

{

       public static SObject buildObject(String objName)

       {

                if(objName == 'Account')

                {

                       return new Account();

                }

                if(objName == 'Opportunity')

                {

                       return new Opportunity();

                }

                if(objName == 'CustomObj__c')

                {

                       return new CustomObj__c;

                }

       }   

}

mtbclimbermtbclimber

Good idea but I'd leverage our describe and dynamic semantics so you can get out of trying to keep this code consistent with your org metadata. Here's the sample with tests :

 

 

public class SObjectFactory {
    public static SObject buildObjectStatic(String objName) {
        if(objName == 'Account')          { return new Account();}
        else if(objName == 'Opportunity') { return new Opportunity();}
        else                              { return null; }
    } 
    
    public static SObject buildObjectDynamic(String name) {
        Schema.SObjectType t = Schema.getGlobalDescribe().get(name);
        System.assert(t != null, 'SObject name provided: "' + name + '" did not match anything in global describe.');
        return t.newSObject();
    }
       
    static testmethod void buildObjectStaticTest() {
        Sobject a = SObjectFactory.buildObjectStatic('Account');
        System.assert(a instanceOf Account, 'Whoops, buildobjectstatic method did not return an Account. Instead it returned: ' + a);
    }  
    
    static testmethod void buildObjectDynamicTest() {
        Sobject a = SObjectFactory.buildObjectDynamic('Account');
        System.assert(a instanceOf Account, 'Whoops, buildobjectdynamic method did not return an Account. Instead it returned: ' + a);
    } 
}

Check out the Dynamic Apex chapter in the Apex documentation for more info.

 

 

 

This was selected as the best answer
Cory CowgillCory Cowgill

Yeah, the Schema Describe is even better than the static approach I gave. Forgot about that when I was typing it up. :) Good Catch.

 

Only caveat - You are limited to 100 Describe calls in a Context.

 

So make sure to Bulkify your code and use a Singleton pattern if necessary to catch the Describe calls in memory.

Anand_agrawal@persistent.co.inAnand_agrawal@persistent.co.in

Thank you very much Andrew. That is really helpful and what I was looking for. thanks again for your contribution. 

 

Thanks to Cory who tried figure this out for me. :) 

 

 

Gr8 help...!! :)

 

 

Thanks,

Anand Agrawal.