Good afternoon everyone,
I have two custom objects in my org where I'll call him here and Object1 Object2, Object1 is master detail. I created a trigger in Object2 where caught all of the X values related to a record in Object1 and do a validation to check if the sum of the field X is greater than 100. If I give a larger error on the screen. Today this trigger works perfectly in case of registration records via Salesforce. My problem is when I load data via eg loader with 200 records. The trigger does not properly validate that even leaving the field X being > 100 it is registered. If I try to change a record of that Object2 manually trigger is fired and validates correctly, ie it does not work only in the data loader.
Could anyone help me? Has anyone had any similar problem?
Sorry by Google Translator
trigger ValidaPorcentagemCadeiaInfluencia on SC_Cadeia_de_Influencia__c (before insert, before update) {
List<SC_Cadeia_de_Influencia__c> listaCadeia = [SELECT de_Decisao_Influencia__c FROM SC_Cadeia_de_Influencia__c
WHERE PIN__c = :Trigger.new[0].PIN__c];
for(SC_Cadeia_de_Influencia__c cadeia : Trigger.new){
decimal percent = 0.0;
if(trigger.isInsert){
if(cadeia.de_Decisao_Influencia__c != null){
percent = cadeia.de_Decisao_Influencia__c;
}
}
if(trigger.isUpdate){
List<SC_Cadeia_de_Influencia__c> cadeiaOld = Trigger.old;
for(SC_Cadeia_de_Influencia__c cadOld : cadeiaOld){
if(cadOld.de_Decisao_Influencia__c != null){
percent = percent - cadOld.de_Decisao_Influencia__c;
}
}
List<SC_Cadeia_de_Influencia__c> cadeiaNew = Trigger.new;
for(SC_Cadeia_de_Influencia__c cad : cadeiaNew){
if(cad.de_Decisao_Influencia__c != null){
percent = percent + cad.de_Decisao_Influencia__c;
}
}
}
for(SC_Cadeia_de_Influencia__c ci : listaCadeia){
if(ci.de_Decisao_Influencia__c != null){
percent += ci.de_Decisao_Influencia__c;
}
}
if(percent > 100){
cadeia.addError('O % de decisão/influência excede 100% nos registros deste PIN');
}
}
}