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
Tatiana Cooke 9Tatiana Cooke 9 

Error: System.LimitException: Too many SOQL queries: 101 salesforce on line 129

Can someone help me fix this. I am trying to deploy code into production but am getting the above error on the below trigger. 

I am new to development and this is something written by an outsourced developer. 

Appreciate any help!
Trigger WE_PersonAccount_Trigger on Account (after insert,after update) 
{

    if(Trigger.isAfter && Trigger.isInsert)
    {
       List <WE_Process__c> weProcessList  = [SELECT Id, Name, WE_Process_Sample_Data__c, WE_Batch_Process__c, WE_Account_Batch_Process__c FROM WE_Process__c];
       List<Wealth_Engine_Setting__c> wealthEngineSettingList = [SELECT Id, Name, APIKey__c, AddressURL__c, PhoneURL__c, EmailURL__c FROM Wealth_Engine_Setting__c];
        if(WE_RecusionWrapper.runPersonAccount ==true && weProcessList[0].WE_Account_Batch_Process__c  == false &&  wealthEngineSettingList.size() > 0)
        {
             WE_RecusionWrapper.runPersonAccount=false;
             List<Sobject> sobjListData = new List<Sobject>();
             List<Sobject> updateList= new List<Sobject> ();
             Set<String> accIdSet = new Set<String>();
             
             List<Account> accList = [SELECT Id, Name, LastName, FirstName, Salutation, Type, RecordTypeId, ParentId, BillingStreet, BillingCity, BillingState, BillingPostalCode, BillingCountry, BillingStateCode, BillingCountryCode, ShippingStreet, ShippingCity, ShippingState, ShippingPostalCode, ShippingCountry, ShippingStateCode, ShippingCountryCode, Phone, Fax, Website, Sic, Industry, PersonContactId, IsPersonAccount, PersonMailingStreet, PersonMailingCity, PersonMailingState, PersonMailingPostalCode, PersonMailingCountry, PersonMailingStateCode, PersonMailingCountryCode, PersonOtherStreet, PersonOtherCity, PersonOtherState, PersonOtherPostalCode, PersonOtherCountry, PersonOtherStateCode, PersonOtherCountryCode, PersonMobilePhone, PersonHomePhone, PersonOtherPhone, PersonAssistantPhone, PersonEmail, PersonTitle, PersonDepartment, PersonAssistantName, PersonLeadSource, PersonBirthdate, PAContactId__c, StatusCode__c, WE_Enhance__c, SSN__pc, Middle_Name__c, Gift_Capacity__pc, WE_ID__pc, WE_Age__pc, WE_Gender__pc, WE_First_Name__pc, WE_Last_Name__pc, WE_Full_Name__pc, WE_Emails__pc, WE_Has_Children__pc, WE_Spouse_First_Name__pc, WE_Spouse_Last_Name__pc, WE_Cash_on_Hand_Max_Score__pc, WE_Cash_on_Hand_Score__pc, WE_Cash_On_Hand__pc, WE_Networth_Max_Score__pc, WE_Networth_Score__pc, WE_Networth__pc, WE_Total_Income_Max_Score__pc, WE_Total_Income_Score__pc, WE_Total_Income__pc, WE_Business_Ownership_Max_Score__pc, WE_Business_Ownership_Score__pc, WE_Business_Ownership__pc, WE_Business_Sales_Volume_Max_Score__pc, WE_Business_Sales_Volume_Score__pc, WE_Business_Sales_Volume__pc, WE_Accredited_Investor__pc, WE_Total_Stock_Max_Score__pc, WE_Total_Stock_Score__pc, WE_Total_Stock__pc, WE_Direct_Stock_Holdings_Max_Score__pc, WE_Direct_Stock_Holdings_Score__pc, WE_Direct_Stock_Holdings__pc, WE_Indirect_Stock_Holdings_Max_Score__pc, WE_Indirect_Stock_Holdings_Score__pc, WE_Indirect_Stock_Holdings__pc, WE_Investable_Assets_Max_Score__pc, WE_Investable_Assets_Score__pc, WE_Investable_Assets__pc, WE_Total_Assets_Max_Score__pc, WE_Total_Assets_Score__pc, WE_Total_Assets__pc, WE_Total_Pensions_Max_Score__pc, WE_Total_Pensions_Score__pc, WE_Total_Pensions__pc, WE_Affiliation_Inclination__pc, WE_Affiliation_Inclination_Score__pc, WE_Planned_Giving__pc, WE_Influence_Rating__pc, WE_Influence_Rating_Value__pc, WE_P2G_Score__pc, WE_P2G__pc, WE_Gifting_Capacity_Max_Score__pc, WE_Gifting_Capacity_Score__pc, WE_Gifting_Capacity__pc, WE_Charitable_Donations_Max_Score__pc, WE_Charitable_Donations_Score__pc, WE_Charitable_Donations__pc, WE_Total_Political_Donations_Max_Score__pc, WE_Total_Political_Donations_Score__pc, WE_Total_Political_Donations__pc, WE_Estimated_Annual_Donations_Max_Score__pc, WE_Estimated_Annual_Donations_Score__pc, WE_Estimated_Annual_Donations__pc, WE_Locations__pc, WE_Num_Properties_Owned__pc, WE_Total_Real_Estate_Max_Score__pc, WE_Total_Real_Estate_Score__pc, WE_Total_Real_Estate__pc, WE_Vehicle__pc, WE_Vehicle_Value__pc, WE_Jobs__pc, WE_Last_Search__pc, WE_Last_Update__pc, WE_Full_Profile_Details__pc, WE_Marital_Status__pc, WE_Search__pc, WE_Update__pc FROM Account where Id  IN: Trigger.newMap.KeySet()];
             for(Account objAccount : accList)
             {
                if((objAccount.BillingStreet != null && objAccount.BillingCity != null && objAccount.BillingState != null && objAccount.BillingStateCode != null && objAccount.BillingPostalCode != null && objAccount.LastName != null && objAccount.FirstName != null) || (objAccount.PersonEmail != null) || (objAccount.phone != null) || (objAccount.PersonMobilePhone != null) || (objAccount.PersonHomePhone != null) && (objAccount.WE_Enhance__c)) 
                 {
                  { 
                  if(objAccount.IsPersonAccount)
                    accIdSet.add(objAccount.Id);
                  }
                }
             }
             if(accIdSet.size() > 0 )
             {
                 if(accIdSet.size() >= 5 )
                 {
                     WE_PA_NewlyImportedRecordForUpdate_Batch b= new WE_PA_NewlyImportedRecordForUpdate_Batch(accIdSet,'account',null,'Trigger','Insert');
                     Database.executeBatch(b,5);
                 }
                 else
                 {
                     WE_UtilCommon.processAndUpdateWealthEngineDataForTrigger(accIdSet,'account','Trigger');
                 }
                 
             }
             
             
        }
    }
    
    else if(Trigger.isAfter && Trigger.isUpdate )
    {
        System.debug('*****Inside WE Account Update Trigger****');
        System.debug('*****WE_RecusionWrapper.runPersonAccount****'+WE_RecusionWrapper.runPersonAccount);
        List<Wealth_Engine_Setting__c> wealthEngineSettingList = [SELECT Id, Name, APIKey__c, AddressURL__c, PhoneURL__c, EmailURL__c FROM Wealth_Engine_Setting__c];
        List <WE_Process__c> weProcessList  = [SELECT Id, Name, WE_Process_Sample_Data__c,WE_On_Demand_Process__c, WE_Batch_Process__c, WE_Account_Batch_Process__c FROM WE_Process__c];
        System.debug('*******WE_RecusionWrapper.runPersonAccount*******'+WE_RecusionWrapper.runPersonAccount);
        if( WE_RecusionWrapper.runPersonAccount=true && weProcessList[0].WE_Account_Batch_Process__c  == false && weProcessList[0].WE_On_Demand_Process__c == false && wealthEngineSettingList.size() > 0)
        {
             WE_RecusionWrapper.runPersonAccount=false;
             List<Sobject> sobjListData = new List<Sobject>();
             List<Sobject> updateList= new List<Sobject> ();
             Set<String> accIdSet = new Set<String>();
             Map<String,String>  valueFiledUpdateListMap = new Map<String,String>();
             for(Account acc : Trigger.New)
             {
                if(acc.IsPersonAccount)
                {
                  if((acc.BillingStreet != null && acc.BillingCity != null && acc.BillingState != null && acc.BillingStateCode != null && acc.BillingPostalCode != null && acc.LastName != null && acc.FirstName != null) || (acc.PersonEmail != null) || (acc.phone != null) || (acc.PersonMobilePhone != null) || (acc.PersonHomePhone != null) && (acc.WE_Enhance__c))
                  {
                     List<Integer> changedIndexFieldList = new List<Integer>();
                     Account oldAccount =  Trigger.OldMap.get(acc.Id);
                     String item = '';
                     if((acc.BillingStreet  != oldAccount.BillingStreet) || (acc.BillingCity != oldAccount.BillingCity ) ||(acc.BillingState != oldAccount.BillingState) || (acc.BillingStateCode != oldAccount.BillingStateCode ) || (acc.BillingPostalCode != oldAccount.BillingPostalCode ) || (acc.LastName != oldAccount.LastName) || (acc.FirstName != oldAccount.FirstName))
                     {
                         changedIndexFieldList.add(1);
                         accIdSet.add(acc.Id);
                         item = item + '1,';
                     }
                    if(acc.PersonEmail != oldAccount.PersonEmail)
                    {
                        changedIndexFieldList.add(2);
                        accIdSet.add(acc.Id);
                        item = item + '2,';
                    }
                    if(acc.phone != oldAccount.phone)
                    {
                        changedIndexFieldList.add(3);
                        accIdSet.add(acc.Id);
                        item = item + '3,';
                    }
                    if(acc.PersonMobilePhone != oldAccount.PersonMobilePhone)
                    {
                        changedIndexFieldList.add(4);
                        accIdSet.add(acc.Id);
                        item = item + '4,';
                    }
                    if(acc.PersonHomePhone != oldAccount.PersonHomePhone)
                    {
                        changedIndexFieldList.add(5);
                        accIdSet.add(acc.Id);
                        item = item + '5,';
                    } 
                    if(item != '')
                    {
                         if(item.contains(','))
                         {
                              item = item.removeEnd(',');
                         }
                        valueFiledUpdateListMap.put(acc.Id,item);
                    }
                 }
                }
                 
                 
             }
             System.debug('*****accIdSet****'+accIdSet);
             System.debug('*****valueFiledUpdateListMap****'+valueFiledUpdateListMap);
             if(accIdSet.size() > 0 && valueFiledUpdateListMap != null)
             {
                 if(accIdSet.size() > =5  )
                 {
                      WE_PA_NewlyImportedRecordForUpdate_Batch b= new WE_PA_NewlyImportedRecordForUpdate_Batch(accIdSet,'account',valueFiledUpdateListMap,'Trigger','Update');
                      Database.executeBatch(b,5);
                 }
                 else
                 {
                    WE_UtilCommon.ProcessAndUpdateWealthEngineDataDuringUpdateTrigger(accIdSet,'account',valueFiledUpdateListMap,'Trigger'); 
                 }
                
                //WE_UtilCommon.ProcessAndUpdateWealthEngineDataDuringUpdateTrigger(accIdSet,'account',valueFiledUpdateListMap,'Trigger');
             }
        }
        else
        {
            weProcessList  = [SELECT Id, Name, WE_Process_Sample_Data__c,WE_On_Demand_Process__c, WE_Batch_Process__c, WE_Account_Batch_Process__c FROM WE_Process__c];
            weProcessList[0].WE_On_Demand_Process__c = false;
            update weProcessList;
        }
        
    }
}

 
Mahesh DMahesh D
Hi Tatiana,

Please paste the below code:

WE_UtilCommon.ProcessAndUpdateWealthEngineDataDuringUpdateTrigger() method here so that it will be east to troubleshoot.

Regards,
Mahesh