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
Samuel Gonsalves 8Samuel Gonsalves 8 

Opportunity Trigger Error "Apex script unhandled trigger exception by user/organization"

Hi All,

I was updating some opportunty data through data loader but got plenty of same error
Error :
Apex script unhandled trigger exception by user/organization: 00590000002bdRR/
00D90000000siXo
OptyStatus: System.LimitException: quotesync:Too many SOQL queries: 101

"Optystatus" is the trigger which is been created in our org

This trigger is used to update all the quote status to "Rejected" when the associated Opportunity Stage is changed to "Closed Lost". trigger OptyStatus on Opportunity (after update)  { try { Set<String> Opty=new Set<String>(); for(Opportunity Opp : Trigger.new) {     if(Opp.StageName=='Closed Lost')     {     Opty.add(Opp.Id);     List<Quote> Qu = [SELECT Id, Name FROM Quote WHERE OpportunityId in: Opty];     if(Qu.size()>0)         {             for(Quote qut : Qu)                 {                 qut.Status='Rejected';                 }                       try                 {                 update Qu;                 }// try ends                 catch (DMLException e)                 {                 System.debug('An unexpected error has occurred while updating Quote: ' + e.getMessage());                 }        }             } } }//try ends catch(Exception ex) { System.debug('An unexpected error has occurred: ' + ex.getMessage()); } }

Can anyone help me out to remove this error.

Thanks in advance.

Regards,
Samuel

 
ShashankShashank (Salesforce Developers) 
You have a SOQL query within a FOR loop which is causing this. As per APEX best practices, your code needs to be bulkified. Here's how: https://developer.salesforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops
 
Samuel Gonsalves 8Samuel Gonsalves 8
Hi Shashank,

I am new to this so can you update the code and provide it to me. It would really be a great help. Also the process how i can update it in production.
ShashankShashank (Salesforce Developers) 
If you are not comfortable with bulkifying the code, it is highly recommended to work with a salesforce developer to get this done for you.
Samuel Gonsalves 8Samuel Gonsalves 8
Hey Shashank,

I am a salesforce administrator of the company and has very basic knowlege of development. There is no developer so need to do by myself. Can you please help me out with it.

Thanks
Bhanu MaheshBhanu Mahesh
Hi Samuel,

As Shashank suggested, you need to bulkify your code and follow the APEX best practices. 

Following is the code for above functionality. 

trigger OptyStatus on Opportunity (after update)  {
    Set<Id> Opty = new Set<Id>();
    List<Quote> lstQuteToUpdate = new List<Quote>();
    for(Opportunity Opp : Trigger.new) {
        if(Opp.StageName == 'Closed Lost' && Opp.StageName != trigger.OldMap.get(Opp.Id).StageName){
            Opty.add(Opp.Id);    
        }
    }
    if(!Opty.isEmpty()){
        for(Quote qute :[SELECT Id, Name,Status FROM Quote WHERE OpportunityId IN: Opty]){
            if(qute.Status != 'Rejected'){
                qute.Status = 'Rejected';
                lstQuteToUpdate.add(qute);
            }
        }
    }
    if(lstQuteToUpdate.size() > 0){
        try{
            update lstQuteToUpdate;
        }catch(Exception e){
            System.debug('An unexpected error has occurred while updating Quote: ' + e.getMessage());
        }
    }
 }

Try this code in your Sandbox first then deploy it to production through changeSets. Better take  help from a developer as Shashank suggested.
Samuel Gonsalves 8Samuel Gonsalves 8
Thanks for your quick reply but i am very new to developement. I am a Salesforce administrator but not developement. What if i have to disable or stop the trigger then update the data what i need and then agian Enabled the trigger. can i do this in production. Please need a urgent help on this