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
Vijay BirsinghVijay Birsingh 

If condition on a checkbox

Hello everybody,

     I would like to know how do you check the value of a checkbox (isDone__c), whether it is true or false.
     I have tried this: if(isDone__c)  and I am getting the following error: Condition expression must be of type Boolean.


Thanks for any help,
Vijay,

Best Answer chosen by Vijay Birsingh
ManojjenaManojjena

Hi Vijay,

Yes you can ,Try like below .

Boolean isChecked=work.isDone__c;


 

All Answers

ManojjenaManojjena
Hi Vijay ,

You are correct but only one thing you are missing ,you need to check like below
Assume the field in account object then Account acc;
If(acc.isDone__c)
Lt me know any issue .
 
Nitin PaliwalNitin Paliwal
Hi,
Is this a property in apex or a field in an object?

Thanks
Nitin
Vijay BirsinghVijay Birsingh

Hi!

     The filed in found in Work object, therefore (work.isDone__c).
     Even then, I have the same error.

Regards,
Vijay

 

surasura
you should not give your object name it should be your variable name of your object instance (record)
ManojjenaManojjena
Hi Vijay,
try below code
If(work.isDone__c == True){

}
let me know if it helps !
praveen murugesanpraveen murugesan
Hi Vijay,

Confirm one time whether the field is checkbox and do a system.debug for work.isDone__c atlast you can try like this if(work.isDone__c == true)

Regards,

Praveen Murugesan
Vijay BirsinghVijay Birsingh

I have tried the code 

If(work.isDone__c == True){
...
}

But I get the error: Comparison arguments must be compatible types: Schema.SObjectField, Boolean

ManojjenaManojjena
Hi Vijay ,

What is the type of isDone__c ?
 
Vijay BirsinghVijay Birsingh

isDone__c is a checkbox. 
Vijay BirsinghVijay Birsingh
And, is it possible to store the value of the checkbox in a variable?
ManojjenaManojjena

Hi Vijay,

Yes you can ,Try like below .

Boolean isChecked=work.isDone__c;


 

This was selected as the best answer
Mitali DhingraMitali Dhingra
Hi Vijay,

You might have forgotten to mention the trigger context.
work workcheck = Trigger.new[0]; // if you want to check on the first record
then if(workcheck.isDone__c) will work.

In case you forget to do this, you have to explicitly change the field type to boolean
eg : Boolean isChecked=work.isDone__c;
sanjay pegasanjay pega
Hello everyone I am beginner in developing and trying to compleate one task on checkboxes. The task is about to get all the student reports(child object) when check the checkboxes of student name(parent object). Here is my code please correct it.
VFpage:

<apex:page controller="SelCheckBox_cntrl">
 <apex:form >
 <apex:pageBlock title="Select Student Name">
 <apex:SelectCheckboxes value="{!coolbool}">
 <apex:SelectOptions value="{!StudentNames}"/>
 <apex:actionSupport event="onclick" reRender="messagebox"/>
 </apex:SelectCheckboxes>
 </apex:pageBlock>
 <apex:pageBlock title="Related Selected Reports">
 <apex:pageBlockTable value="{!srep}" var="SR">
 <apex:column value="{!SR.Name}"/>
 </apex:pageBlockTable>
 </apex:pageBlock>
 <apex:commandButton value="Get Selected Reports" action="{!selctedReports}"/>
 </apex:form>
</apex:page>

APEX code:

public class SelCheckBox_cntrl {
public String StudentDetails{set;get;}
public List<Student_Reports__c> srep{set;get;}
public Boolean coolbool{set;get;}

public SelCheckBox_cntrl(){
srep = new List<Student_Reports__c>();
 coolbool = false;
}

public List<SelectOption> getStudentNames(){
List<SelectOption> Sel = new List<SelectOption>();
for(Student_Details__c SD : [Select id,Name from Student_Details__c]){
Sel.add(new SelectOption(SD.id,SD.Name));
}
return Sel;
}

public void selctedReports(){
  if(coolbool==true){
   srep = [Select id,Name,Student_Name__c,Test1__c,Test2__c,Test3__c,Certification__c,Result__c from Student_Reports__c where Student_Name__c=:StudentDetails];
    }
 else{
 system.debug('Select any Name');
  }
 }
}