You need to sign in to do that
Don't have an account?

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
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:
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!
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.
Ahhhh.....didn't realize that adderror prevents the saving of the record...as you can tell I am new to writing apex.
Thanks!
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...
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 ...
The "!" before your Boolean field is the same as saying "NOT".
THANKS David!!
!Really_Bid__Deal__c is exactly what I needed!!!
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).
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... :)
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))
Got it!!!
Thanks for your HUGE help!