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

set the values to field dynamically


     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();
             string squery=' select ' + fieldtoken + ' from ' + objtoken + ' limit 1 ';
            sobject s=Database.query(squery);
            string sName1=string.valueof(fieldtoken);
            string sVar='hello';

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



please help me





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

Replace the insert with update.



thanks for ur reply

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

but it can't accept the DML operetion .



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