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
joestuartjoestuart 

Check checkbox value in APEX

Hi,

I have a Checkbox (GST__c) on an object called Product2, I want to  check to see if it is checked, if it is checked then apply TAX if not checked don't apply TAX.

This is what i have come up with, do I need to wrap my conditional statement in a function?
Thanks.
public Product2 product { get {
        if (product == null) { 
            try {
                product = ([Select  GST__c from Product2 where Id = :prodId]);
            } Catch (Exception e) { }
        }
        return product;
    } set; }
    
    public Boolean GSTRequired {get; set;}

if(product.GST__c == true){
  // Tax required
  GSTRequired = true;
} else {
   // Tax not required
   GSTRequired = false;
}

 
Best Answer chosen by joestuart
Santosh Bompally 8Santosh Bompally 8
Based on my understanding, If your logic is inside a Class, then yes you need to call it in a method(Call inside a constructor) Else if your logic is inside a method, you need not wrap inside another method. 

All Answers

Santosh Bompally 8Santosh Bompally 8
Based on my understanding, If your logic is inside a Class, then yes you need to call it in a method(Call inside a constructor) Else if your logic is inside a method, you need not wrap inside another method. 
This was selected as the best answer
Santosh Bompally 8Santosh Bompally 8
Yes, Perfect!!
--Glad to help
you can try this as well without writing the logic in the constructor
public Product2 product { get {
        if (product == null) { 
            try {
                product = ([Select  GST__c from Product2 where Id = :prodId]);
            } Catch (Exception e) { }
        }
        return product;
    } set; }
    
    public Boolean GSTRequired {get{
      if(product.GST__c == true)
     return true;
} set;}