0

I have a trigger that when I insert data into a custom object create a map with the id of a contact as a key and a list of the custom object as the value, when I check if the key is contained in the map I get true, but when I try to get the value I get (), but when I filled the map I check if the list has items with an if list.size()>0, this is my code:

    for(Id id : Ids){
        Boolean accs = false;
        Boolean cs = false;
        List<npe03__Recurring_Donation__c> rdOld = new List<npe03__Recurring_Donation__c>();
        for(npe03__Recurring_Donation__c rd : rds){
            if(rd.npe03__Organization__c != null && !cs){
                if(id == rd.npe03__Organization__c){
                    rdOld.add(rd);
                    accs = true;
                }
            }
            else if(rd.npe03__Contact__c != null && !accs){
                if(id == rd.npe03__Contact__c){
                    rdOld.add(rd);
                    cs = true;
                }
            }
        }
        if(rdOld.size()>0){
            rdsMap.put(id, rdOld);
            accs = false;
            cs = false;
        }
    }

for(npe03__Recurring_Donation__c rdNew : trigger.new){
     if(rdNew.Tipo_de_Compromiso__c == 'Nuevo por Aumento'){
         if(rdNew.npe03__Contact__c != null){
                    if(rdsMap.containsKey(rdNew.npe03__Contact__c)){
                        List<npe03__Recurring_Donation__c> rdsSort = new List<npe03__Recurring_Donation__c>();
                        List<npe03__Recurring_Donation__c> rdsOld = rdsMap.get(rdNew.npe03__Contact__c).clone();

                        integer targetsize = rdsOld.size();
                        while(rdsSort.size() != targetsize){
                            datetime dt = Datetime.newInstance(1900, 1, 1, 00, 00, 00);
                            integer i = 0;
                            integer mini = 0;
                            for(i = 0; i != rdsOld.size(); i ++){
                                if(rdsOld[i].CreatedDate > dt){
                                    dt = rdsOld[i].CreatedDate;
                                    mini = i;
                                }
                            }
                            rdsSort.add(rdsOld[mini]);
                            rdsOld.remove(mini);           
                        }
                        if(rdsSort.size()>0 && rdsSort[0].npe03__Amount__c < rdNew.npe03__Amount__c){

                        }
                        else{
                            rdNew.addError('No puede crear un Compromiso de Donación con el Tipo de Compormiso "Nuevo por Aumento", porque el monto ingresado es menor o igual que el monto del Compromiso de Donación Anterior');
                        }
                    }
                }
}
5
  • I do not think this code will even compile. Seems you are missing some? you start a block with else if
    – Eric
    Sep 1, 2017 at 22:11
  • @Eric you are right i don't put all the code Sep 1, 2017 at 22:22
  • if you do a debug at the end of the first loop (The Id Loop) are there definitely records for the Contact in the map? And where are the rds records in line 5 coming from? Sep 7, 2017 at 17:38
  • Can you explain what you are trying to do in the first for loop? What is Ids, and what kind of Map is rdsMap? Sep 9, 2017 at 2:00
  • the ids for are contact and account ids, the map is map<id, list<npe03__recurring_donation__c>>, I try to get all the recurring donations of a contact or all the recurring donations of an account. Sep 9, 2017 at 14:59

1 Answer 1

0

You are removing items from the lists that are the values of the map in this line:

rdsOld.remove(mini);

and so potentially can empty the lists. (Apex works "by reference" meaning when you get the value from the map you are not getting a copy of the list items but are just getting a pointer to the single copy of the list items so if you change them you change them for all references.)

Perhaps you need to make a copy of the list to avoid that:

... rdsOld = rdsMap.get(rdNew.npe03__Contact__c).clone();

so for each iteration over trigger.new you are using the populated list.

3
  • Thanks for you replay, but I put system.debug before List<npe03__Recurring_Donation__c> rdsOld = rdsMap.get(rdNew.npe03__Contact__c); to show the list and I only get () Sep 1, 2017 at 22:25
  • @AngelloCamacho Even the first debug output shows that? Strange.
    – Keith C
    Sep 1, 2017 at 23:23
  • yes this is so strange, because I put this if if(rdOld.size()>0){ rdsMap.put(id, rdOld); } but when I do the debug I get () Sep 1, 2017 at 23:46

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.