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

Validation Rules and Apex Trigger
I am new to apex code but have written a trigger that updates a custom revenue field on the opportunity and that works as desired until the opportunity being updated has a close date in the past. This is due to a validation rule that does fires whenever the close date is less than today. I see that this is a common issue by searching these boards, but I have not found a solution. Is there way to writer the validation rule so that it does not fire if a particular field is being updated, or if the update is caused by the trigger?
With my limited knowledge, I cannot figure out a way around this error and subsequently no update by the trigger. I'd really appreciate some ideas on how to manage this issue. Thanks!
This is the validation rule:
AND( OR ( ISPICKVAL(StageName ,"Job Dispatched"), ISPICKVAL(StageName ,"Waiting for Approval"), ISPICKVAL(StageName ,"Searching for sub"), ISPICKVAL(StageName ,"Waiting for Approval"), ISPICKVAL(StageName ,"Approval Received - Need Contractor"), ISPICKVAL(StageName ,"Priority - Searching for Sub"), ISPICKVAL(StageName ,"Proposal/Price Quote")), CloseDate < TODAY(), IF(OR(ISNEW(), ISNULL(Id)), if(ConnectionReceivedId =null, TRUE, FALSE),NOT(AND(ISCHANGED(ConnectionReceivedId), NOT(ConnectionReceivedId = null)))), NOT(Contains($User.Alias, "pnet")) )
you can add another criteria to your validation rule NOT(ISCHANGED(<the revenue field>). This way, the rule will not fire when the revenue field is updated.
All Answers
you can add another criteria to your validation rule NOT(ISCHANGED(<the revenue field>). This way, the rule will not fire when the revenue field is updated.
Thanks! I will give that a try.
Thanks, I had to do the same thing. It would be nice if there were a way to exempt validation rules from activating if the record is being updated by an APEX trigger. I've had to add exceptions to validation rules to not activate if the person updating the record is in the sysadmin profile, but it would be nice if you could do this in the same way for APEX triggers.
Something like:
NOT(ApexId = [insert-record-id-of-the-apex-trigger-here])
Hello I am very new to Apex and i am trying to write a validation rule that alerts the owner when anoppurtunity is set to closed won...Here is what i have so far. Any suggestions? I would really appreciate the help. Thanks
IF(
AND (
ISPICKVAL( StageName, "Closed Won"),
)
It would be much easier to set a workflow rule with an email alert.
Hi @ Ssuazo
That's not an Apex Trigger, that's a Formula Language statement (2 totally different languages). It sounds like what you are trying to do is create a basic Email Alert (which you can create if your SFDC.org is on EE or UE).
Although I do have a question, wouldn't the owner of the Opportunity be the one who edits the Opportunity to Closed/Won, so they'd already know about it? Or is there some other recipient of the email alert?
If your SFDC.org is on EE or UE you can create a simple Email alert using the Formula
or the Criteria:
Opportunity:Won [equals] TRUE