You need to sign in to do that
Don't have an account?
gb.apex
caused by: System.LimitException: Too many SOQL queries: 201
Hi All,
its too urgent, am written a batch class for populate the approval comment field in order object. now i am facing this error can u any one please help me to avoid governerlimits..thank alot in advance
global void execute(Database.BatchableContext BC,List<Order_vod__c> scope)
{
List<Order_vod__c> updOrdlist = new List<Order_vod__c>();
//for(Order_vod__c order : scope)
//{
// updOrdlist = createOrdCmnts(scope);
//}
for(Order_vod__c o2:scope)
{
ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid = : o2.id limit 1];
system.debug('1--------------Order ID -------------->'+ o2.id);
system.debug('2-------------Process Instance -------------->'+ op);
for (ProcessInstance op1 : op)
{
for (ProcessInstanceStep pis : op1.Steps)
{
system.debug('4-------------Process Instance Steps-------------->'+ pis);
if(op1.Status == 'Approved')
{
o2.PFE_IT_Approval_Comment__c = pis.Comments;
system.debug('3-------------o2.PFE_IT_Approval_Comment__c-------------->'+ o2.PFE_IT_Approval_Comment__c);
}
}
}
updOrdlist.add(o2);
}
system.debug('5-------------updOrdlist-------------->'+ updOrdlist);
update updOrdlist;
}
its too urgent, am written a batch class for populate the approval comment field in order object. now i am facing this error can u any one please help me to avoid governerlimits..thank alot in advance
global void execute(Database.BatchableContext BC,List<Order_vod__c> scope)
{
List<Order_vod__c> updOrdlist = new List<Order_vod__c>();
//for(Order_vod__c order : scope)
//{
// updOrdlist = createOrdCmnts(scope);
//}
for(Order_vod__c o2:scope)
{
ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid = : o2.id limit 1];
system.debug('1--------------Order ID -------------->'+ o2.id);
system.debug('2-------------Process Instance -------------->'+ op);
for (ProcessInstance op1 : op)
{
for (ProcessInstanceStep pis : op1.Steps)
{
system.debug('4-------------Process Instance Steps-------------->'+ pis);
if(op1.Status == 'Approved')
{
o2.PFE_IT_Approval_Comment__c = pis.Comments;
system.debug('3-------------o2.PFE_IT_Approval_Comment__c-------------->'+ o2.PFE_IT_Approval_Comment__c);
}
}
}
updOrdlist.add(o2);
}
system.debug('5-------------updOrdlist-------------->'+ updOrdlist);
update updOrdlist;
}
for(Order_vod__c o2:scope)
{
ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid = : o2.id limit 1];
try something similar to this:
Set<ID> setscopeIDs = new Set<ID>();
for(Order_vod__c sc: scope)
{
setscopeIDs.add(sc.Id);
}
for(Order_vod__c O2 : [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid in =: setscopeIDs ])
{FURTHER PROCESSING}
It is not a good practice to write query inside for loop. Instead of this you can use collection and query one time for all the orders.
Here is the code modified code:
global void execute(Database.BatchableContext BC,List<Order_vod__c> scope)
{
List<Order_vod__c> updOrdlist = new List<Order_vod__c>();
Set<Id> orderIds = new Set<Id>();
for(Order_vod__c order : scope)
{
orderIds.add(order.id);
}
ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid in: orderIds];
Map<Id,ProcessInstance> mapOfOrderProcessInstance = new Map<Id, ProcessInstance>();
for(ProcessInstance p: op){
mapOfOrderProcessInstance.put(p.targetObjectId, p);
}
for(Order_vod__c o2:scope)
{
system.debug('1--------------Order ID -------------->'+ o2.id);
if(mapOfOrderProcessInstance.containsKey(o2.Id)){
ProcessInstance op1 = mapOfOrderProcessInstance.get(o2.Id);
system.debug('2-------------Process Instance -------------->'+ op1);
for (ProcessInstanceStep pis : op1.Steps)
{
system.debug('4-------------Process Instance Steps-------------->'+ pis);
if(op1.Status == 'Approved')
{
o2.PFE_IT_Approval_Comment__c = pis.Comments;
system.debug('3-------------o2.PFE_IT_Approval_Comment__c-------------->'+ o2.PFE_IT_Approval_Comment__c);
}
}
}
updOrdlist.add(o2);
}
system.debug('5-------------updOrdlist-------------->'+ updOrdlist);
update updOrdlist;
}
Please share what error you are getting.