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
Nathan Prats 22Nathan Prats 22 

Limit Usage for NS

Hello, 

I have this code :
 
List<Account> AccList = [SELECT Id,OwnerId,Parent.OwnerId 
                         FROM Account 
                         WHERE Account_Owner_Parent_Owner__c = TRUE 
                         AND IsExcludedFromRealign = FALSE];

Integer X = [SELECT Count()
             FROM Account 
             WHERE Account_Owner_Parent_Owner__c = TRUE 
             AND IsExcludedFromRealign = FALSE];

do {
    for(Account Acc : AccList){ 
        Acc.OwnerId = Acc.Parent.OwnerId;
    }
        
        } while (X > 0);

update AccList;

but each time i run it, i have a Limit Usage for NS error. 

10:45:16:000 LIMIT_USAGE_FOR_NS   Maximum CPU time: 15118 out of 10000 ******* CLOSE TO LIMIT

What can be wrong ? 
Pradeep SinghPradeep Singh
Hi,
You are not changing the value of X ain your code. You can use IF condition if you want to run on the basis of value of X .
Nathan Prats 22Nathan Prats 22
Hi, 

I want the code to run as long as the query X > 0. 

I guess I created an infinite loop as the query is run only one time ? 

How can I run the code till the query X = 0 ? 
Pradeep SinghPradeep Singh
Hi,
Yes this creates an infinite loop as the value of X is not changing/decreasing. As the query is run once, it gives the value of X which is never changing in the code and using query in loops is not good as it can hit the limits.

Can you please tell me how your value of X is changing ..??
Nathan Prats 22Nathan Prats 22
Thanks for your replies,

Sure, this code it supposed to reassign all accounts to the right owner. 
It uses the account hierarchy. The Account_Owner_Parent_Owner__c  formula field tests if the account owner is different from the parent account owner.
X is basically the number of accounts where the owner is different from the parent account owner.
If I run the code only 1 time, it will reassign 1 node in the account hierarchy. That's why I'm trying to execute the code until X = 0. 
Maharajan CMaharajan C
Hi Nathan,

List<Account> AccList = [SELECT Id,OwnerId,Parent.OwnerId 
                         FROM Account 
                         WHERE Account_Owner_Parent_Owner__c = TRUE 
                         AND IsExcludedFromRealign = FALSE];
    if(AccList.size() > 0)
    {
    for(Account Acc : AccList){ 
        Acc.OwnerId = Acc.Parent.OwnerId;
    }
    
    update AccList
    }
    
Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Raj.
Nathan Prats 22Nathan Prats 22
Hi Raj, 

Your code just reassign 1 node in the account hierarchy. 

The following code reassigns 3 nodes. I might just go with this
 
List<Account> AccList = [SELECT Id,OwnerId,Parent.OwnerId 

             FROM Account 

             WHERE Account_Owner_Parent_Owner__c = TRUE 

             AND IsExcludedFromRealign = FALSE];



  for(Account Acc : AccList){ 

    Acc.OwnerId = Acc.Parent.OwnerId;

  }

update AccList;



List<Account> AccList2 = [SELECT Id,OwnerId,Parent.OwnerId 

             FROM Account 

             WHERE Account_Owner_Parent_Owner__c = TRUE 

             AND IsExcludedFromRealign = FALSE];



  for(Account Acc : AccList2){ 

    Acc.OwnerId = Acc.Parent.OwnerId;

  }

update AccList2;



List<Account> AccList3 = [SELECT Id,OwnerId,Parent.OwnerId 

             FROM Account 

             WHERE Account_Owner_Parent_Owner__c = TRUE 

             AND IsExcludedFromRealign = FALSE];



  for(Account Acc : AccList3){ 

    Acc.OwnerId = Acc.Parent.OwnerId;

  }

update AccList3;

 
Pradeep SinghPradeep Singh
public void methodName(){
	List<Account> AccList = [SELECT Id,OwnerId,Parent.OwnerId FROM Account WHERE Account_Owner_Parent_Owner__c = TRUE AND IsExcludedFromRealign = FALSE];
	if(AccList!= Null && AccList.size()>0){
		for(Account Acc : AccList){ 
			Acc.OwnerId = Acc.Parent.OwnerId; 
		}
		update AccList;
		methodName();
	}
}

Try if this helps.!!