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
bohemianguy100bohemianguy100 

How can you bypass validation rules in a trigger

i have a trigger on a lead that throwing errors due to validation rules.  Is there a good way around this?  It is painful trying to skirt the validation rules when a trigger fires?

sfdcfoxsfdcfox

Validation rules fire before triggers, so you can use a before-event trigger to modify records while avoiding validation rules. After-event triggers that modify records will be subject to validation rules, since that causes a recursive trigger invocation. You can't "deactivate" validation rules for a trigger.

bohemianguy100bohemianguy100

Hello sfdcfox,

 

Thanks for replying to my post.  The trigger is an after update.  I have to use after update because it is copying the lead status field to a custom field on the account, contact and opportunity during the lead conversion process.

 

Is there a programming trick to somehow bypass the validation rules since I cannot de-activate them within the trigger?

sfdcfoxsfdcfox

The "standard" way to do this is to not use a trigger at all. Instead, just create a formula field, and map that formula field to the custom fields using (Map Lead Fields). Use a trigger only when standard functionality won't do what you want it to... and this is definitely standard functionality.

bohemianguy100bohemianguy100

I tried to use a formula field on the account, opportunityand contact to access the lead status field, but the lead object is not available in order to pull in the converted status field value.

 

That is why I was using a trigger to copy the value over.  I'm all about using the standard functionality, but unless I'm missing something, pulling in the converted lead status value is not available thru a formula field?

 

Please let me know if you know of a way to accomplish that using a formula field.

 

Thanks.

staylorstaylor

Does anyone have a solution for this?  I am in the same situation.

 

My trigger is an after update as well.  It is copying an Account field to a custom Account field.

bohemianguy100bohemianguy100

The way I've gotten around this is creating a datetime field on the object...e.g. Apex_Last_Updated__c and in my trigger I set that field to the current datetime like so:

 

Apex_Last_Updated__c = Datetime.now().getTime();

 Then in your vaidation rule, you can check the Apex_Last_Updated__c field to see if it has not changed.  Add the following check in the validation rule you want to skip when the trigger fires.

 

NOT( ISCHANGED( Apex_Last_Updated__c ))

 

So, if the field has changed it was done in your trigger and the validation rule will be skipped.  If it hasn't been changed, then the validation rule will fire normally.

 

It is a bit of a hack, but it works.

 

Hope that helps.

Regards.

ddennisddennis
This works well.  I did have to remove the "getTime();" or I got the "Error: Illegal assignment from Long to Datetime"