function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anu-SFDCAnu-SFDC 

Assign Dynamic fields values

Hi

 

I want to give the  default values for mandatory fields.. For example, I have sample__c field in account object in my instance, wehre another user has another field. I want to get those fields dynamically.

 

Plz look at my code:

 

public DefaultCont() { 

objectNames = initObjNames(); 

  fields = new List<Pair>();

  }

 

 

public void showFields() {//fields.clear();

Map <String, Schema.SObjectField> fieldMap = Schema.sObjectType.Account.fields.getMap();

for(Schema.SObjectField sfield : fieldMap.Values()){

   schema.describefieldresult dfield = sfield.getDescribe();

Pair field = new Pair(); 

  field.key = dfield.isNillable();

   field.name = dfield.getname();   // field.val= dfield.getValue();

    if(field.key == false && field.name !='Name' ){ 

     fields.add(field);    

     }   

}

}

public class Pair { 

  public Boolean key {get; set;} 

  public String val {get; set;}   

public String name {get; set;}   

public String label{get;set;}

}

public SObject Saveaccount(){  

rec= new Account();

System.debug('fieldsssss'+fields[0].val); 

   System.debug('fieldsssssname'+fields[0].name); 

   rec.CnP__Sample__c = fields[0].val;

        rec.Name = 'sample'; 

    rec.fields[0].name = fields[0].val;  

  insert rec;   

 return rec;

}
public PageReference Saveacc(){    Saveaccount();        return null;    }
}

 

See the bold lines.. Above that I can get two system.debug statements in that 

fieldsssssname is Sample__c..

 

But when I want to save this controller it shows the error like

 

Invalid field fields for SObject Account at line 63 column 6

 

 

Plz help me.

 

Anuradha

bob_buzzardbob_buzzard

You have to use the get/set notation (aka dynamic DML) to specify fields in this way.

 

Rather than :

 

rec.fields[0].name = fields[0].val; 

 

You'd use:

 

rec.put(fields[0].name fields[0].val);

 

 

Anu-SFDCAnu-SFDC

Hi

 

Thanks for the reply...

 

But for the above code, I have to use that in triggers while inserting accounts..

 

So I have written the following code.

 

DefaultCont sc= new DefaultCont(); 

 DefaultCont.Pair field = new DefaultCont.Pair(); 

List<DefaultCont.Pair> fields = new List<DefaultCont.Pair>(); 

  sc.initObjNames();    sc.ShowFields(); 

  sc.Saveaccount(); 

  sc.Saveacc();   

System.debug('showfieldssssssssssss'+sc.fields[0].name);        System.debug('showfieldsslabelllllllll'+sc.fields[0].val); 

        for(Integer i=0; i<=sc.fields.size();i++){   

  rec.put(fields[0].name, 'testsam'); 

  }

 

 

But I am not getting the fields[0].val from the controller(page input value)..

 

How do I?

 

Thanks

Anu

bob_buzzardbob_buzzard

Can you post the page markup that ties the field val property to the input on the page?

Anu-SFDCAnu-SFDC

   <apex:pageblocksection id="fieldList" rendered="{!not(isnull(selectedObject))}">    

                    <apex:pageblockTable value="{!fields}" var="fls"> 

      <apex:column value="{!fls.name}" headerValue="Label"/> 

      <apex:column value="{!fls.key}" headerValue="Key"/>

      <apex:column headerValue="Value"> 

     <apex:inputText value="{!fls.val}" />   

      </apex:column> 

  </apex:pageblockTable>

bob_buzzardbob_buzzard

Your markup looks okay to me. In the code you have written above, is that a test method? I can't see where the values would be set.

Anu-SFDCAnu-SFDC

No

ts not a test method.. Its controller only.

 

public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();

        public List <Pair> fields {get; set;} 

      public List <SelectOption> objectNames {public get; private set;}   

    public String selectedObject {get; set;}

public  List<SelectOption> initObjNames() { 

  List<SelectOption> objNames =new List<SelectOption>(); 

  List<String> entities = new List<String>(schemaMap.keySet());

    entities.sort(); 

  for(String name : entities) 

      objNames.add(new SelectOption(name,name));   

    return objNames;

  }

 

 

This part is also included in code.. Hope this might helpful to understand.

 

bob_buzzardbob_buzzard

Understood, however the code you posted above:

 

DefaultCont sc= new DefaultCont(); 
 DefaultCont.Pair field = new DefaultCont.Pair(); 
List<DefaultCont.Pair> fields = new List<DefaultCont.Pair>(); 
  sc.initObjNames();    sc.ShowFields(); 
  sc.Saveaccount(); 
  sc.Saveacc();   
System.debug('showfieldssssssssssss'+sc.fields[0].name);        System.debug('showfieldsslabelllllllll'+sc.fields[0].val); 
        for(Integer i=0; i<=sc.fields.size();i++){   
  rec.put(fields[0].name, 'testsam'); 
  }

 Doesn't involve a page, it simple creates some data and saves it.