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;
    }
}
Christian Deckert
  • 5,219
  • 4
  • 33
  • 68
Arun SFDC
  • 1,458
  • 1
  • 17
  • 38

1 Answers1

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

Sam
  • 4,383
  • 2
  • 25
  • 39
  • please help me in removing SOQL inside for loop. – Arun SFDC Jul 18 '13 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. – Ralph Callaway Jul 19 '13 at 00: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 Jul 19 '13 at 04:11