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
bathybathy 

Custom validation rules interfering Apex triggers

Hi All,

Is there a way to bypass validation rules in Triggers? I have created some new validation rules and I see triggers getting failed due to this.
OR is there any other way to implement validation  rules getting triggered only while manual entry?
Please advise.
Thanks
SethuSethu
Hi bathy,

Use Database.insert(ListName,false)  as a dml operation in your trigger. It just bypass the error and print the other successful values.
bathybathy
thanks for your suggestion sethu.
here is my trigger to update related opportunities when a new task is created.

Please advise where should I include your statement.
thanks,
trigger UpdateOppnextstepnew on Task (after insert) {
  
  Set<String> whatIDs = new Set<String>();
  
    for (Task t : Trigger.new) 
    {
      whatIDs.add(t.whatID);
  }
  
     List<Opportunity> opps = [SELECT Id,Notes2__c FROM Opportunity WHERE Id =: whatIDs];
    
     Map<String, Task> taskMap = new Map<String, Task>();
  
    for (Task t : Trigger.new){
      
       
          taskMap.put(t.whatID, t);
        }
      
     
        
    for (Opportunity o : opps) {
      if (taskMap.containsKey(o.Id) && (taskMap.get(o.Id).Next_Step_is__c != null) ) 
      {
      
        o.Notes2__c =  o.Notes2__c + '\n' + taskMap.get(o.Id).Next_Step_is__c;
        
      }
  }
  update opps;
}
SethuSethu
Hi ,
Can you tel me your validation rule..We can't bypass the validation rule,we can only bypass the error while updating.
 
SethuSethu
Hi Yamini,
Try this
trigger UpdateOppnextstepnew on Task (after insert) 
{
  list<opportunity> oppList = new list<opportunity>();
  Set<String> whatIDs = new Set<String>();
  
    for (Task t : Trigger.new) 
    {
      whatIDs.add(t.whatID);
  }
  
     List<Opportunity> opps = [SELECT Id,Notes2__c FROM Opportunity WHERE Id =: whatIDs];
    
     Map<String, Task> taskMap = new Map<String, Task>();
  
    for (Task t : Trigger.new){
      
       
          taskMap.put(t.whatID, t);
        }
      
     
        
    for (Opportunity o : opps) {
      if (taskMap.containsKey(o.Id) && (taskMap.get(o.Id).Next_Step_is__c != null) ) 
      {
      
        o.Notes2__c =  o.Notes2__c + '\n' + taskMap.get(o.Id).Next_Step_is__c;
        oppList.add(o);
      }
  }
 Database.update(oppList,False);
}
bathybathy
Hi Sethu,

What does this statement do actually? Can you please explain.
I dont have much knowledge in Programming.

thanks,
SethuSethu
Hi,
if you have written a validation rule on some object (say Account) as if the name is 'Test1' it should throw error.While inserting the name thru trigger i.e

for(i=0;i<5;i++)
{
 Account a = new Account();
a.Name ='Test'+i;
}

It will insert Test0 to Test4.While inserting 'Test1' Trigger will fail due to validation rule written on Account object.Database.insert/update will bypass the error and insert Test0,Test2,Test3,Test4 not Test1.

Hope it may clear you.

 
bathybathy
hi Sethu,

If I put this in my trigger then it will skip updating the opportunities. but I want the opptys to be updated by bypassing the validation rules. Is there any other way?
SethuSethu
Hi ,
No,You can't.The only way we can inactivate the validation rule and make the trigger to fire.