Create Records in Any SObject Dynamically
October 4, 2024
Use Case : If you want to insert records in any SObject dynamically just by passing field API Name & their value in JSON and SObject Name as a parameter.
public class DynamicRecordController {
public static void createRecords(String objectName,String fieldValue){
try{
// JSON Example- {"Name":"SFDC Elements","Type":"Prospect"} for objectName-Account
Map<String,Object> fieldValueMap=(Map<String,Object>)JSON.deserializeUntyped(fieldValue);
sObject sObj = Schema.getGlobalDescribe().get(objectName).newSObject();
for(String key: fieldValueMap.keySet()){
sObj.put(key, String.valueOf(fieldValueMap.get(key))); //**putting all the field API name and their values
}
insert sObj ;
}
catch(Exception e){
System.debug('Inside DynamicRecordController Exception::'+e.getMessage());
}
}
}
** Note: can add filter as well based on different field type and parse accordingly and JSON should be changed accordingly.
Output:


4
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
1
4
4
4
Users Today : 1
Users Yesterday : 6
Users Last 7 days : 142
Users Last 30 days : 283
Users This Month : 156
Users This Year : 531
Total Users : 1444
Views Today : 1
Views Yesterday : 22
Views Last 7 days : 439
Views Last 30 days : 764
Views This Month : 473
Views This Year : 1142
Total views : 2729
Who's Online : 0
Your IP Address : 216.73.216.231
Server Time : June 14, 2026 6:06 pmPowered By WPS Visitor Counter