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

Verifying Checkbox field value from a SOQL using Apex class.

HI I need help. I have a SOQL Query that has a checbox field  - Authorized_Purchaser__c - as on of  its attribute and wanted to check the value of this field in an IF condition statement but it seems that the value of the check box field is always true even when it is  unchecked(!?). I put debug statements on the  check box and it displays that it is true. However, after looking at it in the Affiliation object Detail Page,  I have seen that it is unchecked and thus should be false . Here is a portion of the code in question and checkbox field Authorized_Purchaser__c is the field I am trying to verify which is unchecked.  Thus it should be false however the value is set to true so the pricebook will never be added to the list of Price books. What am I missing?  Thanks much for your help.
boolean isAuthorized = true;
List<npe5__Affiliation__c> aff = [Select npe5__Organization__r.Registration_Level__c,Authorized_Purchaser__c,npe5__Organization__r.Name from npe5__Affiliation__c where npe5__Contact__c =: ContactId and npe5__Organization__r.Id =: AffID limit 1 ];
isAuthorized = aff[0].Authorized_Purchaser__c;
 system.debug('<< IS AUTHORIZED? >>> '+aff[0]);
for( PriceBook2 PriceBook : [SELECT Id, Name FROM PriceBook2 Where isActive = true and Name in ('Employee Price Book','Contractor Price Book')    Order By Name DESC ]){ 
    if(isAuthorized ==false && PriceBook.Name == 'Employee Price Book'){

Best Answer chosen by tonante
UC InnovationUC Innovation
Please remember to mark best answer if this helped resolve your issue(s).

All Answers

Edwin VijayEdwin Vijay
Things you could check
  • Check if you are referring to the correct record, debug aff[0].Id and verify if in the page layout you see the same record
  • check if the default value of the field is set to 'Checked', you can see this by going to setup=>create=>object=>field
A screenshot would help to debug futher.

UC InnovationUC Innovation
Try this instead. Don't set the first boolean to true and instead  This will ensure that you are actually checking the value of the field in the record.
npe5__Affiliation__c aff = [SELECT npe5__Organization__r.Registration_Level__c,
							FROM npe5__Affiliation__c 
							WHERE npe5__Contact__c = :ContactId AND
								  npe5__Organization__r.Id = :AffID 
							LIMIT 1]; // since limit 1 here no need for list
for(PriceBook2 PriceBook : [SELECT Id, 
							FROM PriceBook2 
							WHERE isActive = true AND 
								  Name IN ('Employee Price Book','Contractor Price Book')    
							ORDER BY Name DESC ]){						
    if(!aff.Authorized_Purchaser__c && PriceBook.Name == 'Employee Price Book'){
If this doesnt work you might want to try to make sure that the 'aff' you query for is the one that is returning from the query so you might want to try checking the values compared in the QHERE clause of that query.

Hope this helps!

HI Ken I wonder if I should not try and bulkify my query since it is limit 1. You have shown this in your exmaple that you are dealing with just one object so maybe I will also try that as well as the IF statement and let you know.
UC InnovationUC Innovation
Please remember to mark best answer if this helped resolve your issue(s).
This was selected as the best answer
UC Innovation thanks it is working now.