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.

  • Is ObjectB__c a child object of ObjectA__c?
    – Adrian Larson
    Commented Mar 21, 2017 at 21:37
  • Yes for every ObjectA__c record, there will be as many ObjectB__c records as there are ObjectA__c custom fields, ObjectB__c Record Name will hold ObjectA__c field api name
    – odlin
    Commented Mar 21, 2017 at 21:39

2 Answers 2


As I understand your question:

  1. ObjectB records will have the NAME field set to the API name of a field in ObjectA
  2. You want to populate ObjectA field specified by ObjectB.Name with a value

For example

sObject objectB = [Select Name, Value__c From ObjectB Where Id = 'XYZ'];

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 clear

String fieldName = 'AccountId';
sObject opp = database.query('Select Id ' + fieldName + ' From opportunity Where Id = XYZ');
Contact objectB = New Contact(AccountId = (Id)opp.get(fieldName));

Or maybe I did not follow what you are trying to do with the code

  • This got me on the right track. My issue was I was creating ObjectB__c Records for EVERY field, including ones I didn't need/fit with the datatype I was looking for. When I filtered for the exact datatype I was looking for my logic worked as intended. Thank you! note to self: just copy paste the code your writing instead of writing theory code
    – odlin
    Commented Mar 21, 2017 at 22:12

I think you need to add fieldname to your query. You're basically trying to access a field you're not querying:

Database.query('Select Id, ' + fieldName + ' From ObjectA__c Where Id = \'' + ObjectB.Object_A_Id__c + '\' Limit 1');

That is my initial thought just looking at your code.

With respects to your type error, if you are entering a fieldName that is not of type number, you're going to run into issues. You wont even be able to type cast this. You need to make sure fieldname is the same type as the variable you're trying to assign it to. In most cases, this will be string.

objectB.String_Field__c = string.valueOf(objectA.get(fieldName));

This should allow you to enter any field into fieldName and populate the field based on the value recieved.

If you are dead set on using a number field, try something like:

IF( string.isnumeric(objectA.get(fieldName)) ) {
    objectB.Number_Field__c = (integer) objectA.get(fieldName);
} else {
    // Do something with your error
  • getting the error System.QueryException: duplicate field selected: Id
    – odlin
    Commented Mar 21, 2017 at 21:37
  • Than remove Id from the query string.
    – gNerb
    Commented Mar 21, 2017 at 21:56
  • I updated my answer to address your type issue. If you're getting the duplicate field selected that means that "fieldame" is populated with a field that is not of type number.
    – gNerb
    Commented Mar 21, 2017 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .