I want to use a variable string rather than a hard-coded string in my get method.
For example, for every Field in Object A there will be a record in Object B and the record name is the field api name from Object A. I want to reference Object B's name in a Object A dynamic soql query:
String fieldName = objectB.Name
Sobject objectA = Database.query('Select Id From ObjectA__c Where Id = \'' + ObjectB.Object_A_Id__c + '\' Limit 1');
objectB.Number_Field__c = (integer)objectA.get(fieldName);
I don't know if this is possible. I get the error System.TypeException: Invalid conversion from runtime type Id to Integer. Still new to Apex coding.
Best Answer
As I understand your question:
For example
For the most part if the field types are the same you should be ok. BUT, if the field types are different then you are going to have to identify the appropriate type and cast it by
([TYPE])objectB.get('Value__c');
Maybe more clearOr maybe I did not follow what you are trying to do with the code