You need to sign in to do that
Don't have an account?
RMish
System.Exception: Too many SOQL queries: 21
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 !!!
you trigger is not bulked, please review this post.
Bulk operations is key to sucess with the platform.