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
RMishRMish 

System.Exception: Too many SOQL queries

Hi All,

 

 I am new in Apex trigger . I am facing problem with bulk data .

I am using a trigger that takes data from one Custom Object and copy in Account object.

It is working fine only for 5 rows I am trying to import more then 5 rows , its shows 

For Insert data in Custom object , I am using Data Loader.

 

System.Exception: Too many SOQL queries 

 

Pls correct the code ...

 

 

trigger accountand_invoice_update on STG_AR_INVOICE__c (after insert) {
 
  List< Account> aList = new List<Account>(); 
  List<STG_AR_INVOICE__c> bList=new List<STG_AR_INVOICE__c>(); 
  STG_AR_INVOICE__c sgtUpd;
 

  Account b;
 
  for (STG_AR_INVOICE__c stg : Trigger.new ){       
         Account[] acc = [select id,name,company_code__c,bu_code__c from account Where Name =:stg.Customer_Name__c and Company_Code__c =:stg.Company_Code__c and Bu_Code__c =:stg.Bu_Code__c];
         if(acc.size()>=1){
         for(integer i=0;i<acc.size();i++){
          b=acc[i] ;
          b.Credit_Limit__c=stg.Credit_Limit__c;
          b.Credit_Available__c=stg.Credit_Available__c;
          b.Net_Credit_Available__c=stg.Net_Credit_Available__c;
          b.OutStanding_Orders__c=stg.OutStanding_Orders__c;
          b.Out_Standing_Net_AR__c=stg.OUT_STANDING_NET_AR__c;
          b.STATUS__c='OLD';        
          aList.add( b );
         
          STG_AR_INVOICE__C [] stgexist=[SELECT ID,NAME,COMPANY_CODE__c,CUSTOMER_NAME__c,CUSTOMER_KEY__C FROM STG_AR_INVOICE__C
                 WHERE BU_CODE__c =:b.BU_CODE__C AND CUSTOMER_NAME__c =:b.NAME AND COMPANY_CODE__c =:b.COMPANY_CODE__c];
         if(stgexist.size()>=1){       
          for(integer j=0;j<stgexist.size();j++){
             sgtUpd=stgexist[j];            
             sgtUpd.Customer_key__c=b.Id;             
             bList.add(sgtUpd); 
          }
         }
         }
         
         }else{
           b = new Account();
           b.Name =stg.CUSTOMER_NAME__c;
           b.COMPANY_CODE__c=stg.COMPANY_CODE__c;
           b.BU_CODE__c=stg.BU_CODE__c;              
           b.Credit_Limit__c=stg.Credit_Limit__c;
           b.Credit_Available__c=stg.Credit_Available__c;
           b.Net_Credit_Available__c=stg.Net_Credit_Available__c;
           b.OutStanding_Orders__c=stg.OutStanding_Orders__c;
           b.Out_Standing_Net_AR__c=stg.OUT_STANDING_NET_AR__c;
           b.STATUS__C='NEW';
          
           aList.add(b);
         }
    }
     upsert aList; 
     update bList;  

 

 

Thanks 

Best Answer chosen by Admin (Salesforce Developers) 
Erica KuhlErica Kuhl
I am going to move your question to the Developer boards - I have a feeling you will get an answer quicker/better over there.  Good luck!

All Answers

Erica KuhlErica Kuhl
I am going to move your question to the Developer boards - I have a feeling you will get an answer quicker/better over there.  Good luck!
This was selected as the best answer
Vijay RautVijay Raut

Hi,

 

Most of your SOQL statements are inside for loops. Re-organize your code and try to get your SOQL / DML statements outside for loop. As we are having different governor limits in Apex.

 

Also i suggest you to go through Apex Language Reference, where there are different example for better code design.

 

Cheers,

V.R.