function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vlrvlr 

Schema.getGlobalDescribe and "Invalid field LastReferencedDate for Account"

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 ebug log:

 

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}

 

 

 

 Thanks

 

 

 

 

 

 

 

vlrvlr
It is not answered there.
crop1645crop1645

sorry, I typed too fast; I should have said - 'see also stackexchange for ideas on issue'

Sochettra SaingSochettra Saing
Hi vlr,
try to update apiVersion of your class or vf page. it will working fine.
Thanks