[SalesForce] What reasons would the id in URL be null

My app is getting the object id from the URL query string with the following code:

ApexPages.currentPage().getParameters().get('id')

However, I have a customer who is reporting that this isn't working. The above line of code is returning null, even though I see the correct object ID in their query string. Of course, I'm unable to reproduce the error, and I'm looking for leads on what to look at to find the problem.

What reasons would my Apex code not be able to access the existing id parameter of my current page?

UPDATE: The customer is reporting that the error is only happening on the Business Account object type, with the Person Account and other objects types (Contacts, Opportunities, etc) have no problems. Could differing Account object recordtypes cause this type of issue?

UPDATE 2: Finally got the code onto an org that I could access with Person Accounts enabled. Turns out having Person Accounts enabled is the issue. Unlike I thought in my original question above, the id is getting retrieved from the URL. But later in the code, when querying salesforce for the Account's Name Field we stop when the first Name Field is found (because objects normally have only one Name Field). However, with Person Accounts enabled Accounts now have 3 Name Fields: LastName, FirstName and Name. LastName comes up first. So, when trying get the name value for a Business Account, LastName is null, and thus breaking my code.

Best Answer

If they open your page without supplying an id parameter then it will be null in your Controller.

e.g. if they navigate to your page by typing the URL

http://instance.salesforce.com/apex/YourPageName

Since you can't stop users doing this, you should check if the id parameter is null in your Controller and either handle this case explicitly or if it can't be handled then display an error message to the user.

Related Topic