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
Eugena Nelson 1Eugena Nelson 1 

Opportunity validation rule for a checkbox

Would anyone have a validation rule for the following:
A rule will trigger when trying to close won an opportunity. It can only be closed won if a checkbox is true. 
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi  Eugena Nelson
Can you explain you are query little more, If checkbox is True from same object? 
if checkbox is true then user should able to select closed won stage only right?
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi Eugena Nelson,
             I just tried in my org and its working as per your requirement,
            Checkbox api name i taken private checkbox in opportunity,You can replace with your checkbox and write this trigger on opportunity object


trigger OpportunityTrigger on Opportunity (before insert,before update) {
    if(Trigger.isBefore && (Trigger.isInsert || Trigger.isUpdate)){
        for(Opportunity opp:Trigger.New){
            if(opp.IsPrivate==true){
                if(opp.StageName !='Closed Won'){
                 opp.StageName.addError('Stage should be closed won if Private is checked');
                }
            }
            }
    }
}

This validation will work on creation of new opportunity or updating existing opportunity

Hope this helps ,If it helps please mark this as  best answer

Thank you
Bhagyasri sreenadhu
Eugena Nelson 1Eugena Nelson 1
Hello Bhagyasri, 
Thank you for you help. I tried this validation rule and it gives me a syntax error, "extra opportunity trigger"User-added image
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi Eugena Nelson 1

You have to create a trigger class and use this code for validation.
Not from validationrule

Thank you
Bhagyasri
 
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi Eugena Nelson 1.

Previously I written Trigger code for validation,If you need validation rule only then you can use below code in validation rules of object(opportunity)
And Here I taken my checkbox isprivate,Instead you can give your api name of your checkbox

           AND( IsPrivate == true,
            !ISPICKVAL(StageName, 'Closed Won')    )

Error Message you can give as per your requirement

Hope it solves your issue,I also tried in my org its working

If it helps,Please mark this as best answer!!

Thank you
Bhagyasri
Eugena Nelson 1Eugena Nelson 1
Hello Bhagyasri, I have no idea how to do this. I just assumed it could be done with a validation rule. Gena
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi 
Eugena Nelson 1

Yes you are right!! You can do this with Validation rules and you can do in other ways as well like Flows,Triggers..etc

Intially I given code for trigger then as you need validation rule i posted validation rule as well as below 


           AND( IsPrivate == true,
            !ISPICKVAL(StageName, 'Closed Won')    )
This works ,If you have any requirement you can use this.Hope this helps

Thank you
Bhagyasri

 
Eugena Nelson 1Eugena Nelson 1
Thank you. I did not get a syntax error when using the below rule but it doesn't seem to be working when I try to close won the opportunity. It allows me to close won the opportunity with the checkbox being false.
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi Eugena
I Thought if checkbox is checked then stage should be closed won,Try below one for if user selected closed won stage and not selected check box

AND( ISPICKVAL(StageName, 'Closed Won'),
IsPrivate = false) 

replace isPrivate with your checkbox api
Eugena Nelson 1Eugena Nelson 1
Hello, This is still allowing for the opportunity to be close won even in the box is left false. It isn't giving an error. Can't figure out what I am missing. Gena
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
For me That validation working as expected only. Can you post your validation rule here
Eugena Nelson 1Eugena Nelson 1
User-added image
Nikhil Shah 14Nikhil Shah 14
Hi Eugena,
Could you verify if the API Name of '100% - BOOKED' is infact that value or is it 'Closed/Won' ? You should give the API name of the picklist value in the formula.
Nikhil
Bhagyasri sreenadhu 5Bhagyasri sreenadhu 5
Hi
Stage NAme you given 100%-Booked, Is it your expected stage? 
Eugena Nelson 1Eugena Nelson 1
Hello. Yes, this is the API name and also the expected stage that we'd like to have the rule trigger. 
 User-added image