[SalesForce] How to add fields dynamically to sObject

I have a map which contain sObject anf sObjectfields. Now am looping through the map and want to add the fields to the list for eg: account.Name, conatct.Name, contact.type__c. And after that need display as inputfield. The field name and object name are saved in an another object as string I converted them into sObject and sObjectField and saved in map. So please help me how can I add fields to the sObject dynamically. And add them into a list.
Controller:

public with sharing class survey_ResultV2Ctrl{

public Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public Map<SObject, List<Schema.SObjectField>> sObjectFieldMap {get; set;}
public List<Schema.SObjectField> sObjectFieldLst = new List<Schema.SObjectField>();
public List<wrapperClass> wcls {get; set;}
public set<Schema.SObjectType> objNameSet =  new set<Schema.SObjectType>();
public Schema.SObjectType sobjtyp;
public List<sObject> sobjlst {get; set;}
public SObject newobj {get; set;}
//public SObject newobj = new SObject{};

public survey_ResultV2Ctrl(){
    sObjectFieldMap = new  Map<SObject, List<Schema.SObjectField>>();
    wcls =  new List<wrapperClass>();
    survey_ResultDisplay();
    sobjlst = new List<sObject>();
}

public void survey_ResultDisplay(){

    for(Filter_Field_Set__c ffs : [SELECT Name, Field_Set_Category__c, Object_Fields__c, Object_Fields_Name__c FROM Filter_Field_Set__c Where Field_Set_Category__c = 'a0F9000000R5Fau']){
        system.debug('All the result from Filter Field Set object ' + ffs.Name);
        system.debug('Schema Map contains Key as object name ' + schemaMap.containsKey(ffs.Name));
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(ffs.Name).getDescribe().fields.getMap();

        if(schemaMap.containsKey(ffs.Name)){
            system.debug('Schema object Values ' + schemaMap.get(ffs.Name));
            system.debug('All the fields related to Schema object ' + fieldMap.get(ffs.Object_Fields_Name__c));

            if(sObjectFieldMap.containskey(schemaMap.get(ffs.Name).newSObject())){
                sObjectFieldLst.add(fieldMap.get(ffs.Object_Fields_Name__c));
                //sobjtyp = schemaMap.get(ffs.Name);
                sObjectFieldMap.put(schemaMap.get(ffs.Name).newSObject(), sObjectFieldLst.clone());
                //objNameSet.add(schemaMap.get(ffs.Name));
                //wcls.add(new wrapperClass(schemaMap.get(ffs.Name), sObjectFieldLst));
                sObjectFieldLst.clear();
            }
            else{
                sObjectFieldLst.add(fieldMap.get(ffs.Object_Fields_Name__c));

                sObjectFieldMap.put(schemaMap.get(ffs.Name).newSObject(), sObjectFieldLst.clone());
                //wcls.add(new wrapperClass(schemaMap.get(ffs.Name), sObjectFieldLst));
                //objNameSet.add(schemaMap.get(ffs.Name));
            }
        }

    }

    for(sObject sobj : sObjectFieldMap.keyset()){
        //sobjlst.add(sObjectFieldMap.get(sobj));

        newobj = sobj;
        system.debug('object Name before loop ' + sobj);
        for(SObjectField sobjFd : sObjectFieldMap.get(sobj)){
            system.debug('object Name ' + sobj + ' Fields ' + sobjFd);
            newobj.clear();
            newobj.put(sobjFd, '');

            break;              
        }           

        system.debug('object details ' + sobj);
        break;    
    }   
    //system.debug('List before insert ' + sobjlst);     
    system.debug('The wapper class after loop ' + wcls);
    system.debug('The Map after loop ' + sObjectFieldMap);

}


public with sharing class wrapperClass{

    public list<Schema.SObjectField> fieldname {get; set;}
    public Schema.SObjectType objectName {get; set;}

    public wrapperClass(Schema.SObjectType objectName, list<Schema.SObjectField> fieldname){
         objectName = objectName;
         fieldname = fieldname;
    }
}   
}

If I am wrong please guide me the best way to achieve the result on this issue.

Best Answer

There are two ways to set fields values:

SObject acc = new Account();

// Schema.FieldType, Object
acc.put(Schema.Account.Description, 'Description of Account');

or

// Field API name, Object
acc.put('Description', 'Description of Account');
Related Topic