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>();
    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();

            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));

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

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


    for(sObject sobj : sObjectFieldMap.keyset()){

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


        system.debug('object details ' + sobj);
    //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.

1 Answer 1


There are two ways to set fields values:

SObject acc = new Account();

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


// Field API name, Object
acc.put('Description', 'Description of Account');
  • But @regal my object is also getting dynamically. So when I try to use for(sObject sobj : sObjectFieldMap.keyset()){ SObject newobj = new sobj(); } I ma getting error that 'Compilation error: Invalid type: sobj'
    – Anu
    Sep 29, 2014 at 6:43
  • @Anu new sobj(); here sObj must be a valid object name like "Account" etc
    – Ashwani
    Sep 29, 2014 at 6:47
  • same I am doing in my code too. but issue is, there is 2 objects in loop account and contact. Account have field Account name and contact have field called type and name. For the first time the field get added to the object that's Account{AccountNumber=} but when it try to take the value from contact I am getting an error that "System.SObjectException: Account.Type__c does not belong to SObject type Contact" not able to get why this error is getting. When I try to add the Value to list of sObject it is getting nullvalue exception but there is value in it. Do you have any idea.
    – Anu
    Sep 29, 2014 at 7:06
  • 1
    @Anu You are trying to reference account field in instance of Contact object. From contact you should only reference AccountId
    – Ashwani
    Sep 29, 2014 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.