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
Paul - SalesbriPaul - Salesbri 

Convert To Boolean - System.TypeException: Invalid boolean: true

Hi All,

Amazing how the simplest things are the most challenging.

I'm trying to convert a string of false / true to a boolean, I've tried the code below and I keep getting 'System.TypeException: Invalid boolean: true'.  I have no idea why, as my other type conversions are working fine.

Code below is an extract of a test function, the actual usage is in a much larger method!  Please help!

public class convertToBool
{
    public static void testConvert()
    {
        Integer i;
        String s;
        s='10';
        i=integer.valueof(s);
        system.assertEquals(10,i); //this test passes
   
        string asdf = 'true';
        //boolean bob = boolean.valueof(asdf);
        system.debug(Boolean.valueOf(asdf));
    }
   

    public static testMethod void testConvertToBool()
    {
        testConvert();
    }
}
LosintikfosLosintikfos
Dont know why you doing this!

Why not go straight and use the ineteger value and a boolean, instead of what you doing.

for example:
String s;
        s='10';
        i=integer.valueof(s);
        system.assertEquals(10,i);

Can be simply:
int s = 10;

and:

string asdf = 'true';

boolean asdf = true;

Unless you have a reason for doing this, tell the forum why this.

 


Message Edited by Losintikfos on 09-22-2008 04:50 AM
LosintikfosLosintikfos
This will work fine in Java:

Code:
String strBoolean = "true";
        
 //Do the String to boolean conversion
 boolean Pipe = Boolean.parseBoolean(strBoolean);
        
 System.out.println(pipe);

 You can try the conversion in apex! see what happens and let the forum know about the end result.
Paul - SalesbriPaul - Salesbri
Hi Losintikfos,

Thanks for the post, we need to convert the date we receive from an external source that is all typed as text into a boolean for a field in sf.com as we can't get the provider to change the data types for us.

I tried your java method but it doesn't exist in APEX, and the strange thing is that it won't convert an integer to a boolean, when the docs say 0 = false and 1 = true.

    public static void testConvert()
{
Integer i;
String s;
s='1';
i=integer.valueof(s);
system.assertEquals(1,i); //this test passes

boolean bob = boolean.valueof(s);
system.debug(bob);
}
Any assistance would be great.
LosintikfosLosintikfos
Before we go ahead into your issue! can i ask you this;

It is possible to to change the field  type in salesforce to text? or this will fire a crash to some dependable applications.


Cheers,
B
Paul - SalesbriPaul - Salesbri
Great minds, however yes - unfortunately we have to keep it as a boolean.
LosintikfosLosintikfos
have you tried this in event handler too?

like this
Code:
//Declare a variable to hold condition
boolean date = false;

//Test the condition
if (string asdf == 'true')
//Set the field condition to true
date = true;

This will make sure everytime it encounters the string value true the handler is set to true, else otherwise. Note you can pass multiple test condition using pipe. Example;
Code:
(string asdf == 'true' || String date = 'true' || String tdate = 'true')

 



LosintikfosLosintikfos
Any success story?
yibongleeyibonglee

I have same issue, did you find the answer?

 

it's very strange situation...

It may be a bug... I think...

 

but I followed the Losintikfos's suggestion,  it works.

thanks,  Losintikfos!

 

this case doesn't work... 

 

String value = ApexPages.currentPage().getParameters().get('value');if (value != null){checkboxvalue8 = Boolean.valueOf(value);}

 

 but this solution works!!

 

String value = ApexPages.currentPage().getParameters().get('value');if (value != null && value == 'true'){checkboxvalue8 = true;}

 

 

 

 

Message Edited by yibonglee on 02-23-2009 02:01 PM
Message Edited by yibonglee on 02-23-2009 02:01 PM