+ Start a Discussion
NishaCNishaC 

getting Error: Too Many Script Statements-200001

I am getting this error when i am using for loop for two maps i.e ChildIdMap, PickSumMAp.

 

GETTING ERROR IN THIS PART OF CODE

===================================================Part of Code============

for(Id parentId:ChildIdMap.keyset())
{

for(Id ChildId : PickSumMap.keyset()){

if((ChildIdMap.get(parentId))==ChildId)
{

Decimal d=PickSumMap.get(ChildId);
}

}
}

 

 

============================================ 

===========================Full Controller Class===========================

global static CaseBean[] SuccessRate(){

Decimal CountPick;
Map<Id,CaseBean> caseBeanMap=new Map<Id,CaseBean>();
Map<Id,Integer> countMap=new Map<Id,Integer>();
Map<Id,Map<Id,Decimal>> servCollMap=new Map<ID,Map<Id,Decimal>>();
Map<Id,Id> ChildIdMap=new Map<Id,Id>();
Map<Id,Decimal> PickSumMap=new Map<Id,Decimal>();
Map<Id,Account> parentAccountMap=new Map<Id,Account>([SELECT Id,Name from Account where ParentId=null]);
List<Case> caseList=[SELECT Id,CaseNumber, Reason, Status,c.Account.Grand_Parent_Account__c,CreatedDate,ClosedDate
FROM Case c where reason in ('Bags not delivered','Bin not delivered' ,'Bin not removed','Complaint from customer',
'Complaint from the Council','Customer wants to cancel','Delayed Collection','Missed Collection','Missing Bin')
AND c.account.id !=null AND CaseNumber!=null AND ClosedDate = THIS_YEAR];
List<Service__c> servList=[Select id,Count_of_Pickup_Days__c,s.Customer__r.Name,s.Customer__r.Parent.id, s.Customer__r.Grand_Parent_Account__c,
Status__c,s.Customer__r.Active_Account_Services__c from Service__c s
where s.Customer__r.id!=null AND Count_of_Pickup_Days__c!=null
AND Status__c = 'Active'];

for(Case caseObj:caseList){
ID parentAccountId=caseObj.Account.Grand_Parent_Account__c;
if(countMap.get(parentAccountId)==null)
{
countMap.put(parentAccountId,1);
}
else
{
integer cnt=countMap.get(parentAccountId);
countMap.put(parentAccountId,cnt+1);
}
}
for(Service__c serObj: servList){
ID AccntId=serObj.Customer__r.id;
ID parentAccntId=serObj.Customer__r.Grand_Parent_Account__c;
if(serObj.Status__c == 'Active'){
if(PickSumMap.get(AccntId)==null)
{

PickSumMap.put(AccntId,serObj.Count_of_Pickup_Days__c);




}
else
{
PickSumMap.put(AccntId,serObj.Count_of_Pickup_Days__c);



}
if(ChildIdMap.get(AccntId)==null)
{
ChildIdMap.put(parentAccntId,AccntId);

}
else
{
ChildIdMap.put(parentAccntId,AccntId);

}
}

}

for(Id parentId:ChildIdMap.keyset())
{

for(Id ChildId : PickSumMap.keyset()){

if((ChildIdMap.get(parentId))==ChildId)
{

Decimal d=PickSumMap.get(ChildId);
}

}
}
CaseBean[] beanList=new CaseBean[100000];

}
//System.Debug('---BeanList---'+beanList);
return beanList;
}

 

 

i just want to show it according to parentId having Child records and want count of those child records

 

Need It Urgently

HariDineshHariDinesh

Hi,

 

Too many script statements:  means that your code has executed 200001 lines of apex in one Invocation.

I can explain you simply like this

For example in button click called method1 and in which there is call for mehod2.

Now your controller will start at method1 and goes to method2 and again came to end of method1.

This entire called one invocation.

 

Here in your case one of your invocations executes 200001 lines of code.

By the time the controller reaches “for(Id parentId:ChildIdMap.keyset())” loop your invocation consumes 200001 lines of code.(might be) OR it might consuming.

So in normal scenarios the code will not be that much huge.so check is there any where your code reaching Infinite loop, which causes this problem.

And I can suggest there is one way to debug for this.

 

Find below link

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_limits.htm

 

We have System Methods

 

Integer myscriptLimit = Limits.getScriptStatements();
Use like system.debug(myscriptLimit);

 

This will give the consumption of script statements at runtime.

Use it in different places and try to find where the loop going abnormally.

 

Please let me know update on this

NishaCNishaC

hii,

      thanks for ur reply.... i have used this system method and get to know about the system limit

      but i dont know how to get rid off this problem...i have more than 10000 records and if i comparing them then it gives this error.... any idea if u have for comparison...

 

i have parent id with child id and corresponding to that i have count of pick days field.....i want to calculate count of pick days for all the ids for parent and child.

 

if i have parent id then according to that id i have associate child records and their count of pick days for both parent and child

 

i want to calculate those and sum of all those pick days of parent and child ids...

 

if u have any ques plz let me know

HariDineshHariDinesh

Hi,

 

Use system methods before and after for loop what you described.

If you can confirm what is for loop causing the Issue then we can do something related to that.

 

Use List, Set or Map to get the all ids what you want to compare and the try to loop using Contains or IN key words.

This defiantly reduces the script statements.

Once you are clear on where is the issue try to solve that by using above process.

If you need any idea/Help related to that logic please post your code related to that here.

 

Really I didn’t went deep into your code for now.

NishaCNishaC

i am telling u the hierarchy......... i am using 2 maps

 

1. For getting child id according to their parent id

Parent Id----------> ChildID

 

2. getting CountOfPickDays according to child ID

ChildID-------------------> Count Of PickDays

 

Count OF Pick Days            AccountName(CHild)        Account ID                     Parent Name

                 5                               caffe Nero                    0012000000ZnBRA                Caffe Nero Group Ltd

                 2                               Caffe Nero                   0012000000ZmvIr                   Caffe Nero Group Ltd

Total = 5+2 = 7 records

Now I have used these loops

for(Id parentId:ParentIdMap.keyset())
{

//System.debug('-----parentId----'+parentId);
for(Id childId:PickSumMap.keyset())
{
//here i am Getting Error : Too Many Script Statements:200001 

Because  i have more than 10000 records in PickSumMap 

 

i want to compare these child Id from both the maps and want to get count of Pick Days According to parent ID associated with child records like i want to get all the count of pick Days of Child records of Caffe Nero having parent Caffe Nero Group Ltd
if((ParentIdMap.get(parentId))==childId)
{

}

 

thanks for your help

     

anshul9887anshul9887

You Can Use Wrapper class For Binding Parent id,child id and Count Of PickDays.

 

Public class wrapper{

     public id ParentId{get;set;}   //get set mandatory when you r using this wrapper class attributes on VF Page

       

       public wrapper(Id ParentId,Id ChildId,Id Count){

                 this.ParentId=ParentId;

                 this.ChildId=ChildId;

                 this.Count=Count;

 

         }

 

}

 

 

 

//****************************************************

Now u can declare a wrapper list like below

Public List <wrapper> wrapperList=new List <wrapper>();

 

 

Public void <controllermethodname> (){

     wrapperList.add(new wrapper(Parentidvalue,childidvalue,countvalue));     

 

 

}

 

 

Then i m updating your error part of code

GETTING ERROR IN THIS PART OF CODE

===================================================Part of Code============

/*for(Id parentId:ChildIdMap.keyset())
{

for(Id ChildId : PickSumMap.keyset()){

if((ChildIdMap.get(parentId))==ChildId)
{

Decimal d=PickSumMap.get(ChildId);
}

}
}

*/

 

//As Now we know that count are already bound to those childs who r connected with hteir parents

//So   We can directly fetch Count Of PickDayst value as below:-

for(wrapper wrapObj:wrapperList){

         Decimal d=wrapobj.count;

 

}

 

Thats all.If after that you face any problem revert me back.............................:)

 

 

 

 

 

 

============================================ 

 

 

 

 

 

NishaCNishaC

thanks for ur reply anshul.....

 i am sending u rest of the code after Bean List

 

global class CaseFailBean{
public String accountName{get;set;}
public Decimal totalCases{get;set;}
public Decimal totalServiceColl{get;set;}

public CaseFailBean(String name,Decimal SuccessRate,Decimal FailureRate){
this.accountName=name;
this.totalCases=SuccessRate;
this.totalServiceColl=FailureRate;
}

public CaseFailBean(){

}
}

 

 

now tell me how to resolve it

_Prasu__Prasu_

You will need to use the MAP more efficiently. Why are you not creating MAP of MAP like structure or as suggested above use the wrapper class and push them in MAP with Parent Id as key.

NishaCNishaC

thanks Prasanna,

                                  i  tried to use map of map before

Map<Id,Map<Id,Decimal>> servCollMap=new Map<ID,Map<Id,Decimal>>();

Map<Id,Decimal> PickSumMap=new Map<Id,Decimal>(); 

 

if(PickSumMap.get(AccntId)==null)
{
PickSumMap.put(AccntId,serObj.Count_of_Pickup_Days__c);
}
else
{
PickSumMap.put(AccntId,serObj.Count_of_Pickup_Days__c);
}
if(servCollMap.get(parentAccntId)==null)
{
servCollMap.put(parentAccntId,PickSumMap);
system.debug('===========servCollMap========'+servCollMap);
}
else
{
servCollMap.put(parentAccntId,PickSumMap);
system.debug('===========servCollMap========'+servCollMap);
}

 

 

//the problem which i am getting is picksummap result is not coming according to its parent id.. it is just coming as it is in system.debug of servCollMap........how can i get Child Id according to its parent Id 

 

 

=========PickSumMap========{0012000000WeyClAAJ=1, 0012000000XIyLmAAL=1, 0012000000Zm8hJAAR=1, 0012000000Zm8heAAB=5, 0012000000Zm8hfAAB=5, 0012000000Zm8hhAAB=5, 0012000000Zm8hlAAB=2, 0012000000Zm8hnAAB=5, 0012000000Zm8hoAAB=5, 0012000000Zm8hqAAB=5, ...}

 

======servCollMap========{0012000000WekRxAAJ={0012000000WeyClAAJ=1, 0012000000XIyLmAAL=1, 0012000000Zm8hJAAR=1, 0012000000Zm8heAAB=5, 0012000000Zm8hfAAB=5, 0012000000Zm8hhAAB=5, 0012000000Zm8hlAAB=2, 0012000000Zm8hnAAB=5, 0012000000Zm8hoAAB=5, 0012000000Zm8hqAAB=5, ...}
=====servCollMap========{0012000000WekRxAAJ

this is the parent ID but not associated with its child records....

Any idea for this

HariDineshHariDinesh

Hi,


As per your words you have more than 10000 records in PickSumMap
So to avoid looping multiple times I can suggest you below technique.


Replace below code:

for(Id parentId:ChildIdMap.keyset())
{

for(Id ChildId : PickSumMap.keyset())
{

if((ChildIdMap.get(parentId))==ChildId)
{

Decimal d=PickSumMap.get(ChildId);
}

}
}

 

With Below

 

for(Id parentId:ChildIdMap.keyset())
{
 id newchldid= ChildIdMap.get(parentId);
 if(PickSumMap.containsKey(newchldid))
 {
   Decimal d=PickSumMap.get(newchldid);
  }
}

 

This will avoid looping of  "for(Id ChildId : PickSumMap.keyset())" loop (which having 10000 records) 1000 times.

All script statements will be reduced.
Follow the above technique and update your code where ever you feel like looping multiple times.

 

Let me know if you face any problems while implementing this

.