3

I was wondering if people knew how to check if a URL parameter is set?

For instance, I have an application that may or may not have a query attached to the URL. Something like this:

...salesforce.com/apex/project?id=##################

I understand to grab that ID in Apex it would look something like:

public String id = apexpages.currentpage().getparameters().get('id');

What I want to do is check if that's been set.

In PHP, I know you can check for things like this:

<?php
  if (isset($_GET['id'])) {
    // Do something...
  }
?>

I'm just looking for the Apex equivilant.

Hope I've been clear enough,

Thanks!

2 Answers 2

4

If I understand your question correctly it should simply be a case of:

    public String idParam = apexpages.currentpage().getparameters().get('id');

    if (idParam != null) {
      doSomething();
    }

You could also go a step further and check that it is an actual Id, not some other value such as a string:

if (idParam != null && idParam instanceOf Id)

I think Id is a reserved keyword in Apex so I've changed your param to 'idParam'.

3
  • This place would be more helpful if the people downvoting my answer commented on why my answer is wrong. Commented Jul 22, 2015 at 13:35
  • The id parameter shouldn't be retrieved this way; Salesforce make no assertions about its presence, and it even disappears out of the URL when viewstate is reloaded. Any other param OK! Commented Jul 22, 2015 at 18:19
  • The OPs question was simply how to check if the parameter was set - I see what you're saying but it was a simple question with a simple answer. Commented Jul 23, 2015 at 7:13
1

I am checking it this way:

  1. key exists check
  2. key is not null

Example:

if(ApexPages.currentPage().getParameters().containsKey('param1') && ApexPages.currentPage().getParameters().get('param1') != '')
{
....
}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.