You need to sign in to do that
Don't have an account?
lnryan
Dynamic Apex - setting field values help
Hi,
I get how to use dynamic apex to find out about elements and fields in an sObject and how to leverage that information to prepare SOQL statements, but i'm having trouble figuring out how to dynamically access or assign a field value on a record using dynamic apex. Can someone please help? Say I want to create a method that receives an incoming sObject and changes the Name to be a concatenated string based on a supplied list of fields in the object itself or its parents. How would I do this?
When I try
sobj.Name = ...
I get the error 'Field expression not allowed for generic SObject'
I have a number of places where i'd like to be able to trigger essentially the same pattern of behavior on multiple objects types...
Thanks in advance.
I get how to use dynamic apex to find out about elements and fields in an sObject and how to leverage that information to prepare SOQL statements, but i'm having trouble figuring out how to dynamically access or assign a field value on a record using dynamic apex. Can someone please help? Say I want to create a method that receives an incoming sObject and changes the Name to be a concatenated string based on a supplied list of fields in the object itself or its parents. How would I do this?
When I try
sobj.Name = ...
I get the error 'Field expression not allowed for generic SObject'
I have a number of places where i'd like to be able to trigger essentially the same pattern of behavior on multiple objects types...
Thanks in advance.
All Answers
sb.put(field, value);
or
sb.get(field);
rec.getSObjectType().get('...');
rec.getSObjectType().getDescribe().get('...');
rec.getSObjectType().getDescribe().fields.get('...');
So what i'm asking, is what do I need to do differently to get it to work...and what would I need to do additionally to retrieve those field values if they were on a related object instead of the object itself?
Thanks for any assistance
SObjectType objToken = Schema.getGlobalDescribe().get('Account');
//grab the describe
DescribeSObjectResult objDef = objToken.getDescribe();
//get the fields
Map<String, SObjectField> fields = objDef.fields.getMap();
//get the field token
SObjectField fieldToken = fields.get('Name');
//get the field describe - if necessary - there is a limit per request on field describes
DescribeFieldResult selectedField = fieldToken.getDescribe();
should work? Thanks for the clarification...
sobject.put(field,value);
So in your case:
sobject.put('name', concat_string);
Actually Every time i create fields and object using metadata api and i displayed all fields in an visualforce page Dynamiacally without using any fieldsets(Here object through URL will come).Here object related record is saved using schema methods,but this object related record fields(Here fields will come dynamically and add to visualforce page) information not saved in database.
Note:Here All fields in an visualforce page as editable fields.I give fields related information and click save only records only created related object, fields related information is not saved
How to save Dynamically comming fields(in an visualforce page) in Related Object Record Dynamically?
help me..........