• Bruno Mendes 19
  • NEWBIE
  • 10 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
I'm having a problem with my code because I need to get a list of records of a dynamic type, and that is working alright, but then I need to use some of the fields and can't reach them, giving me the error 'Variable does not exist".
Here is a sample of the code:
String queryString = 'select Id, '+ camposMainObj.get(0) +', '+ camposMainObj.get(1)+', '+ camposMainObj.get(2) +', test__c from ' +sObjName+ ' where Id = \''+recordId+'\'';
 List<sObject> obj = database.query(queryString); system.debug('sObjName ::::::::: '+obj);
 string listType = 'List<'+sObjName+'>';
 List<sObject> castRecords = (List<sObject>)Type.forName(listType).newInstance();
 castRecords.addAll(obj);
 system.debug(castRecords);
 ChartObject__c co = new ChartObject__c();
 co.Pai__c = castRecords[0].Id;
co.PaiName__c = castRecords[0].Name;
 co.PaiEmail__c = castRecords[0].Email__c;
 co.PaiPhone__c = castRecords[0].Phone__c;


The variable 'sObjName' contains the correct object type (in this case Pai__c).
Only the Id is recognized. The result query looks like this:
(Pai__c:{Id=a070O00001uWD58QAG, Name=pai1, Phone__c=910987654, Email__c=pai1@mail.com, test__c=asdf}

So I can get every field from the query, but can only get the Id on the new object and it matches the queried one. The Name, Phone__c, Email__c, etc give me that error. I tried many solutions but can't seem to make it work.

Any help is appreciated.

Thank you in advance.