+ Start a Discussion
jayamjayam 

set the values to field dynamically

hi

     I get all all the objects in a selectlist. Based on selected object in the picklist all the field values are displyed in another selectlist . when we select a field in the select list  i want to set a value to that field .i set the value to that field by using put method in dynamic apex but i dont know how to save /update the value in object  when I use update /insert command exception thrown

 

 

Mycode is

 

 if(selobject!=null && getfieldnames!=null){
            SObjectType objToken = Schema.getGlobalDescribe().get(selobject);      
            DescribeSObjectResult objDef = objToken.getDescribe();                    
            Map<String, SObjectField> fieldmap = objDef.fields.getMap();
            
            SObjectField fieldToken = fieldmap.get(getfieldnames);
            DescribeFieldResult selectedField = fieldToken.getDescribe();
            sName=string.valueOf(selectedfield);
             string squery=' select ' + fieldtoken + ' from ' + objtoken + ' limit 1 ';
            sobject s=Database.query(squery);
            string sName1=string.valueof(fieldtoken);
            string sVar='hello';
            
            s.put(sName1,svar);

             object o=s.get(sName1);
            sName=string.valueof(o);
            //insert s;
          }//end of if

 

 

please help me

 

 

sebcossebcos

Hi,

you are trying to insert an existing record with the same id.

Replace the insert with update.

 

jayamjayam

thanks for ur reply

 i am trying with update and Database.upsert(s)

but it can't accept the DML operetion .

sebcossebcos

Hi,

please note that there are some sobjects which do not support DML, find a list here:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_dml_non_dml_objects.htm.