Thursday, May 12, 2016

Create sObjects Dynamically in Apex

If you need to create record dynamically, then use below code:

For example, we are creating a contact record. 

String typeName="Contact";
Schema.SObjectType objToken = Schema.getGlobalDescribe().get(typeName);
sobject con= objToken.newSObject();
con.put("firstname","Sunil");  //first parameter is field API name and second is its value
con.put("LastName","Kumar");
con.put("Email","sunil02kumar@gmail.com");
insert con;

You can create any object record dynamically.

If you want to insert many records through list, you can store sObjects in list of sObjects and perform DML on list. 

No comments:

Post a Comment