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
jordanmjordanm 

Check if querystring parameter is null

Hello, I have this URL with 4 parameters passed in it. If the 4th family parameter is empty I want to redirect to an error page (or handle it some other way)

 

This is my url that should error out:

"/apex/osmStep1?id=a00Z00000012XJl&asmt=Outcome%20Scale%20Matrix&version=2.0&fam="

 

fam= at the end is empty

 

this is my controller code block:

   private final Participant__c participant;
   private final Assessment__c assessment;
   private final Family__c family;

   public Participant__c getParticipant() {
            return participant;
   }
   
   public Assessment__c getAssessment() {
            return assessment;
   } 
   
   public Family__c getFamily() {
            return family;
   }   
   
   public osmController() {

        	participant = [select id, Name, Family__c from Participant__c where id =
                       :ApexPages.currentPage().getParameters().get('id')];

            assessment = [select id, Name from Assessment__c where Name = 
                       :ApexPages.currentPage().getParameters().get('asmt') AND
                       Version__c = :ApexPages.currentPage().getParameters().get('version')];
                       
            if (null != ApexPages.currentPage().getParameters().get('fam'))
            {
            family = [select id, Name, Poverty_Level_2012__c from Family__c where Name = 
                       :ApexPages.currentPage().getParameters().get('fam')];
            } else {
            
            PageReference p = null;
          
          	p = Page.failure;
          	p.getParameters().put('error','noFamily');
          	
          	p.setRedirect(true);
            }
                          
   }

 This basically takes me into the page even when the fam parameter is empty. What am I doing wrong?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

I missed the most obvious piece here...

 

You can't redirect from the constructor. You're creating an ApexPage.PageReference, but you can't return it from the constructor, so no redirect occurs. Instead, you'll need an "onload" action function, like the following:

 

<apex:page controller="osmController" action="{!init}">
   <!-- code here -->
</apex:page>

Now, add your family check in the init() function:

 

public ApexPages.PageReference init() {
    String recordId = ApexPages.currentPage().getParameters().get('id');
    String assetmentName = ApexPages.currentPage().getParameters().get('asmt');
    String version = ApexPages.currentPage().getParameters().get('version');
    String famName = ApexPages.currentPage().getParameters().get('fam');
    if(famName == null || famName == '') {
        Pagereference p = Page.failure;
        p.getParameters().put('error','noFamily');
        p.setRedirect(true);
        return p;
    }

    participant = [select id, Name, Family__c from Participant__c where id = :recordId];
    assessment = [select id, Name from Assessment__c where Name = :assetmentName AND Version__c = :version];
    family = [select id, Name, Poverty_Level_2012__c from Family__c where Name = :famName];
    return null;                          
}

It's not that your check wasn't working, it's that you weren't returning the redirect. You might also want to validate your other inputs, too.

 

I apologize for missing the obvious solution the first time around.

All Answers

sfdcfoxsfdcfox
String a = null, b = '';
System.assertEquals(true,a!=b); 

Providing a blank value isn't the same as not providing a value at all (there is a difference). You must check not just against a null value, but also a blank value.

jordanmjordanm

I did try this:

            if (ApexPages.currentPage().getParameters().get('fam')!='' ||
            	ApexPages.currentPage().getParameters().get('fam')!=null) 
            {
            	family = [select id, Name, Poverty_Level_2012__c from Family__c where Name = 
	                     :ApexPages.currentPage().getParameters().get('fam')];
            } else {
            	PageReference p = null;
	          	p = Page.failure;
	          	p.getParameters().put('error','noFamily');
	          	p.setRedirect(true);
            }

 and it errors out saying 'list has no rows to add to object'     the error is on this line: family = [select id, Name, Poverty_Level_2012__c from Family__c where Name = :ApexPages.currentPage().getParameters().get('fam')];

 

which unless my logic is flawed, should not be executing at all since my queryString fam= is empty/null

 

I'm lost on this one

sfdcfoxsfdcfox

Try adding a debug statement:

 

String famString = ApexPages.currentPage().getParameters().get('fam');
System.debug('***'+famString);

In the Development Console window, filter by '***' and see what you come up with.

jordanmjordanm

12:36:27:060 USER_DEBUG [328]|DEBUG|***

 

makes no sense; that proves that the param is empty or null, right?

 

 

p.s. sometimes when the page renders and the fam param is empty, it doesn't show "&fam="

 

it shows nothing for it at all, would that matter? regardless of whether the empty parameter's heading is there or not, the behavior still remains the same

sfdcfoxsfdcfox

I missed the most obvious piece here...

 

You can't redirect from the constructor. You're creating an ApexPage.PageReference, but you can't return it from the constructor, so no redirect occurs. Instead, you'll need an "onload" action function, like the following:

 

<apex:page controller="osmController" action="{!init}">
   <!-- code here -->
</apex:page>

Now, add your family check in the init() function:

 

public ApexPages.PageReference init() {
    String recordId = ApexPages.currentPage().getParameters().get('id');
    String assetmentName = ApexPages.currentPage().getParameters().get('asmt');
    String version = ApexPages.currentPage().getParameters().get('version');
    String famName = ApexPages.currentPage().getParameters().get('fam');
    if(famName == null || famName == '') {
        Pagereference p = Page.failure;
        p.getParameters().put('error','noFamily');
        p.setRedirect(true);
        return p;
    }

    participant = [select id, Name, Family__c from Participant__c where id = :recordId];
    assessment = [select id, Name from Assessment__c where Name = :assetmentName AND Version__c = :version];
    family = [select id, Name, Poverty_Level_2012__c from Family__c where Name = :famName];
    return null;                          
}

It's not that your check wasn't working, it's that you weren't returning the redirect. You might also want to validate your other inputs, too.

 

I apologize for missing the obvious solution the first time around.

This was selected as the best answer
jordanmjordanm

Bingo! I was going crazy trying to figure this out.


Thank you so much!