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
LIM AI KHOONLIM AI KHOON 

Query outside the for loop in triggers?

Hi, I get this error:
System.LimitException: Too many SOQL queries: 101

May I know how to improve this code? Maybe to put query outside the trigger.new loop
trigger pullCommentsApproval on Pricing__c (before update) {
    Map<Id, Pricing__c > oppMap = new Map<Id, Pricing__c >([
        Select (Select Comments From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From Pricing__c WHERE Id IN : Trigger.new]);
    
    for(Pricing__c opp: Trigger.new) {
        Pricing__c opp1 = oppMap.get(opp.Id);
        opp.Approver_Comments__c = '';
        for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
            opp.Approved_Date__c = DateTime.parse(system.now().format());
            
        }
        opp.Approver_Comments3__c = 'aa';
    }
}

 
CharuDuttCharuDutt
Hii Lim Ai Khoon
Try Below TRigger
trigger pullCommentsApproval on Pricing__c (before update) {
    Map<Id, string > oppMap = new Map<Id, string >();
    list<ProcessInstanceHistory> lstProcessInstanceHistory = new list<ProcessInstanceHistory>();
    for(Pricing__c opp: Trigger.new) {
        oppMap.put(opp.Id,opp.Id);
        opp.Approver_Comments__c = '';
        opp.Approver_Comments3__c = 'aa';
    }
    list<ProcessSteps> lstPro = [Select Comments,Pricing__c From ProcessSteps where Pricing__c IN :oppMap.keySet() AND StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC];
	for (ProcessInstanceHistory processStep : lstPro) {
            processStep.Approved_Date__c = DateTime.parse(system.now().format());
            lstProcessInstanceHistory.add(processStep);
        }
    if(lstProcessInstanceHistory.size()>0){
        update lstProcessInstanceHistory;
    }
}
Please Mark It As Best Answer If It Helps
Thank You! 
 
LIM AI KHOONLIM AI KHOON
Hi Charudutt,

I tried the given code but hit errors.

Line 9: Invalid type: ProcessSteps
Line 11: Variable does not exist: Approved_Date__c
Line 15: DML operation Update not allowed on List<ProcessInstanceHistory>

 
mukesh guptamukesh gupta
Hi Lim,

I am not getting what excatlly you want to do, and what is meant of below linw


for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {

Please clear this line then after that i will share a perfect solution

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
LIM AI KHOONLIM AI KHOON

Actually, I want to put the approval comment for the pricing in a field so that we can display it inside the report. But When I perform one flow, it give me an error System.LimitException: Too many SOQL queries: 101

trigger pullCommentsApproval on Pricing__c (before update) {
    Map<Id, Pricing__c > oppMap = new Map<Id, Pricing__c >([
        Select (Select Comments From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From Pricing__c WHERE Id IN : Trigger.new]);
    
    for(Pricing__c opp: Trigger.new) {
        Pricing__c opp1 = oppMap.get(opp.Id);
        opp.Approver_Comments__c = '';
        for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
            opp.Approved_Date__c = DateTime.parse(system.now().format());
            opp.Approver_Comments__c += 'Comments: ' + processStep.comments + ' . Status: ' + processStep.StepStatus + ' . Date: ' + processStep.CreatedDate +' . Commentor Name: ' + processStep.Actor.Name + '\\';
        }
        opp.Approver_Comments3__c = 'aa';
    }
}
 
trigger pullCommentsApproval on Pricing__c (before update) {
    Map<Id, Pricing__c > oppMap = new Map<Id, Pricing__c >([
        Select (Select Comments From ProcessSteps where StepStatus IN ('Approved','Rejected') ORDER BY CreatedDate DESC) From Pricing__c WHERE Id IN : Trigger.new]);
    
    for(Pricing__c opp: Trigger.new) {
        Pricing__c opp1 = oppMap.get(opp.Id);
        opp.Approver_Comments__c = '';
        for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
           //Mukesh, you mean I need to clear this part?
        }
        opp.Approver_Comments3__c = 'aa';
    }
}
mukesh guptamukesh gupta
Hi LIM,

Yes ,  


becasue you are decleare variable ProcessSteps of Object type (ProcessInstanceHistory) and using for Object(ProcessSteps)
for (ProcessInstanceHistory processStep : opp1.ProcessSteps) {
           //Mukesh, you mean I need to clear this part?
        }

Please clear it
LIM AI KHOONLIM AI KHOON
Okay, but how I can get value for Approved_Date__c and Approver_Comments__c?