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
NewGirlNewGirl 

How do I evaluate a check box field in an apex trigger

 

I have a custom field called Really_Big_Deal__c that is a data type "checkbox".  
I need to evaluate it in an apex trigger.  I know checkbox is a boolean - what do I need to do to evaluate this?
The code is as follows...
trigger OrderOwnerUpdate on Opportunity (before insert, before update) {
//change the owner from customer service to RSM
ID TerritoryId;
Opportunity[] opp = Trigger.new;
for (opportunity s:opp)
  {
    if (s.name != null)
        {
            ID OpportunityOwnerId = [Select User_id__c from territories__c 
            where name = :s.opp_ship_to__c].user_id__c;
            s.ownerid = OpportunityOwnerId;
            
            if ((s.amount >9899) && (s.Really_Big_Deal__c == true ))
            {
                s.addError('Check Big Deal Field if sale is over $9899.00');
            }
        }
  }
}
Thanks for any insight to this issue!
Karen

 

David81David81

One thing you'll have to be careful of is putting SOQL queries inside for loops. You'll run into governor limits in no time flat.

Also, you're setting up TerritoryId but it doesn't get used in the code. Is it needed?

 

The other bit that checks the Really Big Deal box, might be better handled with a validation rule. It'll be much simpler to maintain and if you can do it through the UI, by all means, do it that way.

 

Other than that, it looks OK. I'd do something like this:

 

 

trigger OrderOwnerUpdate on Opportunity (before insert, before update) {
	//change the owner from customer service to RSM
	Map<String,Id> ownerIdMap = new Map<String,Id>();
		
	for (Opportunity o : trigger.new){
		if (o.name != null && o.opp_ship_to__c != null){
			ownerIdMap.put(o.opp_ship_to__c,null);
		}
	}
	if(!ownerIdMap.isEmpty(){
		for(Territories__c t : [SELECT User_id__c FROM Territories__c WHERE Name IN :ownerIdMap.keyset()]){
			ownerIdMap.put(t.Name,t.User_Id__c);
		}
	}
	
	for(Opportunity o : trigger.new){
		if(ownerIdMap.containsKey(o.opp_ship_to__c) && ownerIdMap.get( o.opp_ship_to__c != null){
			o.OwnerId = ownerIdMap.get(o.opp_ship_to__c);
		}
	}
	
}

 

 

 

 

NewGirlNewGirl

Thanks for your help. I can't use a validation rule as I just want to display a warning message without stopping the transaction. I thought I the next best thing would be to handle it in a trigger.

 

That being said, do you have any recommentations on how to evaluate really_big_deal__c which is declared in the table as a data type checkbox (boolean)?

 

Thanks again!

 

dmchengdmcheng

Not sure what your question is because your syntax is correct. 

 

s.Really_Big_Deal__c == true

 

You know that addError prevents the record from being saved, so you have not gained anything compared to using a validation rule.  If you want show an informational message and still save the record, you will have to build a  VisualForce page and controller instead.

NewGirlNewGirl

Ahhhh.....didn't realize that adderror prevents the saving of the record...as you can tell I am new to writing apex.

 

Thanks!

David81David81

One thing to consider....

 

If "Really Big Deal" should be checked when the amount goes above a certain level, why not put a workflow rule in place to check it automatically if the user doesn't do it themselves...

NewGirlNewGirl

 

I have tested the code many ways - when I "check" really_big_deal_cc (data type check box) it NEVER goes into this "if" statement....as it doesn't go into the if statement if it is false either...how can I display the values of s.amount and s.really_big_deal__c???
Why is it never entering the if statement??
if ((s.amount >9899) && (s.Really_Big_Deal__c == true ))
THANKS!!
trigger OrderOwnerUpdate on Opportunity (before insert, before update) {
//change the owner from customer service to RSM
ID TerritoryId;
Opportunity[] opp = Trigger.new;
for (opportunity s:opp)
  {
    if (s.name != null)
        {
            ID OpportunityOwnerId = [Select User_id__c from territories__c 
            where name = :s.opp_ship_to__c].user_id__c;
            s.ownerid = OpportunityOwnerId;
            
            if ((s.amount >9899) && (s.Really_Big_Deal__c == true ))
            {
                s.addError('Check Big Deal Field if sale is over $9899.00');
            }
        }
  }
}
Thanks for any insight to this issue!
Karen

 

David81David81

You'll want to put some system.debug statements before the if. I usually put in the asterisks just to make the debug statements easy to find in the debug logs. Now that I'm looking at it though, don't you want to add the error if Really Big Deal is not checked (FALSE)? The shorthand way would be ...

 

 

system.debug('****AMOUNT = ' + s.amount);
system.debug('****REALLY BIG DEAL = ' + s.Really_Big_Deal__c);
if(s.Amount>9899 && !Really_Big_Deal__c){
}

 

The "!" before your Boolean field is the same as saying "NOT".

 

NewGirlNewGirl

THANKS David!!

 

 

!Really_Bid__Deal__c is exactly what I needed!!!

David81David81

Glad I could help.

 

I still think that logic could/should be moved out of the trigger and into a Validation Rule (since your addError is essentially the same) or Workflow Rule/Field Update (to check the box automatically, without user intervention).

NewGirlNewGirl

I see what you are saying...

 

I just tried to create a field validation...as :

 

amount >9899 and not(really_big_deal__c)

 

I also tried amount >9899 and !really_big_deal__c

 

Back to my original question (almost)  - How do I evaluate the boolean in a field validation...  :)

 

 

David81David81

The syntax for Validation Rules is a bit different, more similar to formulas in Excel if you are familiar with those. Luckily each function is pretty well documented, so if you select "AND" from the list, you'll get the basic syntax and a link to further details.

 

In a nutshell, you'll want AND(Amount>9899, NOT(Really_Big_Deal__c))

NewGirlNewGirl

Got it!!! 

 

Thanks for your HUGE help!