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
sushmaiyer2@gmail.comsushmaiyer2@gmail.com 

How to dynamically create an instance for an Object in Constructor of a Controller?

Hi All,

 

How to dynamically create an instance for an Object in Constructor of a Controller when that Object is selected from the picklist in VF page.

souvik9086souvik9086

I think you are looking after this

 

public String filterID{get;set;}

public ConstructorName(){
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

/* Get the token for the sobject based on the type. */
Schema.SObjectType st = gd.get(filterID);

/* Instantiate the sobject from the token. */
Sobject s = st.newSobject();
}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

Puja_mfsiPuja_mfsi

Hi,

There is method to create an instance of Sobject :

 

public static sObject createObject(String typeName) {
    Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName);
     return targetType.newSObject();
}