• RMish
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies

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 

  • March 16, 2009
  • Like
  • 0

I am usinf after insert Trigger.

 

 

trigger import_into_stg on STG_AR_INVOICE__c (after insert) {

// Create new Customer
List <Account> toBeInserted = new List<Account>();

// Instantiate a list of Accounts to update
List< Account> aList = new List<Account>();
List<Account> allAcc= new List<Account>();

util t=new util();

// Instantiate a list of STG_AR_INVOICE to populate Customer Key
List< STG_AR_INVOICE__C > stgList = new List< STG_AR_INVOICE__C >();
List< STG_AR_INVOICE__C > updatestg = new List< STG_AR_INVOICE__C >();

Boolean updateFlage;

for ( STG_AR_INVOICE__c stg : Trigger.new ){

Account[] a=[SELECT NAME,COMPANY_CODE__c,BU_CODE__C,SOURCE_CUSTOMER_ID__c FROM ACCOUNT
WHERE BU_CODE__c =:stg.BU_CODE__C AND NAME =:stg.CUSTOMER_NAME__c AND COMPANY_CODE__c =:stg.COMPANY_CODE__c];

Account b;
IF(a.size()>=1){
for(integer i=0;i<a.size();i++){
b=a[i];
}
b.NAME =stg.CUSTOMER_NAME__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;
aList.add( b );

// Update the Account
if(aList.size()>=1){
update aList;
updateFlage = TRUE;
}

//Back in Staging table and populate record key
if(updateFlage){
STG_AR_INVOICE__C [] stgexist=[SELECT ID,NAME,COMPANY_CODE__c,BU_CODE__C,SOURCE_CUSTOMER_ID__c,CUSTOMER_KEY__C FROM STG_AR_INVOICE__C
WHERE BU_CODE__c =:stg.BU_CODE__C AND SOURCE_CUSTOMER_ID__c =:stg.SOURCE_CUSTOMER_ID__c AND COMPANY_CODE__c =:stg.COMPANY_CODE__c];
STG_AR_INVOICE__C stgupdate;
if(stgexist.size()>0){
for(integer i=0;i<stgexist.size();i++){
stgupdate=stgexist[i];
}
stgupdate.CUSTOMER_KEY__C=b.Id;
stgList.add(stgupdate);


}
}
}else{
updateFlage = false;
// loop through trigger records
//System.debug('In Trigger Create Customer');

Account CreateAccnt = new Account();
CreateAccnt.Name =stg.CUSTOMER_NAME__c;
CreateAccnt.COMPANY_CODE__c=stg.COMPANY_CODE__c;
CreateAccnt.BU_CODE__c=stg.BU_CODE__c;

CreateAccnt.Credit_Limit__c=stg.Credit_Limit__c;
CreateAccnt.Credit_Available__c=stg.Credit_Available__c;
CreateAccnt.Net_Credit_Available__c=stg.Net_Credit_Available__c;
CreateAccnt.OutStanding_Orders__c=stg.OutStanding_Orders__c;
CreateAccnt.Out_Standing_Net_AR__c=stg.OUT_STANDING_NET_AR__c;

toBeInserted.add(CreateAccnt);



// Update Staging table

String accId=t.getAccountId(stg.BU_CODE__C,stg.Company_Code__c,stg.Customer_Name__c);

if(accId !=null){
STG_AR_INVOICE__C [] updstg=[SELECT ID,NAME,COMPANY_CODE__c,BU_CODE__C,SOURCE_CUSTOMER_ID__c,CUSTOMER_KEY__C FROM STG_AR_INVOICE__C
WHERE BU_CODE__c =:stg.BU_CODE__C AND SOURCE_CUSTOMER_ID__c =:stg.SOURCE_CUSTOMER_ID__c AND COMPANY_CODE__c =:stg.COMPANY_CODE__c];
STG_AR_INVOICE__C stgupdate;
if(updstg.size()>0){
for(integer i=0;i<updstg.size();i++){
stgupdate=updstg[i];
}
stgupdate.CUSTOMER_KEY__C=b.Id;
updatestg.add(stgupdate);

}
}
}


}

if(stgList.size()>0){
update stgList;
}
if(toBeInserted.size()>0){
insert toBeInserted;
updateFlage =true;
}

if(updatestg.size()>0 && updateFlage){
update updatestg;
}
updateFlage =false;
}

 

 

Apex Class 

 

 

public class util{

public List<Account> copyCustomersFromERP(String bu_code,String company_code,String customer_name,Double credit_limit){

// Check First Customer exist or not.
//private isCustomerExist=false;
List <Account> toBeInserted = new list<Account>();
/*
String bu_code='100';
String company_code='FMA';
String source_customer_id='DELL';
*/
/*
Integer count=[SELECT Count() FROM Account act
Where act.BU_CODE__c=: bu_code AND act.COMPANY_CODE__c=:company_code AND act.NAME =:customer_name];
*/


//IF(count <= 0){
// loop through trigger records
//for (Integer i=0; i<Trigger.new.size(); i++){
Account CreateAccnt = new Account();
CreateAccnt.Name = customer_name;
CreateAccnt.COMPANY_CODE__c= company_code;
CreateAccnt.BU_CODE__c = bu_code;
CreateAccnt.Credit_Limit__c=credit_limit;
toBeInserted.add(CreateAccnt);
// }

//}
return toBeInserted;
}


}

 

 

 

 

 I am new in Apex trigger . 

 

When I am Inserting bulk data then it is showing Exception

System.Exception: Too many SOQL queries: 21 

Please any Body Suggest me how can I do. Please correct my code.

 

Please also describe me how "after trigger" works. I have confusion that whether it work row by row or after complete all insertion.

 

 

 

 

Thanks in Advance !!!

 

 

 

 

  • March 14, 2009
  • Like
  • 0