Find your content:

Search form

You are here

Trigger is not validate via data loader


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 =[0].PIN__c];

    for(SC_Cadeia_de_Influencia__c cadeia :{    
        decimal percent = 0.0;
            if(cadeia.de_Decisao_Influencia__c != null){
                percent = cadeia.de_Decisao_Influencia__c;

             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 =;
                   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');

Attribution to: Guilherme Gobetti

Possible Suggestion/Solution #1

Check if you have made the code work for batch. You should have used for loop and it should handle it for the or check if you have used Trigger.newmap.keyset or whatever in the necessary places in your trigger.

Also check if this link helps you..

Attribution to: Sathya

Possible Suggestion/Solution #2

Part of your code is only operating on the first record in the batch of records.


List<SC_Cadeia_de_Influencia__c> listaCadeia = [
    SELECT de_Decisao_Influencia__c 
    FROM SC_Cadeia_de_Influencia__c
    WHERE PIN__c =[0].PIN__c

The[0].PIN__c only gets the first element in the batch or records being inserted or updated. For example, if the batch has 10 records then records in[1-9] won't get processed in the above SOQL query.

I did notice that you are iterating over Trigger.New later in the code. That actually does iterate over every new record in the batch. That means that you are mixing the SOQL query that only gets info from the first record with apex that iterates over all records in the batch.

Please read the Apex Developer's Guide section on triggers for more information. Specifically look at the section on Trigger and Bulk Request Best Practices.

The reason that it works when you manually do it through the UI is that only has that one record on which you are operating. In other words, when you edit through the UI Trigger.New.size() == 1, so the[0].PIN__c works because the zeroth element is the only one on the list.

Basically, your code was written to only handle the situation where one record is being inserted/updated at a time.

When you fix this code make sure that you don't put your SOQL query inside of a loop or it could fail when you hit the SOQL queries limit.

Attribution to: Peter Knolle
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content