0

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.

0

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');
  • 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 '14 at 6:43
  • @Anu new sobj(); here sObj must be a valid object name like "Account" etc – Ashwani Sep 29 '14 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 '14 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 '14 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.