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
Jose ZunigaJose Zuniga 

Dealing with a SOQL Query limit

I have the next code:

---------------------------------------------------------------------
List<User> lUsers = [select federationIdentifier, email, firstName, lastName, ContactId, isActive from User where isActive=True and FederationIdentifier != ''];
List<Account> lAccounts = new List<Account>();


for (User U:lUsers)
{
    Account uAccount = [select Id, PersonContactId, net_id__pc, personemail, firstName, LastName from account where PersonContactId = :U.ContactId].get(0);
    uAccount.net_id__pc = U.federationIdentifier;
    lAccounts.add(uAccount);  
}

system.debug(lAccounts);

--------------------------------------------------------------
But I am getting a System.LimitException: Too many SOQL queries:101. Is there any other way to write this code and avoid this problem?

Thanks

 
Ankit SehgalAnkit Sehgal
List<User> lUsers = [select federationIdentifier, email, firstName, lastName, ContactId, isActive from User where isActive=True and FederationIdentifier != ''];

List<Account> lAccounts = new list<Account>();

 lAccounts = [select Id, PersonContactId, net_id__pc, personemail, firstName, LastName from account];
List<Account> newAccounts = new list<Account>();

for (User U:lUsers)
{
    for(Account a:lAccounts)
    {
          if(a.PersonContactId ==U.ContactId)
          {
              a.uAccount.net_id__pc = U.federationIdentifier;
              newAccounts.add(a); 
           } 
     }  
}

system.debug(newAccounts);

try this.