1

I have a controller where I have this code. I need to pass BooleanVariableName to another controller

  PageReference pageRef = new PageReference('/apex/ContactV1?id=' + contact.Id);
  pageRef.getParameters().put('BooleanVariableName', 'true');
  pageRef.setRedirect(true);
  return pageRef;

In second controller, I put the following code to get the value for BooleanVariableName. This controller is the conroller for page ContactV1

 Boolean  PBV = system.CurrentPageReference().GetParameters().get('BooleanVariableName');

When I tried to save it it gave me the following error. Error: Compile Error: Illegal assignment from String to Boolean

Please help.Thanks in advance

3

All parameters are passed as strings so you will have to do this:

if you are not going to ever call the controller from anon apex and always be in VF context you can do this (sfdcfox):

boolean pbv = ApexPages.currentPage().getParameters().get('BooleanVariable‌​Name') == 'true';

otherwise (Generally not needed and not typically a pattern you should follow as there are other ways to mitigate it)

Boolean PBV;

try{
    PBV = boolean.valueOf(ApexPages.currentPage().getParameters().get('BooleanVariableName'));
}catch(Exception e){ //covers not in VF context use case
   PBV = false;
}
  • 1
    You don't need the try-catch: pbv = ApexPages.currentPage().getParameters().get('BooleanVariableName') == 'true'; – sfdcfox Mar 8 '17 at 1:53
  • @sfdcfox - You do if testing the controller frrm anon apex....When currentPage is null. But I added this to answer as generally you would not you are right. I just went through this so thats why it was on my mind. I could not debug a controller in VF (SF logs are broken - missing entry points) so I had to call it manually and could not since current page was null. – Eric Mar 8 '17 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.