0

I have a method where It updated the records. When I used the below trigger and try to update from account detail page.it is updating as expected. but when try to update using saveme method , I am getting Too may SOQL errors

Help me in either changing saveme() or trigger.

public void saveme() {
    System.debug('In Save Me Method===');
    try {
        Account updatedAcc;
        List<Account> updatedAccList = new List<Account>();
        List<Call_String__c> updatedStringList = new List<Call_String__c>();
        for ( Wrapper wrapObj : ListCallplanWrapper ) {
            updatedAcc = new Account();
            updatedAcc = wrapObj.acc;

            if ( wrapObj.acc.Call_Strings__r!= null && !wrapObj.acc.Call_Strings__r.isEmpty() ) {
                updatedStringList.addAll(wrapObj.acc.Call_Strings__r);
               // updatedAcc = wrapObj.acc;
            }           
            updatedAccList.add(updatedAcc); 
        }

        update updatedStringList;
        update updatedAccList;
    } catch ( Exception e ) {
        System.debug('Exception in Saving records == '+e.getMessage());
    }
    callPlanIndicators();
    getCallplanAccounts();
} 

Here is my trigger:

trigger UpdateCallString on Call_String__C (after update) {

    set<Id> sUserId = new set<Id>();
    map<String,Integer> mapNewCallbyother = new map<String,Integer>();    
    List<Call_String__C> lstCallString = [select id, Account__r.Co_Owner__c, Account__r.Name, Original_CallString__c,Updated_CallString_plist__c from Call_String__C where id IN: trigger.newmap.keyset()];
    for ( Call_String__C cl:lstCallString ) { 
    Call_String__c oldval = Trigger.oldMap.get(cl.Id);
        if ( cl.Account__r.Co_Owner__c != null && cl.Account__r.Co_Owner__c != '' ) {
            String[] arrOwnerid = cl.Account__r.Co_Owner__c.split(';');
            for ( String str: arrOwnerid ) {
                if ( str != null && str != '' ) {
                    sUserId.add(str.trim());                    
                }
            }
        }
        if ( cl.Updated_CallString_plist__c!='No Change' && cl.Updated_CallString_plist__c!='' ) {
            mapNewCallbyother.put(cl.Account__r.Name,integer.valueOf(cl.Updated_CallString_plist__c));
        } else {
            mapNewCallbyother.put(cl.Account__r.Name,integer.valueOf(oldval.Original_CallString__c));
        }
    }
    List<Account> lstAccount = [Select Total_Calls_Formula__c,called_by_other__c , OwnerId, Co_Owner__c, Name from Account where ownerId IN: sUserId and Name in: mapNewCallbyother.keyset()];

    for ( Account acc : lstAccount ) {
        String s='%' + acc.name+'%'; 
        String str = acc.Co_Owner__c; 
        List<String> sArr = str.split(';', 0);

        list<account> acclist = [Select name,OwnerId,Territory_Name__c,Total_Calls_Formula__c,(select Updated_CallString_plist__c,Original_CallString__c from Call_Strings__r) from account where ownerid!= :acc.Ownerid and name like :s and OwnerId in :sArr  ];
        Integer totalothercall = 0;
        for ( Account ac : acclist ) {
            system.debug('#######'+ac.Total_Calls_Formula__c);

            for(Call_String__c cs: ac.Call_Strings__r){
                if(cs.Updated_CallString_plist__c != '' && cs.Updated_CallString_plist__c!='No Change')
                    totalothercall += integer.valueOf(cs.Updated_CallString_plist__c);
                else
                    totalothercall += integer.valueOf(cs.Original_CallString__c);
            }
            //totalothercall += integer.valueOf(ac.Total_Calls_Formula__c);
        }
        acc.called_by_other__c = 0;
        acc.called_by_other__c = totalothercall;// + mapNewCallbyother.get(acc.Name);
    }
    if(lstAccount.size() > 0){
        update lstAccount;
    }
}
3
  • Your trigger is not properly bulkified; you have SOQL inside a for loop. Have you tried moving that SELECT outside of the loops?
    – Mike Chale
    Commented Jul 18, 2013 at 13:03
  • I tried moving Select. I am out of thoughts. Please help me..
    – Arun SFDC
    Commented Jul 18, 2013 at 13:30
  • possible duplicate of System.Exception: Too many SOQL queries: 101 Commented Aug 14, 2014 at 22:32

1 Answer 1

2

You have a SOQL inside the for loop in trigger.

        list<account> acclist = [Select name,OwnerId,Territory_Name__c,Total_Calls_Formula__c,(select Updated_CallString_plist__c,Original_CallString__c from Call_Strings__r) from account where ownerid!= :acc.Ownerid and name like :s and OwnerId in :sArr  ];

remove it from the loop and that should solve the issues

3
  • please help me in removing SOQL inside for loop.
    – Arun SFDC
    Commented Jul 18, 2013 at 13:04
  • 1
    @Arun If you have something inside a loop and you're not sure how to get it out, I'd suggest doing some reading on maps. They're usually the technique to get out of it. Commented Jul 19, 2013 at 0:01
  • @ArunSFDC: Can you explain as to what you're trying to achieve with this trigger, I was having a hard time understanding it? You can edit the question and add the explanation as that will help you get the correct answer..
    – Sam
    Commented Jul 19, 2013 at 4:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .