1

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');
        }        
    }
}
  • 1
    post your code. – superfell Aug 21 '12 at 20:05
  • Can you clarify the requirement please? E.g. Is the trigger supposed to report errors for any record w/ de_Decisao_Influencia__c exceeding 100%? – James Loghry Aug 21 '12 at 21:01
  • No, report errors only when the sum of all de_decisao_influencia__c related to PIN__c execeeding 100% understand? Thanks for help – Guilherme Gobetti Aug 21 '12 at 22:43
8

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

See:

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
];

The Trigger.new[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 Trigger.new[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 Trigger.new 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 Trigger.new[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.

1

Check if you have made the code work for batch. You should have used for loop and it should handle it for the trigger.new 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..

http://boards.developerforce.com/t5/Apex-Code-Development/Apex-update-trigger-not-working-using-Apex-Data-Loader/m-p/306219#M54050

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.