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
PeacePeace 

Dynamically create object for a value selected from Picklist

Hi,

 

I am new to salesforce. i have a simple query. Please help.

 

i have a listbox containing 10 values.  The value i select is stored in 'selectedValue' variabe.

 

now i have to create an object for this selected value in apex.

 

How can i dynamically create objects ?

 

Best Answer chosen by Admin (Salesforce Developers) 
Bhawani SharmaBhawani Sharma

You need to something like this:

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName); //typeName is your picklist's selected value 
SObject sObj = targetType.newSObject();
sObj.put('name', 'Test');
datbase.insert(sObj);

 

 

 

 

All Answers

Bhawani SharmaBhawani Sharma
Trying best to understand your question:
You have a picklist field:
Values in this picklist are name of object.
You want to create a new record of that object on selection of picklist values.

Is this correct?
PeacePeace

Yes Bhawani,

 

I have a picklist field
Values in this picklist are name of object.
I want to create an instance of that object on selection of picklist values.

 

 

Bhawani SharmaBhawani Sharma

You need to something like this:

Schema.SObjectType targetType = Schema.getGlobalDescribe().get(typeName); //typeName is your picklist's selected value 
SObject sObj = targetType.newSObject();
sObj.put('name', 'Test');
datbase.insert(sObj);

 

 

 

 

This was selected as the best answer