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
arasuarasu 

Inline query has too many rows for direct assignment, use FOR LOOP

Hi,

I have the following apex code that throws this error:
Inline query has too many rows for direct assignment, use FOR LOOP
Code:
 WebService static Integer processRecordsToInsert(List<ID> pupcid,List<ID> palliid,String preason) {
  Integer success=0;
  Integer failure=1;
 try{
  
 List<Partner_Relationship__c> insertedPR = new List<Partner_Relationship__c>();
 List<Account> insertedAcc = new List<Account>();
     Savepoint sp_insert = Database.setSavepoint(); 
 Partner_Relationship__c prd = new Partner_Relationship__c();

   //Get the list of Card UPCs that are not in Partner Relationship object
   List <Card_UPC__c> lupc=[Select id from Card_Upc__c where id NOT IN :pupcid];
   
   //loop thru each UPC, only 2 UPCs therefore loops only 2 times
   for(Card_UPC__c upcdata:[Select Id from Card_Upc__c where Id IN :pupcid ] ){
    
   //loop thru each Alliance, only 2 Alliances therefore loops only 2 times
    for(Account accdata:[Select id from Account where Id IN :palliid] ){              
     prd = new Partner_Relationship__c(Card_Upc__c=upcdata.Id,Alliance_Account__c=accdata.Id,Status__c='Card Partner Discontinued');
          insertedPR.add(prd);      
    }
   }
   Database.SaveResult[] lsr = Database.insert(insertedPR,true);     
   return success;
 
 }catch (DmlException e) {
   
      throw e;
      return failure;
   }catch (Exception e) {
      throw e;
      return failure;
   }           
   }    

 
Appreciate all your valuable feedbacks on this.
Thanks
jrotensteinjrotenstein
Have you tried assigning the result of your query to an array, the iterate over the array?

For example:

Code:
Card_UPC__c[] upc_results = [Select Id from Card_Upc__c where Id IN :pupcid]

for (Card_UPC__c upcdata : upc_results ){

 

111111
        Account[] account = [select Name from account where Name = 'Acme' for update];
   
        for (Account ac : account) {
            ac.Name = 'Acme1';
           
        }
 
why error is appeared?
Inline query has too many rows for direct assignment, use FOR LOOP
jrotensteinjrotenstein
111,

What is the error message? What are you attempting to do? Are you writing this code in a Trigger?

Firstly, remove "for update" -- this isn't a database.

After you modify a value, use the update command to save the change.
111111
remove "for update"?
1.  how can i lock the table?
 
 
2.record > 1000 error::::: Inline query has too many rows for direct assignment, use FOR LOOP
 
 
my source:
 

global class B03WebService {

 WebService static boolean updateOpportunityLineItem(){
 pportunityLineItem[] opportunityList = [select Id,s__mitosakuban__c from
                OpportunityLineItem where s__mitosakuban__c =:mitoSakubanBef for update];
        try{
            Database.SaveResult[] SR = database.update(opportunityList);
            for(Database.SaveResult lsr:SR){

                if(!lsr.isSuccess()){

                    updateFlg = false;
                   
                    break;
                }
            }
        }catch (System.DmlException e) {

            throw e;
       
        }catch (System.Exception e) {

            throw e;
       
        } 
       
        return updateFlg;
    }
}
jrotensteinjrotenstein
What are you trying to do? The code you provided did not seem to update anything. Please tell us the purpose of your code and how it is being used. We could then recommend the best way to code it.


Message Edited by jrotenstein on 06-05-2008 04:37 PM
111111
i am sorry ,i for get
 
global class B03WebService {
 WebService static boolean updateOpportunityLineItem(){
  pportunityLineItem[] opportunityList = [select Id,s__mitosakuban__c from
                OpportunityLineItem where s__mitosakuban__c =:mitoSakubanBef for update];
        try{
  for (OpportunityLineItem opportunity : opportunityList) {
                  
                    opportunity.s__mitosakuban__c = 'fff';
                  
                    opportunity.s__delete_flg__c = true;
                } 
            Database.SaveResult[] SR = database.update(opportunityList);
            for(Database.SaveResult lsr:SR){
                if(!lsr.isSuccess()){
                    updateFlg = false;
                   
                    break;
                }
            }
        }catch (System.DmlException e) {
            throw e;
       
        }catch (System.Exception e) {
            throw e;
       
        } 
       
        return updateFlg;
    }
}
jrotensteinjrotenstein
Where does mitoSakubanBef come from? Is it passed into the webservice?

If so, does it mean that whenever you call the webservice, you want to update all Opportunities where s__mitosakuban__c =:mitoSakubanBef and set s__mitosakuban__c = 'fff' and s__delete_flg__c = true ?

Then your problem might be that the SELECT is returning too many rows. Can you think of a way to return less rows? Perhaps change your query to:

[select Id, s__mitosakuban__c from OpportunityLineItem where s__mitosakuban__c =:mitoSakubanBef and (s__mitosakuban__c <> 'fff' or s_deleted_flg__c = false)]

That way, it will only return rows that need updating. That might keep it under 1000 rows.
111111

pportunityLineItem[] opportunityList = [select Id,s__mitosakuban__c from
                OpportunityLineItem where s__mitosakuban__c =:mitoSakubanBef for update];

 
 
why ?(opportunityList 'record > 1000,error[Inline query has too many rows for direct assignment, use FOR LOOP] is appear ?)
 
pportunityLineItem[]  is object,not list.     i think  list 'record > 1000,error is appeared ,but not pportunityLineItem[] .
 
 
 
pportunityLineItem[] opportunityList = [select Id,s__mitosakuban__c from
                OpportunityLineItem where s__mitosakuban__c =:mitoSakubanBef for update];
 
for update is false? and then how can lock the pportunityLineItem[] just as Datebase.pportunityLineItem[] is not table?is not Datebase?
 
 
 
jrotensteinjrotenstein
My error -- I am used to the DML syntax, you are using the Database syntax for which there is "for update". It is interesting that DML does not require such a statement.
111111
thank you for your help
 
can you  help me to make the test method   ,and then make the  ((!lsr.isSuccess()) ==ture)
how can i got the error object to make the   [lsr.isSuccess==false]
 
 
if(!lsr.isSuccess()){
                    updateFlg = false;
                   
                    break;
                }