You need to sign in to do that
Don't have an account?
Jagadesh
Avoid SOQL query from for loop
HI All,
Im facing some issue where i need to avoid SOQL query in for loop.My code is:
for(Case casex:cases)
{
if(casex.Account == null )
{
List accounts = [SELECT id from Account c WHERE c.Accounts_Reference_Code__c =:casex.Accounts_Reference_Code__c AND c.Accounts_Ledger__c = :casex.Origin];
if(accounts.size() < 1)
{
casex.Description = 'No Account Found';
}
else if(accounts.size() > 1)
{
casex.Description = 'Multiple Accounts Found';
}
else
{
casex.AccountId = accounts[0].id;
}
}
I need to remove SOQL query from the for Loop.Any help is greatly appreciated.
Im facing some issue where i need to avoid SOQL query in for loop.My code is:
for(Case casex:cases)
{
if(casex.Account == null )
{
List accounts = [SELECT id from Account c WHERE c.Accounts_Reference_Code__c =:casex.Accounts_Reference_Code__c AND c.Accounts_Ledger__c = :casex.Origin];
if(accounts.size() < 1)
{
casex.Description = 'No Account Found';
}
else if(accounts.size() > 1)
{
casex.Description = 'Multiple Accounts Found';
}
else
{
casex.AccountId = accounts[0].id;
}
}
I need to remove SOQL query from the for Loop.Any help is greatly appreciated.
please try following code:
Keep in mind that this code likely has errors in it, but it should get you started down the right path.
Also, Shrikants example won't work for cases where there are multiple Reference Codes for the same Ledger, or multiple Ledgers for the same Reference code, as this can introduce false positive matches for your cases.