You need to sign in to do that
Don't have an account?
bathy
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
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
Use Database.insert(ListName,false) as a dml operation in your trigger. It just bypass the error and print the other successful values.
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;
}
Can you tel me your validation rule..We can't bypass the validation rule,we can only bypass the error while updating.
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);
}
What does this statement do actually? Can you please explain.
I dont have much knowledge in Programming.
thanks,
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.
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?
No,You can't.The only way we can inactivate the validation rule and make the trigger to fire.