Play Games

Search This Blog

Wednesday, January 8, 2025

How to update/insert salesforce record with data present in Map which is dynamically generated in apex salesforce

 Problem: We have the Map which will store the field api name and its respectively value which is created at run time.

We need to insert/update respective record with this map data dynamically without hardcoding.

Solution:

Map<String,string> dataMap = new Map<string,string>{'Name'=>'Test'};

Schema.SobjectType targetType =Schema.getGlobalDescribe().get('Account');

Sobject obj = targetType.newSobject();

for(String fieldName : dataMap.keyset()) {

obj.put(fieldName,dataMap.get(fieldName));

}

insert obj;

System.debug('Account: '+obj);

Output:



No comments:

Post a Comment