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
Leonard CadetLeonard Cadet 

for loop pass array to next loop to compare array

I'm new to apex and coding in general and I'm looking for a place to talk out an issue so I can find the way to find the solution. I'm open to being given content to read to help me become better to resolve that problem. I'm also open to answer any question to the best of ability in case I'm not clear in my explaination 

I'm trying to create nested loops that compair IDs to other arrays.

The first loop will grab the first ID from the first object. Then compare a value to another objects array. 

Then pass both IDs and do a comparison again. 

Here's a snippet of the code I'm working on that is suppose to loop through everything.

It comes down to how can I pass the TT varaible from the TempTask array variable into the next loop to be used to compare a value. 

   for(CCMI__Milestone_Task__c TT:TempTask){
        for (CCMI__Milestone_Task__c CT:CCMITask){
            for(Department_To_Task__c DT:DeptTask){
               
          }
        }
      }
Best Answer chosen by Leonard Cadet
Ajay K DubediAjay K Dubedi
Hi Leonard,

I have gone through your issue. Your code is correct. You can directly access the Id of the object like this - 
 
for(CCMI__Milestone_Task__c TT:TempTask){
    for (CCMI__Milestone_Task__c CT:CCMITask){
        for(Department_To_Task__c DT:DeptTask){
            if(TT.Id == CT.Id) {

            }       
        }
    }
}

Here TT will one by one store every object from TempTask list of type CCMI__Milestone_Task__c. 
All fields of TT object can be directly accessed using the dot(.) operator. 

You can check free Apex Developer Guide pdf to learn more about the language from the link below - 

https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com

All Answers

Soyab HussainSoyab Hussain
Hi Leonard Cadet,
Nested loops should be avoided in Apex controllers because they may slow down the processing of the page or may hit the governing limits for the page.
One easy way, and which gives some nice structure to the code is to make the inner loop a separate function, or minimize using loops altogether.

Regards,
Soyab
Ajay K DubediAjay K Dubedi
Hi Leonard,

I have gone through your issue. Your code is correct. You can directly access the Id of the object like this - 
 
for(CCMI__Milestone_Task__c TT:TempTask){
    for (CCMI__Milestone_Task__c CT:CCMITask){
        for(Department_To_Task__c DT:DeptTask){
            if(TT.Id == CT.Id) {

            }       
        }
    }
}

Here TT will one by one store every object from TempTask list of type CCMI__Milestone_Task__c. 
All fields of TT object can be directly accessed using the dot(.) operator. 

You can check free Apex Developer Guide pdf to learn more about the language from the link below - 

https://resources.docs.salesforce.com/sfdc/pdf/salesforce_apex_language_reference.pdf

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com
This was selected as the best answer
Leonard CadetLeonard Cadet

Ajay, thank you, I will run with this concept and confirm if this can accomplish my requirements.