3

I'm getting this error when I execute a query and call a method inside the anonimous window. It's strange because I've executed a code very similar in length before and I've never seen this error. I've got an object with more than 200 fields that I try to query (required to be like this because of the business logic).

enter image description here

This is the code I'm executing:

```PrivateFundRegistration__c fundRegistration = [SELECT
                                                    Id,
                                                    Name,
                                                    CurrencyIsoCode,
                                                    Account__r.Name,
                                                    ALMCOCVResumeTitle__c,
                                                    ALMCOCVResume__c,
                                                    ALMCOCity__c,
                                                    ALMCOCountryOfBirth__c,
                                                    ALMCOCountry__c,
                                                    ALMCODateOfBirth__c,
                                                    ALMCOEmailAddress__c,
                                                    ALMCOEmployer__c,
                                                    ALMCOFacsimileNumber__c,
                                                    ALMCOFirstName__c,
                                                    ALMCOGender__c,
                                                    ALMCOLastName__c,
                                                    ALMCOMiddleName__c,
                                                    ALMCOOccupationTitle__c,
                                                    ALMCOOtherNames__c,
                                                    ALMCOPOBox__c,
                                                    ALMCOPhoneNumber__c,
                                                    ALMCOPrefix__c,
                                                    ALMCOStateProvince__c,
                                                    ALMCOStreetAddress__c,
                                                    ALMCOZipPostalCode__c,
                                                    AMLCFTServiceProviderCountry__c,
                                                    AMLCFTServiceProviderEmail__c,
                                                    AMLCFTServiceProviderName__c,
                                                    AccountingPrinciples__c,
                                                    ActualOrExpectedSizeOfInvestorBase__c,
                                                    AdministratorsConsentLetterTitle__c,
                                                    AttachAuditorsConsentLetter__c,
                                                    AttachCertOfIncorporationTitles__c,
                                                    AttachCertificateOfIncorporation_Re__c,
                                                    AttachConsFromRegAndTransfTitle__c,
                                                    AttachConsentFromNAVCalcAgenTitle__c,
                                                    AttachOfferingDocSummaryTitles__c,
                                                    AttachStructureChartTitles__c,
                                                    AttachStructureChart__c,
                                                    AttachTrustDeedDeclarationTitles__c,
                                                    AuditingPrinciples__c,
                                                    AuditorsConsentLetter__c,
                                                    BaseCurrencyOfOffering__c,
                                                    BuildingFloorSuiteB03a__c,
                                                    BuildingFloorSuiteB05a__c,
                                                    CIMAApplicationNumber__c,
                                                    CIMAIDC06__c,
                                                    CIMAID__c,
                                                    CaymanAuditorLocalAuditSignOff__c,
                                                    CityB03d__c,
                                                    CityB05d__c,
                                                    CompaniesRegistrationCORISNumber__c,
                                                    CountryB03g__c,
                                                    CountryB05g__c,
                                                    CountryOfAdministrator__c,
                                                    CountryOfDepositary__c,
                                                    CountryOfFormation__c,
                                                    CountryOfInvestmentManager__c,
                                                    CountryOfNAVCalculationAgent__c,
                                                    CountryOfOverseasAuditor__c,
                                                    CountryOfPromoterSponsor__c,
                                                    CountryOfRegistrarAndTransferAgent__c,
                                                    DAMLCOCIMAID__c,
                                                    DAMLCOCVResumeTitle__c,
                                                    DAMLCOCVResume__c,
                                                    DAMLCOCity__c,
                                                    DAMLCOCountryOfBirth__c,
                                                    DAMLCOCountry__c,
                                                    DAMLCODateOfBirth__c,
                                                    DAMLCOEmailAddress__c,
                                                    DAMLCOEmployer__c,
                                                    DAMLCOFacsimileNumber__c,
                                                    DAMLCOFirstName__c,
                                                    DAMLCOGender__c,
                                                    DAMLCOLastName__c,
                                                    DAMLCOMiddleName__c,
                                                    DAMLCOOccupationTitle__c,
                                                    DAMLCOOtherNamesAliases__c,
                                                    DAMLCOPOBox__c,
                                                    DAMLCOPhoneNumber__c,
                                                    DAMLCOPrefix__c,
                                                    DAMLCOStateProvince__c,
                                                    DAMLCOStreetAddress__c,
                                                    DAMLCOZipPostalCode__c,
                                                    DMLROCIMAID__c,
                                                    DMLROCVResumeTitle__c,
                                                    DMLROCVResume__c,
                                                    DMLROCity__c,
                                                    DMLROCountryOfBirth__c,
                                                    DMLROCountry__c,
                                                    DMLRODateOfBirth__c,
                                                    DMLROEmailAddress__c,
                                                    DMLROEmployer__c,
                                                    DMLROFacsimileNumber__c,
                                                    DMLROFirstName__c,
                                                    DMLROGender__c,
                                                    DMLROLastName__c,
                                                    DMLROMiddleName__c,
                                                    DMLROOccupationTitle__c,
                                                    DMLROOtherNamesAliases__c,
                                                    DMLROPOBox__c,
                                                    DMLROPhoneNumber__c,
                                                    DMLROPrefix__c,
                                                    DMLROStateProvince__c,
                                                    DMLROStreetAddress__c,
                                                    DMLROZipPostalCode__c,
                                                    DateOfFormationIncorporation__c,
                                                    EmailAddressB03i__c,
                                                    EmailAddressB05i__c,
                                                    EmailAddress__c,
                                                    Engagement__c,
                                                    FYEDay__c,
                                                    FYEMonth__c,
                                                    FirstName__c,
                                                    FrequencyOfValuations__c,
                                                    GPCountry__c,
                                                    GPLegalEntityIdentifierLEI__c,
                                                    GeneralPartnerGP__c,
                                                    IfInCaymanNotRegisteredProvideIM__c,
                                                    IfInCaymanSelectAdministratorName__c,
                                                    IfInCaymanSelectInvestmentManager__c,
                                                    IfNoProvideFreePayments__c,
                                                    IfNoProvideNameOfNAVCalcAgent__c,
                                                    IfNoProvideNameOfRegistrarAndTra__c,
                                                    IfNoProvideOngoingQueries__c,
                                                    IfNotInCaymanProvideAdministrator__c,
                                                    IfNotInCaymanProvideIM__c,
                                                    IfNotInCaymanSelectNameOfRegulat__c,
                                                    IfRegulatorIsNotOnTheListInB19__c,
                                                    IfRegulatorIsNotOnTheListInB24__c,
                                                    IfRegulatorisNotOnTheListInB09__c,
                                                    IndustryReference__c,
                                                    InitialTermDurationOfFundInYears__c,
                                                    InvestmentRestrictionsCategory__c,
                                                    InvestmentRestrictionsDetails__c,
                                                    LastName__c,
                                                    LegalCounselInCayman__c,
                                                    LegalEntityIdentifierLEI__c,
                                                    LegalStructure__c,
                                                    MLROCIMAID__c,
                                                    MLROCVResumeTitle__c,
                                                    MLROCVResume__c,
                                                    MLROCity__c,
                                                    MLROCountryOfBirth__c,
                                                    MLROCountry__c,
                                                    MLRODateOfBirth__c,
                                                    MLROEmailAddress__c,
                                                    MLROEmployer__c,
                                                    MLROFacsimileNumber__c,
                                                    MLROFirstName__c,
                                                    MLROGender__c,
                                                    MLROLastName__c,
                                                    MLROMiddleName__c,
                                                    MLROOccupationTitle__c,
                                                    MLROOtherNamesAliases__c,
                                                    MLROPOBox__c,
                                                    MLROPhoneNumber__c,
                                                    MLROPrefix__c,
                                                    MLROStateProvince__c,
                                                    MLROStreetAddress__c,
                                                    MLROZipPostalCode__c,
                                                    MaximumTermDurationOfFundInYears__c,
                                                    MinimumInitialInvestmentForInvestor__c,
                                                    NameC04__c,
                                                    NameOfDepositary__c,
                                                    NameOfDistributorIfApplicable__c,
                                                    NameOfFund__c,
                                                    NameOfOverseasAuditor__c,
                                                    NameOfPromoterSponsor__c,
                                                    NameOfRegisteredOffice__c,
                                                    NameOfRegulatorForAdministratorContB15__c,
                                                    NameOfRegulatorForAdministratorCont__c,
                                                    NameOfRegulatorForNAVCalculationAg__c,
                                                    NameOfRegulatorForRegistrarAndTran__c,
                                                    NewInvestmentManagerApplicationRef__c,
                                                    OfferingDocTermsSummaryMarketingMat__c,
                                                    POBoxB03c__c,
                                                    POBoxB05c__c,
                                                    PrimaryInvestmentStrategyOther__c,
                                                    PrimaryInvestmentStrategy__c,
                                                    PrimaryStockExchangeOfFund__c,
                                                    ProvinceStateB03e__c,
                                                    ProvinceStateB05e__c,
                                                    RequestedDateOfRegistration__c,
                                                    RiskFactors__c,
                                                    SecondaryInvestmentStrategyOther__c,
                                                    SecondaryInvestmentStrategy__c,
                                                    SingleFundMultiFund__c,
                                                    Status__c,
                                                    StreetNameAndNumberB03b__c,
                                                    StreetNameAndNumberB05b__c,
                                                    TelephoneNumberB03h__c,
                                                    TelephoneNumberB05h__c,
                                                    TrustDeedPartnDeclMemAndAssArt__c,
                                                    Trustee__c,
                                                    ZIPPostalCodeB03f__c,
                                                    ZIPPostalCodeB05f__c,
                                                    FeePaymentsRegisteredOff__c,
                                                    FundProvideItsAdministrationServices__c,
                                                    HasNomineeInvestors__c,
                                                    InvestmentManagerIndependent__c,
                                                    IsTheAMLFunctionOutsourced__c,
                                                    IsTheNAVCalculationAgentIndependent__c,
                                                    IsTheRegistrarAndTransferAgent__c,
                                                    NAVCalculationAgentSameAdmin__c,
                                                    OngoingQueriesRegisteredOff__c,
                                                    RegisterAndTransferAgentSameAdmin__c
                                                    FROM
                                                    PrivateFundRegistration__c
                                                    WHERE Id = 'a4Q17000000UFpREAW'];

String result = PrivateFundRegistrationsJSONBuilder.convertToJSON(fundRegistration);
System.debug(result);
System.debug('FINISHED');```

I've tried deleting the cookies and cache of my browser (google chrome) and doing logout and login again but that didn't work.

If someone can help me or give me some advice I would appreciate it very much. Thanks for the time, regards.

3

2 Answers 2

4

The Problem

This error is due to the number of characters you've used. I go over it in more detail in What is the longest URI that Salesforce will accept through the REST API? , but the gist of it is that we have a url length limit of around 16,000 characters.

Anonymous apex submits requests via REST, and the code you're executing is sent via a url query parameter1. So, the url length limit applies to anonymous apex too.

In the character count, you need to include commented code and whitespace. You need to account for url encoding in that too (each single space becomes "%20", each single quote becomes "%27", etc...) as well as a few extra characters for the name of the query string parameter "anonymousBody=" and another parameter or two that Salesforce adds in there.

The Solution

The only way to fix it is to reduce the number of characters you're using.

You may be able to scrape by simply by removing all of that extra whitespace you're using for formatting your anonymous apex but, when you have that many fields, you should start to think about a way to write your query without physically typing out all of your fields.

I'd suggest making your query a dynamic query. That'll allow you to generate a list of fields (using either a fieldset or SObject describe information) to inject into your query, which will reduce the amount of characters in your anonymous apex window.

As an example:

// Getting all of the fields on Account starts with describe information
Map<String, Schema.SObjectField> accountFieldsMap = Schema.SObjectType.Account.fields.getMap();

// We then need to iterate over the map to get the api names of the fields
List<String> fieldApiNames = new List<String>();

for(Schema.SObjectField sobjField :accountFieldsMap.values()){
    fieldApiNames.add(sobjField.getDescribe().getName());
}

// Now we can construct the query
// The {0} is a placeholder that we can insert data into using String.format
String queryBase = 'SELECT {0} FROM Account';

List<Account> result = database.query(
    String.format(
        queryBase,
        // Placeholders are replaced with data from the corresponding
        //   index in this list.
        new List<String>{String.join(fieldApiNames, ',')}
    )
);

The key is that you need to construct your query in a way that doesn't require you to physically type out all of the fields in the anonymous apex window.

1: You can verify this yourself by opening the developer tools for your browser and taking a look at the network requests that are performed when you push the "execute" button in the anonymous apex window.

1
  • Wao thanks a lot Derek, you really helped me with this. I copied your code and it worked perfectly, now I understand that it was only a problem with the Anonimous Window and not with the class that I was executing itself. Thanks a lot!
    – Emmanuel_M
    Jun 19, 2020 at 16:15
-2

Solved for me by Removing Cache/Cookies Or Just using Incognito

New contributor
Svashley is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • 1
    This would only work if you're just barely past the character limit in your anonymous apex. A quick test suggests that this approach might gain you ~400 characters (i.e. slightly more than the total length of this comment). Removing commented code and extra whitespace (perhaps including indentation as a last resort) as I covered in my answer is definitely the superior approach.
    – Derek F
    2 days ago

You must log in to answer this question.

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