9

I am using this code to print out Account fields to PDF file . Programm fails when it tries to retrieve "LastReferencedDate" from the instance of account object. It fails with "System.SObjectException: Invalid field LastReferencedDate". When I run simillar code in Execute Anonymous there is no LastReferencedDate field on the Map. I do not understand why when I debug apex class it tries to retrieve "LastReferencedDate" field and fails, but it does not have LastReferencedDate field when I run it in Execute Anonymous. And why Schema.getGlobalDescribe() returns field which Account does not have. Can anyone explain me what is going on here? Thanks.

My Apex class:

  public with sharing class AccountPDFGenerator
     {

         public static final String FORM_HTML_START = '<HTML><BODY>';
         public static final String FORM_HTML_END = '</BODY></HTML>';

         public static void generateAccountPDF(Account account)
         {
            System.debug('Account Name=' + account.Name);
             String pdfContent = '' + FORM_HTML_START;
             try
             {
                 pdfContent = '' + FORM_HTML_START;
                 pdfContent = pdfContent + '<H2>Account Information in PDF</H2>';

                 //Dynamically grab all the fields to store in the PDF
                 Map<String, Schema.SObjectType> sobjectSchemaMap = Schema.getGlobalDescribe();
                 Schema.DescribeSObjectResult objDescribe = sobjectSchemaMap.get('Account').getDescribe();
                 Map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap();

                 //Append each Field to the PDF
                 for(Schema.SObjectField fieldDef : fieldMap.values()) 
                 {
                    System.debug('fieldDescResult  =' + fieldDef.getDescribe());
                     Schema.Describefieldresult fieldDescResult = fieldDef.getDescribe();

                     System.debug('field name =' + fieldDescResult.getName());
                     String name = fieldDescResult.getName();

                     System.debug('account.get(name) =' + account.get(name));
                     pdfContent = pdfContent + '<P>' + name + ': ' + account.get(name.trim()) + '</P>';
                 }
                 pdfContent = pdfContent + FORM_HTML_END;
             }catch(Exception e)
             {
                System.debug('Error = ' + e.getMessage() );
                System.debug('Account fields = ' + account );

                 pdfContent = '' + FORM_HTML_START;
                 pdfContent = pdfContent + '<P>THERE WAS AN ERROR GENERATING PDF: ' + e.getMessage() + '</P>';
                 pdfContent = pdfContent + FORM_HTML_END;
             }
             attachPDF(account,pdfContent);
         }

         public static void attachPDF(Account account, String pdfContent)
         {
             try
             {
                 Attachment attachmentPDF = new Attachment();
                 attachmentPDF.parentId = account.Id;
                 attachmentPDF.Name = account.Name + '.pdf';
                 attachmentPDF.body = Blob.toPDF(pdfContent); //This creates the PDF content
                 insert attachmentPDF;
             }catch(Exception e)
             {
                 account.addError(e.getMessage());
             }
         }

     }

Snippet of debug log file:

11:07:54.111 (1111596000)|USER_DEBUG|[24]|DEBUG|fieldDescResult  =Schema.DescribeFieldResult[getByteLength=0;getCalculatedFormula=null;getController=null;getDefaultValue=null;getDefaultValueFormula=null;getDigits=0;getInlineHelpText=null;getLabel=Last Referenced Date;getLength=0;getLocalName=LastReferencedDate;getName=LastReferencedDate;getPrecision=0;getRelationshipName=null;getRelationshipOrder=null;getScale=0;getSoapType=DATETIME;getSobjectField=LastReferencedDate;getType=DATETIME;isAccessible=true;isAutoNumber=false;isCalculated=false;isCascadeDelete=false;isCaseSensitive=false;isCreateable=false;isCustom=false;isDefaultedOnCreate=false;isDependentPicklist=false;isDeprecatedAndHidden=false;isDisplayLocationInDecimal=false;isExternalId=false;isFilterable=true;isGroupable=false;isHtmlFormatted=false;isIdLookup=false;isNameField=false;isNamePointing=false;isNillable=true;isPermissionable=false;isRestrictedDelete=false;isSortable=true;isUnique=false;isUpdateable=false;isWriteRequiresMasterRead=false;]
11:07:54.111 (1111606000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
11:07:54.111 (1111614000)|SYSTEM_METHOD_ENTRY|[25]|Schema.SObjectField.getDescribe()
11:07:54.111 (1111631000)|SYSTEM_METHOD_EXIT|[25]|Schema.SObjectField.getDescribe()
11:07:54.111 (1111644000)|SYSTEM_METHOD_ENTRY|[27]|System.debug(ANY)
11:07:54.111 (1111649000)|USER_DEBUG|[27]|DEBUG|field name =LastReferencedDate
11:07:54.111 (1111654000)|SYSTEM_METHOD_EXIT|[27]|System.debug(ANY)
11:07:54.111 (1111665000)|SYSTEM_METHOD_ENTRY|[30]|SObject.get(String)
11:07:54.111 (1111742000)|EXCEPTION_THROWN|[30]|System.SObjectException: Invalid field LastReferencedDate for Account
11:07:54.111 (1111852000)|SYSTEM_METHOD_EXIT|[30]|SObject.get(String)
11:07:54.111 (1111894000)|SYSTEM_METHOD_ENTRY|[36]|System.SObjectException.getMessage()
11:07:54.111 (1111928000)|SYSTEM_METHOD_EXIT|[36]|System.SObjectException.getMessage()
11:07:54.111 (1111939000)|SYSTEM_METHOD_ENTRY|[36]|System.debug(ANY)
11:07:54.111 (1111945000)|USER_DEBUG|[36]|DEBUG|Error = Invalid field LastReferencedDate for Account
11:07:54.111 (1111950000)|SYSTEM_METHOD_EXIT|[36]|System.debug(ANY)
11:07:54.111 (1111966000)|SYSTEM_METHOD_ENTRY|[37]|String.valueOf(Object)
11:07:54.114 (1114349000)|SYSTEM_METHOD_EXIT|[37]|String.valueOf(Object)
11:07:54.114 (1114376000)|SYSTEM_METHOD_ENTRY|[37]|System.debug(ANY)
11:07:54.114 (1114383000)|USER_DEBUG|[37]|DEBUG|Account fields = Account:{Sub_conc_Academic_Advisor__c=null, Areas_of_Skill_or_Interest__pc=null, TickerSymbol=null, Concentration_Field_Coordinator__c=null, Foundation_Student_Level__c=null, Expected_Graduation_Term__c=null, Personal_Email__c=null, GPA__c=null, Original_City__c=null, Calswec_PCW__c=false, BillingStreet=null, Original_Zip__c=null, PersonAssistantName=null, Awards_Workstudy__c=false, Degree_Title__c=null, Prior_Supervisory_Experience__pc=null, ShippingLongitude=null, Rating=null, Previous__c=null, Program_of_Interest__c=null, Vehical_Required_Lead_Form__c=false, AccountSource=null, Anticipated_Return_Cohort__c=null, Access_to_Vehicle_During_Placement__c=false, Conc_Field_Liaison__c=null, SystemModstamp=2013-09-18 18:07:53, Vehicle_Required_During_Placement__c=null, Cohort_Accepted__c=null, PersonMailingLatitude=null, PersonLastCURequestDate=null, of_FT_Field_Instructors__c=null, Number_of_Health_Medical__c=0, Age__pc=null, Country_of_Citizenship__c=null, Number_of_Mental_Health_MVS__c=0, Foundation_Field_Liaison__c=null, Alternate_Placement_Days_Available__c=null, Imported_Ethnicity__c=null, Percent_Caucasian_Served__c=0, Middle_Name_Alias__c=null, Number_of_Work_and_Life_MVS__c=0, Foundation_Academic_Advisor__c=null, Other_Populations_Served__c=null, PersonDoNotCall=false, Agency_Requirements__c=null, RecordTypeId=012300000015e8sAAA, Percent_Asian_Served__c=0, Special_Schedule__c=false, Is_Active_Employee__pc=false, Number_of_Foundation_Students_Updated__c=null, Residency__c=null, Registered_Units__c=null, General_Focus_of_Services__c=null, Mental_Health_Rank__c=null, University_Title__pc=null, ADA_Compliance_Explanation__c=null, Active__pc=false, Accept_Foundation_Students__c=false, Concentration_Interest__c=null, AccountNumber=null, Consideration_Foundation_Populations_Exc__c=null, Awards_Stipends__c=false, Disability_Type__c=null, USC_Email__c=kross@usc.edu, PersonMailingStreet=null, Field_Status__c=null, Last_Enrollment_Term__c=null, of_PT_Field_Instructors__c=null, Number_of_Health_Medical_Updated__c=null, Phone=null, PersonMobilePhone=null, Sub_Concentration_Coordinator__c=null, BillingLatitude=null, PersonOtherLongitude=null, Prior_Experience_with_Interns__c=false, Primary_Demographic_Group_Served__c=null, LastActivityDate=2013-07-08 00:00:00, PersonMailingCountry=null, Auto_Insurance__c=false, University_Name__pc=null, PersonMailingState=null, Applied_for_Financial_Aid__c=false, Concentrations_Accepted__c=null, Length_of_Service__pc=null, Micro_Practice_Modalities__c=null, Home_Address__pc=null, Ownership=null, ParentId=null, Field_Instructor_Training_Date__pc=null, Languages__pc=null, Number_of_Mental_Health_SCH_PPS_Updated__c=null, Accept_Concentration_Students__c=false, LastModifiedDate=2013-09-18 18:07:53, Sub_conc_Field_Liaison__c=null, SIS_Local_Address_Last_Updated__c=null, Surrogate_Foundation_Liaison__c=null, Number_of_Family_and_Children_SCH_PPS__c=0, Intern_EBP_Training__c=false, PersonHasOptedOutOfFax=false, Primary_Populations_Served__c=null, Cohort_Current__c=null, First_Enrolled_Term__c=null, PersonTitle=null, Resume__pc=null, Resume_Received__pc=false, Agency_Auspice__c=null, Number_of_Mental_Health__c=0, Language__c=null, BillingCity=null, IsDeleted=false, LCSW__pc=false, Concentration_Field_Liaison__c=null, Number_of_Family_and_Children_Updated__c=null, Number_of_Mental_Health_SCH_PPS__c=0, Primary_Email__c=null, Modified_Schedule__c=false, AnnualRevenue=null, Is_LCSW__pc=false, Number_of_Mental_Health_MVS_Updated__c=null, BillingPostalCode=null, PersonEmailBouncedDate=null, GPA_Units__c=null, FirstName=Alba, Placements_Provided__c=null, PersonLeadSource=null, Intern_Workstation__c=null, PersonMailingPostalCode=null, ShippingStreet=null, Unit__pc=null, Ethnicity__c=Native American, MOU_Current__c=false, Disability__c=false, Is_SOWK_Graduate__pc=false, MasterRecordId=null, Placement_Consideration__c=null, Units_Attempted__c=null, PersonHomePhone=null, Percent_Native_American_Served__c=0, Number_of_Concentration_Students__c=0, Agency_Description__c=null, Primary_Contact_Type__c=null, Last_LOA_Semester__c=null, Last_Name_Alias__c=null, International_Phone_2__c=null, PersonLastCUUpdateDate=null, SicDesc=null, Macro_Practice_Modalities__c=null, Preferred_Greeting__pc=null, ADA_Compliant__c=false, Awards_Worstudy_Data_From_Lead__c=null, Conc_Academic_Advisor__c=null, LCSW_Year__pc=null, SIS_First_Enrolled_Term__c=null, Limitations_Personal_Qualities__pc=null, Personal_Email__pc=null, Secondary_Email__c=null, PersonMailingCity=null, Last_Semester_GPA__c=null, BillingLongitude=null, Tertiary_Populations_Served__c=null, BillingCountry=null, Reason_for_Enrollment_Exception__c=null, Number_Family_Children_SCH_PPS_Updated__c=null, PersonMailingLongitude=null, On_SAP_List__c=true, Id=001e000000AHXqMAAX, Date_of_Interview__pc=null, PersonEmailBouncedReason=null, Type=null, Site_Change__c=null, Number_of_Mental_Health_Updated__c=null, Accept_Concentration_Students_Updated__c=null, Faculty_Id__pc=null, Receive_Holiday_Card__pc=false, Placement_Consideration_Exception__c=null, Special_Student__c=false, Why_Current_Interest__c=null, Fax=null, Lead_Academic_Center_Interest__c=null, Level__pc=null, Intern_Activity_Description__c=null, Cleaned__c=false, Can_Provide_Services_in_Non_English_Lang__c=false, IUC__c=false, Evidence_Based_Practice__c=false, COPA_Rank__c=null, Consideration_Foundation_Populations__c=null, Sub_Pop_Code__c=null, Placement_Status__c=null, Has_Multiple_Locations__c=false, Number_of_Work_and_Life_MVS_Updated__c=null, Areas_of_Skill_or_Experience__pc=null, MSW_School__pc=null, Objective__c=null, Secondary_Populations_Served__c=null, Valid_Driver_s_Licence__c=false, Anticipated_Return_Semester_Year__c=null, Southern_California_County__c=null, Field_Personal_Statement__c=null, CreatedById=005a0000007rRD2AAM, Calswec_MH__c=false, Degree_Name__c=null, PersonAssistantPhone=null, OwnerId=005a0000007rRD2AAM, Number_of_Foundation_Students__c=null, BillingState=null, Reviewed_USC_Field_Requirements__pc=false, Number_of_COPA_MVS_Updated__c=null, of_Contract_Field_Instructors__c=null, First_Name_Alias__c=null, Language_2_Fluency__c=null, Waiver_Exam__c=false, CreatedDate=2013-06-28 21:01:45, JigsawCompanyId=null, Sic=null, Work_and_Life_Rank__c=null, Major__c=null, Field_Instructor_Training_Location__pc=null, Number_of_MSW_Employees__c=null, PersonHasOptedOutOfEmail=false, Placement_Clearance_Issue_Details__c=null, MSW_Degree_Year__pc=null, Notes_from_Agency_Lead__c=null, Concentration_Academic_Advisor__c=null, Site=null, Hours_of_Operation__c=null, Concentrations_Accepted_Last_Updated__c=null, Academic_Review_Completed__c=false, Sub_conc_Field_Coordinator__c=null, PR_Number__pc=null, Other_Language_1__c=null, Transfer_Student__c=false, Number_of_Work_and_Life_Updated__c=null, Conc_Field_Coordinator__c=null, IsPersonAccount=true, Industry=null, SalesforceCustomer=null, PersonContactId=003e00000070iW3AAI, Other_Language_2__c=null, Language_1_Fluency__c=null, Number_of_COPA__c=0, Date_Clean_Completed__c=null, Awards_Workstudy_Agency__c=false, Prior_Teaching_Experience__pc=null, Secondary_Reports_To__pc=null, Sub_Concentration__c=null, Description=null, Online_2Tor_Agency__c=false, Admissions_Term__c=null, LastModifiedById=005a0000007rRD2AAM, Faculty_USC_ID__pc=null, Location__pc=null, International_Student__c=false, Program_Change__c=null, Home_Visits_Required__c=null, Health_Rank__c=null, Latitude__c=null, Seminar_Instructor__c=null, PersonEmail=null, Number_of_Work_and_Life__c=0, Geographic_Area__c=null, LastName=Caldera, PersonDepartment=null, Gender__c=Female, NumberOfEmployees=null, Primary_Language__c=null, Tertiary_Demographic_Group_Served__c=null, ShippingCity=null, Degree__pc=null, Accept_Foundation_Students_Updated__c=null, Jigsaw=null, ShippingCountry=null, Units_Earned__c=null, Salutation=null, Number_of_Family_and_Children__c=0, USC_ID__c=67895789, AssistantEmail__pc=null, PersonOtherStreet=null, Longitude__c=null, PersonOtherPostalCode=null, Home_Visits_Required_From_Lead__c=false, Enrollment_Status__c=Full Time, Provides_Reimbursement_For__c=null, Academic_Condition__c=false, Post_Code__c=null, SIS_Permanent_Address_Last_Updated__c=null, Number_of_Health_Medical_MVS__c=0, PersonOtherPhone=null, Mezzo_Practice_Modalities__c=null, Grade_Points__c=null, Stipends_Available_To__c=null, Bilingual_Ability__c=null, Citizenship_Status__c=null, Memorandum_of_Agreement_Expiration_Date__c=null, Degree_Conferred_Date__c=null, of_Foundation_Placements__c=null, Completed_Field_Instructor_Training__pc=false, International_Phone_1__c=null, Percent_Latino_Served__c=0, Position__pc=null, County__c=null, Percent_African_American_Served__c=0, of_Concentration_Placements__c=null, Number_of_Family_and_Children_MVS__c=0, Faculty_Type__pc=null, Enrollment_Exception__c=null, Awarded_Work_Study__c=false, Number_of_Health_Medical_MVS_Updated__c=null, Prior_Teaching_or_Supervisory_Experience__pc=null, Paper_Opt_Out__c=false, PersonOtherState=null, ShippingLatitude=null, Status_of_Enrollment_Exception_Request__c=null, Preferred_Last_Name__c=null, Status_of_Site_Change__c=null, Why_Interested__pc=null, Site_Change_Effective_Semester__c=null, Program_Change_Effective_Semester__c=null, Limitations__pc=null, Additional_Comments__c=null, Status_Notes__pc=null, Number_of_COPA_Updated__c=null, Number_of_Family_Children_MVS_Updated__c=null, Campus__c=a0Be0000000X44mEAC, Placement_Consideration_Details__c=null, Fluency__c=null, ShippingPostalCode=null, PersonOtherCity=null, Status__c=null, Number_of_COPA_MVS__c=0, Concentration__c=null, ShippingState=null, PersonOtherCountry=null, Academic_Probation__c=false, Preferred_First_Name__c=null, Enrollment_Exception_Effective_Semester__c=null, Hire_Date__pc=null, Secondary_Demographic_Group_Served__c=null, Has_Field_Instructor__c=false, Status_of_Program_Change__c=null, Placement_Clearance_Issue__c=false, Families_and_Children_Rank__c=null, Stipend_Amount__c=null, Website=null, PersonBirthdate=null, PersonOtherLatitude=null}

It looks like I am using last API version: enter image description here

Here is the code I ran in Execute Anonymous: String query = ''; Map sobjectSchemaMap = Schema.getGlobalDescribe(); Schema.DescribeSObjectResult objDescribe = sobjectSchemaMap.get('Account').getDescribe(); Map fieldMap = objDescribe.fields.getMap();

//Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();
for(Schema.SObjectField fieldDef : fieldMap.values()) 
{

    System.debug('fieldDescResult  =' + fieldDef.getDescribe());
    Schema.Describefieldresult fieldDescResult = fieldDef.getDescribe();

     System.debug('field name =' + fieldDescResult.getName());
      String name = fieldDescResult.getName();

    query = name + ', \n' + query;
}

System.debug('---------------->> a ' + query);

Here is it's partial output of execute anonymous:

12:50:59.260 (260035000)|USER_DEBUG|[21]|DEBUG|---------------->> a      Percent_African_American_Served__c, 
Awards_Worstudy_Data_From_Lead__c, 
PersonMailingPostalCode, 
International_Student__c, 
Preferred_Greeting__pc, 
IUC__c, 
SIS_First_Enrolled_Term__c, 
Academic_Review_Completed__c, 
PersonEmailBouncedReason, 
Southern_California_County__c, 
USC_ID__c, 
BillingStreet, 
Intern_EBP_Training__c, 
PersonHomePhone, 
Awards_Stipends__c, 
PersonAssistantPhone, 
Number_of_COPA_MVS__c, 
MasterRecordId, 
Accept_Foundation_Students__c, 
Website, 
Units_Attempted__c, 
Hours_of_Operation__c, 
Primary_Contact_Type__c, 
General_Focus_of_Services__c, 
Anticipated_Return_Cohort__c, 
Concentrations_Accepted_Last_Updated__c, 
Accept_Concentration_Students_Updated__c, 
Middle_Name_Alias__c, 
Auto_Insurance__c, 
Enrollment_Status__c, 
Secondary_Populations_Served__c, 
Hire_Date__pc, 
Stipends_Available_To__c, 
Cohort_Current__c, 
Longitude__c, 
Work_and_Life_Rank__c, 
Id, 
Number_of_Health_Medical_MVS_Updated__c, 
Position__pc, 
Faculty_Type__pc, 
BillingCountry, 
Health_Rank__c, 
Bilingual_Ability__c, 
Academic_Probation__c, 
PersonEmail, 
Placement_Consideration__c, 
Active__pc, 
PersonMailingState, 
Seminar_Instructor__c, 
Ethnicity__c, 
Foundation_Student_Level__c, 
Percent_Asian_Served__c, 
Consideration_Foundation_Populations_Exc__c, 
Transfer_Student__c, 
MSW_Degree_Year__pc, 
Notes_from_Agency_Lead__c, 
Number_of_Work_and_Life__c, 
Primary_Email__c, 
PersonContactId, 
Number_of_Mental_Health__c, 
BillingPostalCode, 
ShippingState, 
PersonMailingStreet, 
of_Contract_Field_Instructors__c, 
ShippingCity, 
AssistantEmail__pc, 
Number_of_Mental_Health_MVS_Updated__c, 
Age__pc, 
Accept_Concentration_Students__c, 
Placement_Status__c, 
Cohort_Accepted__c, 
Units_Earned__c, 
Evidence_Based_Practice__c, 
Concentration_Interest__c, 
Fluency__c, 
Enrollment_Exception_Effective_Semester__c, 
ADA_Compliant__c, 
Program_of_Interest__c, 
Degree_Title__c, 
Number_of_MSW_Employees__c, 
Can_Provide_Services_in_Non_English_Lang__c, 
Site_Change__c, 
Placement_Consideration_Exception__c, 
Applied_for_Financial_Aid__c, 
Field_Instructor_Training_Date__pc, 
Memorandum_of_Agreement_Expiration_Date__c, 
PersonOtherPhone, 
Is_SOWK_Graduate__pc, 
PersonMobilePhone, 
Type, 
LCSW_Year__pc, 
Major__c, 
Phone, 
Residency__c, 
Disability_Type__c, 
Expected_Graduation_Term__c, 
Primary_Demographic_Group_Served__c, 
International_Phone_2__c, 
PersonOtherCountry, 
Salutation, 
Intern_Workstation__c, 
Number_of_Family_and_Children_SCH_PPS__c, 
International_Phone_1__c, 
Waiver_Exam__c, 
Last_Semester_GPA__c, 
On_SAP_List__c, 
Secondary_Demographic_Group_Served__c, 
Alternate_Placement_Days_Available__c, 
GPA_Units__c, 
Micro_Practice_Modalities__c, 
Number_of_Foundation_Students_Updated__c, 
Foundation_Academic_Advisor__c, 
Preferred_Last_Name__c, 
PersonOtherCity, 
Placement_Clearance_Issue__c, 
Placement_Consideration_Details__c, 
Awards_Workstudy__c, 
PersonMailingCountry, 
Additional_Comments__c, 
Reason_for_Enrollment_Exception__c, 
Number_of_Concentration_Students__c, 
Degree__pc, 
Conc_Academic_Advisor__c, 
Number_of_COPA_Updated__c, 
Concentration_Academic_Advisor__c, 
Foundation_Field_Liaison__c, 
ShippingStreet, 
Degree_Conferred_Date__c, 
Personal_Email__c, 
PersonOtherState, 
PersonOtherPostalCode, 
Preferred_First_Name__c, 
Completed_Field_Instructor_Training__pc, 
Language__c, 
Percent_Caucasian_Served__c, 
Tertiary_Populations_Served__c, 
Disability__c, 
Prior_Supervisory_Experience__pc, 
ShippingPostalCode, 
Personal_Email__pc, 
PersonDepartment, 
OwnerId, 
Areas_of_Skill_or_Experience__pc, 
CreatedDate, 
Limitations__pc, 
Latitude__c, 
Number_of_Mental_Health_Updated__c, 
Number_of_Work_and_Life_MVS__c, 
Primary_Language__c, 
Number_Family_Children_SCH_PPS_Updated__c, 
Industry, 
Date_Clean_Completed__c, 
PersonLeadSource, 
Number_of_Health_Medical_MVS__c, 
Status_Notes__pc, 
Home_Visits_Required__c, 
Consideration_Foundation_Populations__c, 
BillingCity, 
Number_of_Family_and_Children_Updated__c, 
MOU_Current__c, 
CreatedById, 
ParentId, 
Resume__pc, 
Jigsaw, 
Faculty_Id__pc, 
Paper_Opt_Out__c, 
Language_2_Fluency__c, 
Number_of_Health_Medical__c, 
Concentration__c, 
PersonAssistantName, 
Macro_Practice_Modalities__c, 
Concentration_Field_Liaison__c, 
Sub_Concentration_Coordinator__c, 
USC_Email__c, 
Post_Code__c, 
Agency_Description__c, 
Country_of_Citizenship__c, 
Status_of_Enrollment_Exception_Request__c, 
Other_Language_1__c, 
First_Enrolled_Term__c, 
Prior_Teaching_or_Supervisory_Experience__pc, 
Agency_Requirements__c, 
Placements_Provided__c, 
Mezzo_Practice_Modalities__c, 
Academic_Condition__c, 
University_Name__pc, 
Prior_Experience_with_Interns__c, 
Concentration_Field_Coordinator__c, 
Grade_Points__c, 
Number_of_Work_and_Life_MVS_Updated__c, 
Is_LCSW__pc, 
Access_to_Vehicle_During_Placement__c, 
Name, 
Why_Interested__pc, 
Valid_Driver_s_Licence__c, 
Field_Personal_Statement__c, 
Field_Status__c, 
ADA_Compliance_Explanation__c, 
Percent_Latino_Served__c, 
PersonBirthdate, 
IsPersonAccount, 
LastModifiedDate, 
COPA_Rank__c, 
Status_of_Program_Change__c, 
of_Foundation_Placements__c, 
Secondary_Reports_To__pc, 
PR_Number__pc, 
Length_of_Service__pc, 
Conc_Field_Liaison__c, 
AccountSource, 
Has_Multiple_Locations__c, 
MSW_School__pc, 
Vehical_Required_Lead_Form__c, 
Location__pc, 
of_FT_Field_Instructors__c, 
Limitations_Personal_Qualities__pc, 
SicDesc, 
Sub_Concentration__c, 
Original_City__c, 
Degree_Name__c, 
Placement_Clearance_Issue_Details__c, 
Is_Active_Employee__pc, 
Status_of_Site_Change__c, 
Enrollment_Exception__c, 
Campus__c, 
Gender__c, 
JigsawCompanyId, 
Agency_Auspice__c, 
of_Concentration_Placements__c, 
Fax, 
PersonEmailBouncedDate, 
Registered_Units__c, 
Areas_of_Skill_or_Interest__pc, 
Anticipated_Return_Semester_Year__c, 
Tertiary_Demographic_Group_Served__c, 
Unit__pc, 
Modified_Schedule__c, 
Sub_conc_Field_Liaison__c, 
Description, 
Sub_conc_Academic_Advisor__c, 
of_PT_Field_Instructors__c, 
Reviewed_USC_Field_Requirements__pc, 
LastActivityDate, 
Stipend_Amount__c, 
PersonOtherStreet, 
Status__c, 
Last_Enrollment_Term__c, 
Sub_conc_Field_Coordinator__c, 
Program_Change__c, 
Number_of_Work_and_Life_Updated__c, 
Online_2Tor_Agency__c, 
Date_of_Interview__pc, 
Home_Address__pc, 
PersonLastCURequestDate, 
Prior_Teaching_Experience__pc, 
Awarded_Work_Study__c, 
Calswec_MH__c, 
FirstName, 
ShippingCountry, 
Last_Name_Alias__c, 
PersonLastCUUpdateDate, 
Other_Populations_Served__c, 
Number_of_Family_and_Children__c, 
Number_of_COPA_MVS_Updated__c, 
SIS_Local_Address_Last_Updated__c, 
Number_of_Health_Medical_Updated__c, 
Number_of_Family_Children_MVS_Updated__c, 
County__c, 
Receive_Holiday_Card__pc, 
Last_LOA_Semester__c, 
Accept_Foundation_Students_Updated__c, 
Number_of_Mental_Health_SCH_PPS__c, 
Imported_Ethnicity__c, 
University_Title__pc, 
Special_Student__c, 
Primary_Populations_Served__c, 
Number_of_Family_and_Children_MVS__c, 
Calswec_PCW__c, 
LastName, 
BillingState, 
Why_Current_Interest__c, 
Field_Instructor_Training_Location__pc, 
Admissions_Term__c, 
Cleaned__c, 
Intern_Activity_Description__c, 
Site_Change_Effective_Semester__c, 
Mental_Health_Rank__c, 
Concentrations_Accepted__c, 
Number_of_Foundation_Students__c, 
LastModifiedById, 
Number_of_Mental_Health_SCH_PPS_Updated__c, 
First_Name_Alias__c, 
Resume_Received__pc, 
Program_Change_Effective_Semester__c, 
NumberOfEmployees, 
Vehicle_Required_During_Placement__c, 
PersonMailingCity, 
Lead_Academic_Center_Interest__c, 
Surrogate_Foundation_Liaison__c, 
Citizenship_Status__c, 
Other_Language_2__c, 
AnnualRevenue, 
Faculty_USC_ID__pc, 
Secondary_Email__c, 
Original_Zip__c, 
PersonTitle, 
Level__pc, 
GPA__c, 
Families_and_Children_Rank__c, 
Awards_Workstudy_Agency__c, 
Previous__c, 
SIS_Permanent_Address_Last_Updated__c, 
Number_of_Mental_Health_MVS__c, 
SystemModstamp, 
Language_1_Fluency__c, 
Percent_Native_American_Served__c, 
RecordTypeId, 
IsDeleted, 
Home_Visits_Required_From_Lead__c, 
Has_Field_Instructor__c, 
LCSW__pc, 
Sub_Pop_Code__c, 
Geographic_Area__c, 
Conc_Field_Coordinator__c, 
Languages__pc, 
Number_of_COPA__c, 
Objective__c, 
Provides_Reimbursement_For__c, 
Special_Schedule__c, 

12:50:59.360 (260153000)|CUMULATIVE_LIMIT_USAGE
12:50:59.360|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Number of code statements: 1650 out of 200000
  Maximum CPU time: 0 out of 15000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 1 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10

12:50:59.360|CUMULATIVE_LIMIT_USAGE_END

12:50:59.260 (260184000)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:50:59.260 (260194000)|EXECUTION_FINISHED

After testing through Developer Console: enter image description here

I tried another test environment we have. The strange thing is that IDE is aonly allow me to create classes of version 27 for both environments:

enter image description here

  • What about the API version of the code that is querying the Account? Where does this come from? – Andrew Fawcett Sep 18 '13 at 19:42
  • 1
    I was able to use the dev console in a Summer '13 org to execute the query select id, LastReferencedDate from Account limit 1. Your code does show LastReferencedDate when I execute anonymous via dev console. – Mike Chale Sep 18 '13 at 19:48
  • @MikeChale: well, in my case, it does not. I just updated my post. Thanks. – vlr Sep 18 '13 at 19:58
  • @MikeChale snap, just proposed the same on my answer. vlr is your org quite old or quite new? Wondering if for some reason this feature has not been provisioned? So your sure it doesn't work in dev console either? – Andrew Fawcett Sep 18 '13 at 20:02
  • Can you see the RecentlyViewed object in Dev Console? Or run this List<RecentlyViewed> lists = [select Id from RecentlyViewed]; – Andrew Fawcett Sep 18 '13 at 20:05
10

Force.com Platform Apex Versioning and Feature/Field Visibility

The platform (or more specifically Apex runtime) often (but not always) conditions the visibility of various features (including fields on objects) based on the API version (linked with platform version) associated with Apex code file containing the code being run. This way new features arriving on the platform don't break existing code, but it also means unless you upgrade the API version of your code it won't see new features/fields. This is driven by the -meta.xml file associated with your Apex and VF pages, also visible in the UI under Version Settings tab.

enter image description here

enter image description here

RE: Difference in behaviour in Execute Anonymous

When you run Apex code fragments this way, you have no containing Apex class or -meta.xml file. In this case the version of the Apex Web Service API being used to execute the Apex code governors the version of the Apex runtime your code runs in. I suspect your running your code in the Force.com IDE? Which likely uses a version of the Apex Web Service that pre-dates the introduction of this field (new for Summer'13). So try upgrading your Force.com IDE to resolve this. Note that if you use Developer Console to perform the same via its Execute Anonymous window, the code will work, as this is implicitly always on the latest platform version.

Resolving your Issue

Thus the first thing to check is that your using the latest API version in your code and VF files, I like to ensure they are all on the same version. The latest version, as of Summer'13 is API 28. This LastReferenced field was introduced in API 28, so use either of the two approaches to check and change your API version of your code and VF files.

Slight Oddity about your Issue

In theory the Apex Describe should return fields only supported by the API version the code making the describe calls supports. Thus you should reasonably expect to pass them to a SOQL query as well. What might be a platform bug here is the Apex Describe is surfacing the LastReferenced field incorrectly for older API Apex code, while the SOQL parser is correctly enforcing the perception that at that API level it didn't exist. Regardless updating to a new API version should resolve it, though if Apex Describe is "leaking" new fields regardless of the Apex callers API version this could reoccur. I'll do a little testing on this see if I can prove this assertion about a possible platform bug.

  • wow!So detailed ! – Mohith Shrivastava Sep 18 '13 at 19:40
  • @Andrew, thank you for your prompt response. I edited my post. It looks like I am using last version of API. Thanks. – vlr Sep 18 '13 at 19:40
  • Yes so I see, hmmmmm, what about the code querying the Account, where/how does it get read? – Andrew Fawcett Sep 18 '13 at 19:43
  • 1
    @AndrewFawcett, I just ran the code suggested by you in Execute Anonymous, and here is the output: Compile error at line 1 column 23 No such column 'LastReferencedDate' on entity 'Account'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names. – vlr Sep 18 '13 at 20:17
  • 1
    I'm encountering similar error, "No such column 'lastreferenceddate' on entity 'Account'". My apex code created a query of all account columns dynamically using sObjectDescribe... tests pass in sandbox but get this error when try to deploy to production via changeset. Using API 29.0 – Doug Ayers Dec 10 '13 at 20:55

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.