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
Arun KArun K 

Hittng governor limits

I know I am doing the wrong way as I hittin the governor limits.

But I am not getting how to change the code as it is behaving

 

What needs to be done

here is the code

 

public approval_dynamic_differential()
{
Dynamiclist = new list< Dynamic_territory_differential>();
UserTerritory ustr = [Select TerritoryId ,UserId From UserTerritory where UserId = :UserInfo.getUserId()];

for(Territory terr :[SELECT Id ,name FROM Territory where ParentTerritoryId =:ustr.TerritoryId])
{ 

UserTerritory user = [SELECT Id, UserId, TerritoryId FROM UserTerritory where TerritoryId = :terr.Id];
User userid = [select Id,Name From User Where id = :user.Userid];

TransferAcc = [select id,name,Active__c,Specialization_differential__c,Reason_For_Change__c ,Approval__c,Billingcity,Billingstate,Original_Calls_Total__c,Notes__c from Account WHERE OwnerId=:userid.id and Approval__c='Pending'and Submit__c=True];

UpdateAcc = [select id,name,Active__c,Approval__c,Specialization_differential__c,Billingcity,Billingstate ,(select id,name,Original_CallString__c,Updated_CallString__c,Call_String_Status__c from Call_Strings__r where Call_String_Status__c='Pending') from Account WHERE OwnerId=:userid.id and Approval__c!='Pending' and Submit__c=True];

d1= new Dynamic_territory_differential();
d1.TerritoryId = terr;
d1.userTerrid = user;
d1.username = userid;
d1.Accountslist = TransferAcc;
d1.UpdateAcclist = UpdateAcc ;
Dynamiclist.add(d1);
system.debug('size======='+d1.UpdateAcclist.size());

}

getdynamicdifferential();

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Here's a stab at it... I don't have too much time to optimize it but it should get you in the right direction.

 

I've removed all the SOQL from the loop and instead built a map relative to what logic you were doing in the code. As this was written on the fly it may not compile immediately.

 

public approval_dynamic_differential()
{
    Dynamiclist = new list< Dynamic_territory_differential>();
    UserTerritory ustr = [Select TerritoryId ,UserId From UserTerritory where UserId = :UserInfo.getUserId()];
    
    Map<Id, Territory> terrMap = new Map<Id, Territory>([SELECT Id ,name FROM Territory where ParentTerritoryId =:ustr.TerritoryId]);
    //Map for user territory based off the Id of Territory Id
    //Key -> Territory Id
    //Value -> User Territory for that territory Id
    Map<Id, UserTerritory> userTerritoryMap = new Map<Id, UserTerritory>{};
    //Contains all the User Ids being processed from the user territory map
    Set<Id> userIds = new Set<Id>{};
    //Account Map for the accounts to transfer
    //Key-> Owner Id
    //Value -> Account
    Map<Id, Account[]> accountsToTransfer = new Map<Id, Account>{};
    //Account map for the accounts to update
    //Key -> OwnerId
    //Value -> Account
    Map<Id, Account[]> updatedAccounts = new Map<Id, Account>{};    
    
    for (UserTerritory user = [ SELECT Id, UserId, User.Name, TerritoryId FROM UserTerritory WHERE TerritoryId IN :terrList.keySet())
    {
        userIds.add(user.UserId);
        userTerritoryMap.put(user.TerritoryId, user);
    }
    
    for (Account a : [ select id,name,Active__c,Specialization_differential__c,Reason_For_Change__c ,Approval__c,Billingcity,Billingstate,Original_Calls_Total__c,Notes__c, OwnerId from Account WHERE OwnerId IN :userIds and Approval__c='Pending'and Submit__c=True ])
    {
        Account aList = accountsToTransfer.get(a.OwnerId);
        
        if (aList == null)
        {
            aList = new Account[]{};
            accountsToTransfer.put(a.OwnerId, aList);
        }
        
        aList.add(a);
    }
    
    for (Account a : [select id,name,Active__c,Approval__c,Specialization_differential__c,Billingcity,Billingstate, OwnerId ,(select id,name,Original_CallString__c,Updated_CallString__c,Call_String_Status__c from Call_Strings__r where Call_String_Status__c='Pending') from Account WHERE OwnerId IN :userIds and Approval__c!='Pending' and Submit__c=True])
    {
        Account aList = updatedAccounts.get(a.OwnerId);
        
        if (aList == null)
        {
            aList = new Account[]{};
            updatedAccounts.put(a.OwnerId, aList);
        }
        
        aList.add(a);
    }        
    
    for(Territory terr : terrMap.values())
    {         
        UserTerritory user = userTerritoryMap.get(terr.Id);
        User userid = terr.User;
        
        TransferAcc = accountsToTransfer.get(user.UserId);        
        UpdateAcc = updatedAccounts.get(user.UserId);
        
        d1= new Dynamic_territory_differential();
        d1.TerritoryId = terr;
        d1.userTerrid = user;
        d1.username = userid;
        d1.Accountslist = TransferAcc;
        d1.UpdateAcclist = UpdateAcc ;
        Dynamiclist.add(d1);
        system.debug('size======='+d1.UpdateAcclist.size());
        
    }
    
    getdynamicdifferential();
    
}

 

All Answers

Yoganand GadekarYoganand Gadekar

hi,

What error message do you get?

If it is about 101 SOQL limit, then it is becuase of your select queries within for loop. Implment that logic without having soql select query within for loop.

 

Hit kusdos and marka s answer if this reply helps you.

 

thanks,

 

Arun KArun K

Yes, It is 101 soql stmts error.

 

I want to loop through those soql stms and I couldnt do it.

 

Can u send the update one.

Sean TanSean Tan

Here's a stab at it... I don't have too much time to optimize it but it should get you in the right direction.

 

I've removed all the SOQL from the loop and instead built a map relative to what logic you were doing in the code. As this was written on the fly it may not compile immediately.

 

public approval_dynamic_differential()
{
    Dynamiclist = new list< Dynamic_territory_differential>();
    UserTerritory ustr = [Select TerritoryId ,UserId From UserTerritory where UserId = :UserInfo.getUserId()];
    
    Map<Id, Territory> terrMap = new Map<Id, Territory>([SELECT Id ,name FROM Territory where ParentTerritoryId =:ustr.TerritoryId]);
    //Map for user territory based off the Id of Territory Id
    //Key -> Territory Id
    //Value -> User Territory for that territory Id
    Map<Id, UserTerritory> userTerritoryMap = new Map<Id, UserTerritory>{};
    //Contains all the User Ids being processed from the user territory map
    Set<Id> userIds = new Set<Id>{};
    //Account Map for the accounts to transfer
    //Key-> Owner Id
    //Value -> Account
    Map<Id, Account[]> accountsToTransfer = new Map<Id, Account>{};
    //Account map for the accounts to update
    //Key -> OwnerId
    //Value -> Account
    Map<Id, Account[]> updatedAccounts = new Map<Id, Account>{};    
    
    for (UserTerritory user = [ SELECT Id, UserId, User.Name, TerritoryId FROM UserTerritory WHERE TerritoryId IN :terrList.keySet())
    {
        userIds.add(user.UserId);
        userTerritoryMap.put(user.TerritoryId, user);
    }
    
    for (Account a : [ select id,name,Active__c,Specialization_differential__c,Reason_For_Change__c ,Approval__c,Billingcity,Billingstate,Original_Calls_Total__c,Notes__c, OwnerId from Account WHERE OwnerId IN :userIds and Approval__c='Pending'and Submit__c=True ])
    {
        Account aList = accountsToTransfer.get(a.OwnerId);
        
        if (aList == null)
        {
            aList = new Account[]{};
            accountsToTransfer.put(a.OwnerId, aList);
        }
        
        aList.add(a);
    }
    
    for (Account a : [select id,name,Active__c,Approval__c,Specialization_differential__c,Billingcity,Billingstate, OwnerId ,(select id,name,Original_CallString__c,Updated_CallString__c,Call_String_Status__c from Call_Strings__r where Call_String_Status__c='Pending') from Account WHERE OwnerId IN :userIds and Approval__c!='Pending' and Submit__c=True])
    {
        Account aList = updatedAccounts.get(a.OwnerId);
        
        if (aList == null)
        {
            aList = new Account[]{};
            updatedAccounts.put(a.OwnerId, aList);
        }
        
        aList.add(a);
    }        
    
    for(Territory terr : terrMap.values())
    {         
        UserTerritory user = userTerritoryMap.get(terr.Id);
        User userid = terr.User;
        
        TransferAcc = accountsToTransfer.get(user.UserId);        
        UpdateAcc = updatedAccounts.get(user.UserId);
        
        d1= new Dynamic_territory_differential();
        d1.TerritoryId = terr;
        d1.userTerrid = user;
        d1.username = userid;
        d1.Accountslist = TransferAcc;
        d1.UpdateAcclist = UpdateAcc ;
        Dynamiclist.add(d1);
        system.debug('size======='+d1.UpdateAcclist.size());
        
    }
    
    getdynamicdifferential();
    
}

 

This was selected as the best answer