function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KevinRussellKevinRussell 

How to Loop through List using For statement, multiple times....

I'm trying to loop through a local list a number of times.  It seems like it works when I have a single record but not when I have more.  I suspect that my multiple 'for' loops don't start from the begining of the list each time.

Can anyone tell me where I'm going wrong?

 

Thanks,

 

Kevin

 

Integer SpeakingRecordCount;
Boolean PastLeadershipBoard;
Boolean EmeritusTrustee;
Boolean LeadershipBoard;


try {

// Store Contact record ID
map< id, contact > contacts = new map< id, contact >();
map< id, account > accounts = new map< id, account >();
       
if (trigger.isinsert || trigger.isupdate) 
    {
     
       // Create trigger for new or selected npe5__Affiliation__c  record
       for(npe5__Affiliation__c  record:trigger.new)        
       {

        // Build local list of Affiliation records 
       List<npe5__Affiliation__c > affl = new List <npe5__Affiliation__c>();
       affl=[Select id, npe5__Contact__c, npe5__Organization__c ,Type__c, npe5__Status__c, npe5__Role__c from npe5__Affiliation__c  
       WHERE npe5__Contact__c = :record.npe5__Contact__c AND npe5__Organization__c = :record.npe5__Organization__c ];

        // Loop through the local Affiliation list to parse results for Contact Record


        for (npe5__Affiliation__c li: affl) {
            if (li.npe5__Contact__c == record.npe5__Contact__c 
                && li.Type__c.contains('Board') && li.npe5__Status__c == 'Current' )
                LeadershipBoard = TRUE;
            else {
                LeadershipBoard = FALSE;
            }
        }
        
        for (npe5__Affiliation__c li: affl) {
            if (li.npe5__Contact__c == record.npe5__Contact__c 
                && li.Type__c.contains('Board') && li.npe5__Status__c == 'Former' )
                PastLeadershipBoard = TRUE;
            else {
                PastLeadershipBoard = FALSE;
            }
        }
        
     // Construct Contact record update with results  
        contacts.put(record.npe5__contact__c, new contact(id=record.npe5__contact__c, Leadership_Board__C = LeadershipBoard, Past_Leadership_Board__c = PastLeadershipBoard ));               

     // Update Contact record
        update contacts.values(); 


        }   
    }

 

 

ArunDavidNSIArunDavidNSI

Is your code throwing up any governor limit errors or is it just not functioning? You need to try and keep queries outside of the loop. The moment you have multiple records, you will start hitting limits for queries executed in a single execution.

 

Arun

KevinRussellKevinRussell

I'm showing a partial code list above, the code does compile and run, but I get inconsistant results.  I almost get the feeling that:

 

- at each for-loop, it is not reading from the start of the local list named 'affl'.

- it is not readding the affl list, maybe the source list

- at each for-loop it exits before it reads the entire list..

- maybe my contact..put statement is not configured correctly

 

I don't know how to see variable values as the code steps though to really understand what's going on.

 

Having said all that, my full production trigger code has 8 queries, I'm trying to reduce to a single query and loop over that local list 8 times to get the 8 values I need.

 

Then, yesterday it hit me, I realized that after I get this done, I still have to figure a way to 'bulkify' this trigger.  If I get this working I'm still limited to 100 external record updates/modifications.  Right now I can only import/modify 14 records because of the queries in my trigger.

 

My initial trigger, with 8 seperate queries in it, works fine when working manually through the Salesforce web interface, the problem is when I need to upload/modify 6,000+ records, then I come face to face with those limits!!!!

 

We are hooking Salesforce up to three external systems and trying to keep Salesforce sync'd daily.  That's why I care so much about figuring this out.

 

Thanks for any help,

 

Kevin