You need to sign in to do that
Don't have an account?
mikef
dynamic apex question
I am trying to pull a field token back from a field schema map and use it in an SObject.
I do a describe call for all the fields in the SObject and then use a string value of the field name to get the field token back.
Apex compiler throws an error, Invalid field accField for SObject Account.
But the accField is a variable of type Schema.SObjectField and the map I use to set the variable is bring back the correct schema.
And the field does exist on the Account record.
Bottom line I trying to set a variable of type Schema.SObjectField with a valid account field. Can I do this in Apex?
I do a describe call for all the fields in the SObject and then use a string value of the field name to get the field token back.
Apex compiler throws an error, Invalid field accField for SObject Account.
But the accField is a variable of type Schema.SObjectField and the map I use to set the variable is bring back the correct schema.
And the field does exist on the Account record.
Bottom line I trying to set a variable of type Schema.SObjectField with a valid account field. Can I do this in Apex?
Assume I have already done the describe now I want to do this.
Can I use dynamic apex in a map call?
Here is the out come.
I was trying to bind a dynamic field to a map.get() method, and map.get() does not support this binding.
Here is the solution.
The code is not my production code and is only used as an example, and is not something I would put in production.
The key point in this example for me was getting the account back from the account map and then using the SObject.get() method to get the field from the DescribeFieldResult.
Now what if you want to populate an SObject or a later update.
Here I want to populate a new account SObject for later update. You have to use the SObject.put() method to add the Label and value of your dynamic field.
Hope this helps.
Message Edited by mikef on 10-31-2008 08:18 AM
btw-Thanks for this answer TM!
I unsuccessfully searched Help on this topic for 5 different applications and finally landed here with the solution for using dynamic apex to grab field describe results when the object and field name aren't known until runtime.