I'm working with a trailhead org. I've created a custom field on Account(TrailApp_Test57__Country__c). I have this query and it has no errors but, when trying to get the TrailApp_Test57__Country__c value of the object it displays "Variable does not exist: TrailApp_Test57_Country__c" This is my code:

List<SObject> sObjLIST= [SELECT Id, TrailApp_Test57__Country__c FROM Account WHERE Id = :recordId];
String country = sObjLIST[0].TrailApp_Test57__Country__c;

And this is the created field:

enter image description here


Complete apex code:

public static wrapperResponse getDefaultFieldValuesByRecordId(String recordId, String sObjectOrigin){
    wrapperResponse response = new wrapperResponse();
    List<SObject> sObjLIST;
    String sObjectName = Id.valueOf(recordId).getSObjectType().getDescribe().getName();
    if(sObjectOrigin == 'Opportunity'){
        if(sObjectName == 'Account'){
            sObjLIST = [SELECT Id, TrailApp_Test57__Country__c FROM Account WHERE Id = :recordId];
            response.accountID = recordId;
            response.country = sObjLIST[0].TrailApp_Test57_Country__c;
    return response;

public class wrapperResponse{
    @AuraEnabled public String accountID;
    @AuraEnabled public String contactID;
    @AuraEnabled public String country;

And this is the error I get:

Variable does not exist: TrailApp_Test57_Country__c on line 38 which is the one that set response.country.

  • The error message leaves me to believe there is more going on like a code formatting error. Please post the entire error message including line number and the corresponding code with surrounding code (by copying not typing to reduce differences). It is not a run time error and not a permissions issue as it is written. – Eric Feb 24 '19 at 17:07
  • @Eric sorry for the delay. I just added the apex code and message retrieve. Thanks in advance for your help. – molinet Feb 25 '19 at 7:46

To get a field's value from sObject, you will have to use the get method, you cannot get the value of field (other than Id field) using dot notation. Hence, your code should be

String country =(string)sObjLIST[0].get('TrailApp_Test57__Country__c') ;

Or simply assign your SOQL result to List<Account> instead of List<sObject>, and then you can use dot notation to access the mentioned field.

String country = sObjLIST[0].TrailApp_Test57__Country__c;

More help on this link

  • Hi, I've written it wrong but even though with that it's still giving me the same error... – molinet Feb 24 '19 at 9:54
  • Hi, I have seen that you have updated the question, but you again added the underscore in the wrong place. It should be TrailApp_Test57__Country__c and not TrailApp__Test57_Country__c (Note that additional underscore should be after Test57) – Vijay Ganji Feb 24 '19 at 9:56
  • Yeah, sorry. But the point is that in my code both fields are written the same but I can't still manage to retrieve it because it says it doesn't exist. – molinet Feb 24 '19 at 10:12
  • Hi, updated the answer, this should work fine – Vijay Ganji Feb 24 '19 at 10:22
  • Hi. This is working now. I used String.valueOf(sObjLIST[0].get('TrailApp_Test57__Country__c')) in my code and it's working right. Thanks! – molinet Feb 25 '19 at 14:16

Check the Field Level Security (FLS) by clicking that middle button in your screenshot. On the FLS page, make sure the logged in user's profile has the Visible checkbox marked true, and that the Read Only checkbox is left unmarked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.