I have two object foo__c
and zoo__c
. zoo__c is a child of foo__c with a master detail relationship. I have validation rule created on Zoo__c with a condition that when the status__c
field on related Foo__c
object is 'Open
' then throw an error. => Foo__r.status__c== 'Open'
I wrote a trigger on Foo__c
, which creates a new zoo__c
record whenever foo__c
is updated. Below is my logic in trigger:
trigger footrigger on Foo__c (after insert,after update) {
if(trigger.isUpdate && trigger.isAfter){
List<zoo__c> zlist = new List<zoo__c>();
For(Foo__C f : trigger.new){
Zoo__c z = new Zoo__c();
z.Foo__c = f.id;
z.Name='AZooObj';
zlist.add(z);
}
Database.insert(zlist,false);
}
}
Note I use Database.Insert
to pass the records. Hence the records that meets validation rule on zoo__c
should create a new zoo__c
record and fail the one which does not meet the rule.
The Problem : When I update a foo__c
record from UI which has its status__c
field as 'Open'
, the validation rule does not kick in and it updates the foo__c
record without any error. However when I use Insert
statement instead of Database.Insert
, the validation rule kick in at that time. How can I resolve this situation with Database.Insert and Validation rule. I want to only thrown the error for the records that does not meet the validation rule and pass other records which meets the validation rule. Say I am updating 30 foo__c
records using dataloader. If I use Insert
statement, it fails all the records which I do not want. I want to fail only the ones which does not meet the validation rule. Any suggestions?
zoo__c
records for those instances that you want them? i.e. check first whether thefoo__c
record is open before creating thezoo__c
record