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
Gopal Das 13Gopal Das 13 

Instantiate custom object by name and insert record into it

How to create custom object dynamically?

I have one method in Apex controller which takes two arguement like below -

insertIntoCustomObjectDynamic(customObjectName, Map<String, String> fieldvalues){
//customObjectName can be any customobject name inside the org, its not static or predefined value
//if customObjectName is like MyObject__c then it will insert all the field values into MyObject__c customobject.

}
Best Answer chosen by Gopal Das 13
Asif Ali MAsif Ali M
Hi Gopal,

Try the below code which will dynamically inserts data into Objects based on customObjectName parameter. Let me know if you get any issues.
You need to extend this to support more DATA Types.
 
public void insertIntoCustomObjectDynamic(string customObjectName, Map<String, String> fieldvalues) {

        Sobject anySObject = Schema.getGlobalDescribe().get(customObjectName).getDescribe().getSObjectType().newSObject();

        // Keys are in lower case
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(customObjectName).getDescribe().fields.getMap();

        for (String key: fieldvalues.keySet()) {

            String fieldValue = fieldvalues.get(key);

            if (objectFields.containsKey(key.toLowerCase()) && fieldValue != null) {

                Schema.SObjectField field = objectFields.get(key);

                if (field.getDescribe().getType() == Schema.DisplayType.BOOLEAN) {
                    anySObject.put(field, Boolean.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.DOUBLE) {
                    anySObject.put(field, Double.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.STRING) {
                    anySObject.put(field, String.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.DATE) {
                    anySObject.put(field, Date.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.DATETIME) {
                    anySObject.put(field, Datetime.valueOf(fieldValue));
                }
            }
        }

        system.debug('SObject' + anySObject);

        Database.insert(anySObject);
    }



 

All Answers

RD@SFRD@SF
Hi Gopal,

This should be straight forward.
1) Write a nested if according to customObjectName, basically to get the insert into text.
2) And inside the nested if, iterate the map values and form the query.
The points one and two working together are forming a dynamic query.
And finally outside the nested if block call the Salesforce update query which updates the custom object.

I have to say this is pretty neat

Hope it helps
RD
Asif Ali MAsif Ali M
Hi Gopal,

Try the below code which will dynamically inserts data into Objects based on customObjectName parameter. Let me know if you get any issues.
You need to extend this to support more DATA Types.
 
public void insertIntoCustomObjectDynamic(string customObjectName, Map<String, String> fieldvalues) {

        Sobject anySObject = Schema.getGlobalDescribe().get(customObjectName).getDescribe().getSObjectType().newSObject();

        // Keys are in lower case
        Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(customObjectName).getDescribe().fields.getMap();

        for (String key: fieldvalues.keySet()) {

            String fieldValue = fieldvalues.get(key);

            if (objectFields.containsKey(key.toLowerCase()) && fieldValue != null) {

                Schema.SObjectField field = objectFields.get(key);

                if (field.getDescribe().getType() == Schema.DisplayType.BOOLEAN) {
                    anySObject.put(field, Boolean.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.DOUBLE) {
                    anySObject.put(field, Double.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.STRING) {
                    anySObject.put(field, String.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.DATE) {
                    anySObject.put(field, Date.valueOf(fieldValue));

                } else if (field.getDescribe().getType() == Schema.DisplayType.DATETIME) {
                    anySObject.put(field, Datetime.valueOf(fieldValue));
                }
            }
        }

        system.debug('SObject' + anySObject);

        Database.insert(anySObject);
    }



 
This was selected as the best answer