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
David Holland 6David Holland 6 

Strange behaviour with boolean value of dynamic query string

Hi all, hoping someone can help.

I won't bore you with the ins and out of the code, but basically, I have a query string I am dynamically populating from an SObject and seeing if it is true.

The code snippet is below:
 
private static Boolean shouldShowInInbox(String queryString, SObject sObjectToCheck, Set<String> fieldsToCheck){
        for(String fieldToCheck : fieldsToCheck){
            String fieldValueOnObject = String.valueOf(sObjectToCheck.get(fieldToCheck));
            queryString = queryString.replaceAll(fieldToCheck, String.isBlank(String.valueOf(fieldValueOnObject)) ? '' : String.valueOf(fieldValueOnObject));
        }
        System.debug(queryString);
        System.debug(Boolean.valueOf(queryString));
        return Boolean.valueOf(Boolean.valueOf(queryString));
    }



It is returning me these two debugs and I have no idea why!

qforcesupport@quintessentially.com == qforcesupport@quintessentially.com
false





So it is stating that the two values are not the same....

Please someone help!
 
Best Answer chosen by David Holland 6
Jayant JadhavJayant Jadhav
Hi David,

This is bcz of Boolean class valueOf method behaviour. In your code Boolean.valueOf(queryString), in this statement valueOf method just look for string value 'true' and if it is other than 'true' then it will return false. In your code value of queryString is qforcesupport@quintessentially.com == qforcesupport@quintessentially.com and not string of 'true'. 

Plz refer this link for more information. https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_boolean.htm

All Answers

Jayant JadhavJayant Jadhav
Hi David,

This is bcz of Boolean class valueOf method behaviour. In your code Boolean.valueOf(queryString), in this statement valueOf method just look for string value 'true' and if it is other than 'true' then it will return false. In your code value of queryString is qforcesupport@quintessentially.com == qforcesupport@quintessentially.com and not string of 'true'. 

Plz refer this link for more information. https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_boolean.htm
This was selected as the best answer
David Holland 6David Holland 6
Jayant

Of course,how stupid of me.

Is there a way to dynamically fire that queryString to return me the boolean of it?

I am struggling to find any documentation to allow me to do so.
Jayant JadhavJayant Jadhav
David,

I am not sure about any method but you can use split method to split this string and then compare two strings. For example.
 
Boolean status=false;

String[] query=queryString.split('==');

if(query[0].equalsIgnoreCase(query[1] )) //  or query[0].equals(query[1] 
{
    status=true;
}
else
{
     status=false;
}
return status;

Plz let me know if this works for you.
Jayant JadhavJayant Jadhav
David,

Plz replace if stmt with below stmt. I miss out trim() method.
if(query[0].trim().equalsIgnoreCase(query[1].trim()))

This will remove spaces in string and then match.
Amit Chaudhary 8Amit Chaudhary 8
Try below code:-
 
private static Boolean shouldShowInInbox(String queryString, SObject sObjectToCheck, Set<String> fieldsToCheck)
{
        for(String fieldToCheck : fieldsToCheck)
		{
            String fieldValueOnObject = String.valueOf(sObjectToCheck.get(fieldToCheck));
			
			if(fieldValueOnObject =='true')
			{
				// add your query below	
				// queryString = queryString +' '+fieldToCheck+' =:true';
			}
			else if(fieldValueOnObject =='false')
			{
				// add your query below	
				// queryString = queryString +' '+fieldToCheck+' =:false';
			}
			else
			{
				queryString = queryString.replaceAll(fieldToCheck, String.isBlank(String.valueOf(fieldValueOnObject)) ? '' : String.valueOf(fieldValueOnObject));
			}
        }
        System.debug(queryString);
        System.debug(Boolean.valueOf(queryString));
        return Boolean.valueOf(Boolean.valueOf(queryString));
}
Please let us know if this will help you
 
Jayant JadhavJayant Jadhav
@David,

Hope above post help you to resolve your issue. Kindly mark this as best asnwer, this will help others if they face same issue.