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
pmozz01pmozz01 

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"))
)

 

 

Best Answer chosen by Admin (Salesforce Developers) 
geetageeta

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

geetageeta

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.

This was selected as the best answer
pmozz01pmozz01

Thanks! I will give that a try.

TGHTGH

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])

SsuazoSsuazo

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"),

)

pmozz01pmozz01

It would be much easier to set a workflow rule with an email alert.

Steve :-/Steve :-/

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? 

Steve :-/Steve :-/

If your SFDC.org is on EE or UE you can create a simple Email alert using the Formula

 

 

IsWon = TRUE

or the Criteria:

Opportunity:Won  [equals]  TRUE