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
mikefmikef 

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?
tmatthiesentmatthiesen
Take a look at the below sample:

SObjectType objToken = Schema.getGlobalDescribe().get(selectedObjectName);
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fields = objDef.fields.getMap(); 
        
SObjectField fieldToken = fields.get(selectedFieldName);
DescribeFieldResult selectedField = fieldToken.getDescribe();

 

mikefmikef
Thanks but I can't get the describe to work in a map.

Assume I have already done the describe now I want to do this.
Code:
SObjectType objToken = Schema.getGlobalDescribe().get(Account);
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fields = objDef.fields.getMap(); 
        


accountMap = new Map<Id,Account>(); //lets say i have 200 records

for(Opportunity o : trigger.new){
   SObjectField fieldToken = fields.get(o.Custom_Field__c);
   DescribeFieldResult selectedField = fieldToken.getDescribe();
   if(accountMap.containsKey(o.AccountId)){
      o.Custom_Field__c = accountMap.get(AccountId).selectedField.getName();
   }

}
The error is invalid field for Account

Can I use dynamic apex in a map call?
 

mikefmikef
Thanks Taggart for your help.

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.

Code:
SObjectType objToken = Schema.getGlobalDescribe().get(Account);
DescribeSObjectResult objDef = objToken.getDescribe();
Map<String, SObjectField> fields = objDef.fields.getMap(); 
        


accountMap = new Map<Id,Account>(); //lets say i have 200 records

Account getAccount;

for(Opportunity o : trigger.new){
   SObjectField fieldToken = fields.get(o.Custom_Field__c);
   DescribeFieldResult selectedField = fieldToken.getDescribe();

   if(accountMap.containsKey(o.AccountId)){
      getAccount = accountMap.get(AccountId);
//after I get the full account back I can get the dynamic field value
 o.Custom_Field__c = (castToDataType) getAccount.get(selectedField.getName()); } }

 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.

Code:
tempAccount = new Account(id=o.AccountId);
tempAccount.put(selectedField.getLabel(),quantityToAdd + existingQuantity);

 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
jdk4702jdk4702

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.