How to fix error 431 Request Header Fields Too Large . HTTP CODE[431] in Anonymous Window

1.4K    Asked by DanielBAKER in Salesforce , Asked on Apr 23, 2021

I'm getting http error 431 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


===================

```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. apex


Answered by Daniel Cameron

Reason of getting the http error 431 is that you may have exceeded the character limit. The HTTP 431 Request Header Fields Too Large response status code indicates that the server refuses to process the request because the request's HTTP headers are too long. The standard 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 " ", each single quote becomes "'", 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 HTTP error 431 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 number of characters in your anonymous apex window.

As an example:

  // Getting all of the fields on Account starts with describe information Map accountFieldsMap = Schema.SObjectType.Account.fields.getMap(); // We then need to iterate over the map to get the api names of the fields List fieldApiNames = new List(); 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 result = database.query( String.format( queryBase, // Placeholders are replaced with data from the corresponding // index in this list. new List{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.



Your Answer

Interviews

Parent Categories