You need to sign in to do that
Don't have an account?
Will Foley
System.LimitException: Too many query rows: 50001 - Just Started Throwing the Code
This issue came up without any new code being deployed.
All help is greatly appreciated.
Description of Trigger
The Trigger automatically greats a renewal opportunity after a new opportunity is marked as closed one.
Code
All help is greatly appreciated.
Description of Trigger
The Trigger automatically greats a renewal opportunity after a new opportunity is marked as closed one.
Code
trigger RenewalOpptTrigger on Opportunity (after update) { Map<Id, Account> accts = new Map<Id, Account>([SELECT Id, Name FROM Account]); List<Opportunity> opps = new List<Opportunity>([SELECT Name FROM Opportunity]); //Checking paramaters for Renewal Opportunity - Must be Won and Closed, etc. if (trigger.isUpdate){ for (Opportunity opp : Trigger.new){ System.debug('Opportunity: ' + opp.Name + ', Stage: ' + opp.StageName); if(opp.StageName == 'Customer - Closed Won'){ if(opp.Type == 'Renewal' || opp.Type =='Renewal and Expansion' || opp.Type == 'New - Enterprise' || opp.Type == 'New Business Expansion'){ System.debug(LoggingLevel.Error, 'Opp ID: ' + opp.Id); //Renewal Date Calculations - Renewal Start and End Dates Date contractStartDate = opp.Contract_Start_Date__c; integer duration = (integer)opp.Contract_Duration_Months__c; Date renewalStartDate = contractStartDate.addMonths(duration); system.debug(renewalStartDate); Account acct = accts.get(opp.AccountId); String acctName = acct.Name; String stage = 'SAL Demo Scheduled'; decimal amount; if(opp.License__c != null && opp.Contract_Increase__c!=null ){ amount = ((opp.Contract_Increase__c/100)+1) * opp.License__c; } else{amount = opp.License__c;} system.debug(amount); String title = String.valueOf(renewalStartDate.year()) + '_Renewal_' + acctName + '_$' + String.valueOf(amount.format()); OpportunityContactRole[] oppconroles = [SELECT IsPrimary, ContactId FROM OpportunityContactRole WHERE OpportunityId = :opp.Id]; boolean exists = false; for (Opportunity o : opps) { if (o.Name == title) { exists = true; System.debug(LoggingLevel.Error, o.Name + ' already EXISTS'); } } if(!exists){ Opportunity ren_opp = opp.clone(false, true, false, false); ren_opp.License__c = amount; ren_opp.Amount = amount; ren_opp.Previous__c = opp.License__c; system.debug(ren_opp.License__c); ren_opp.StageName = stage; ren_opp.Contract_Start_Date__c = renewalStartDate; ren_opp.Contract_Duration_Months__c = duration; ren_opp.CloseDate = renewalStartDate; ren_opp.Type = 'Renewal'; ren_opp.Probability = 10; if(ren_opp.Contract_Increase__c>0){ ren_opp.Type='Renewal and Expansion';} ren_opp.Name = title; //System Information deletions ren_opp.CampaignId = null; ren_opp.Date_moved_to_Closed_Lost__c = null; ren_opp.Date_moved_to_Closed_Won__c = null; ren_opp.Date_moved_to_Contract_Sent__c = null; ren_opp.Date_moved_to_Demo_Complete__c = null; ren_opp.Date_moved_to_Demo_Scheduled__c = null; ren_opp.Date_moved_to_Verbal_Commit__c = null; ren_opp.Date_moved_to_Proposal_Sent__c = null; ren_opp.Amount_at_Closed_Lost__c = null; ren_opp.Amount_at_Closed_Won__c = null; ren_opp.Amount_at_Contract_Sent__c = null; ren_opp.Amount_at_Demo_Complete__c = null; ren_opp.Amount_at_Demo_Scheduled__c = null; ren_opp.Amount_at_Proposal_Sent__c = null; ren_opp.Amount_at_Verbal_Commit__c = null; //SOW Deletions ren_opp.Unique_Pricing_Scenario__c = null; ren_opp.Department__c = null; ren_opp.Signature_Events__c = null; ren_opp.Number_of_Seats__c = null; ren_opp.Special_Admin_Structure__c = null; ren_opp.Design_Resources__c = null; ren_opp.Needs_Designer_Pro__c = false; ren_opp.Key_Functionality_Landmines__c = null; ren_opp.Custom_Dev_Work__c=null; ren_opp.Splash_User_Experience__c=null; ren_opp.Kickoff_Email_Recipients__c=null; ren_opp.Imminent_Deadline__c=null; ren_opp.Client_Fears_or_Concerns__c=null; ren_opp.Opportunities_for_Growth__c = null; ren_opp.Opportunities_for_Growth_Yes__c = null; ren_opp.Additional_Comments__c = null; ren_opp.Require_Starter_Themes__c = null; // ren_opp.SOW_End_Date__c = renewalStartDate; ren_opp.Primary_Purpose_of_Events__c = null; //Docusign deletions ren_opp.DocuSign_Status__c = null; //Discovery Questions deletions ren_opp.Annual_of_Events__c = null; ren_opp.CRM__c=null; ren_opp.CRM_Other__c=null; ren_opp.CRM_Other__c = null; ren_opp.Budget__c = null; ren_opp.Purpose_of_Events__c = null; ren_opp.Purpose_of_Events_Other__c=null; ren_opp.What_are_they_currently_using__c = null; ren_opp.Marketing_Automation__c = null; ren_opp.Marketing_Automation_Other__c = null; ren_opp.Role_of_Primary_Contact__c = null; ren_opp.Role_of_Primary_Contact_other__c = null; ren_opp.Current_process_for_event_program__c = null; ren_opp.Currently_using_other__c = null; ren_opp.Value_Driver_details__c = null; ren_opp.Primary_Purpose_of_Events_Other__c = null; ren_opp.Need_SPS__c = null; //Other Deletions ren_opp.ForecastCategoryName = 'Pipeline'; ren_opp.Agreed_to_Case_Study__c = false; insert ren_opp; List<OpportunityContactRole> rolesToInsert = new List<OpportunityContactRole>(); for (OpportunityContactRole opprole : oppconroles) { System.debug(LoggingLevel.ERROR, 'ContactRole: ' + opprole.ContactId); OpportunityContactRole ocp = opprole.clone(false, true, true, true); ocp.OpportunityId = ren_opp.Id; rolesToInsert.add(ocp); } if (rolesToInsert.size() > 0) { insert rolesToInsert; } update ren_opp; } else{} } else{ System.debug('Case is not of the correct type'); } } else{ System.debug('Case '+ opp.Name + ' is not Closed and Won'); } } } }
1) Added filter in SOQL
2) Remove SOQL from for loop.
Please try below code.
Try to remove DML from for loop
Please let us know if this will fix your issue
All Answers
1- Move your code to classes. Call your class methods from the trigger depending on before/after insert, update etc...
2- Break this into methods
3- How many Opportunities do you have now? Average related Contact roles? Partial bulkification will work depending on the amount of records you store into the Opportunity obj. However once you store a ton of contact roles, you will need to bulkify further. In this case I recommend you start using the batching classes, so your update statements don't get attempted against +50,001 contact role records.
4- Any reason you're dumping all opptys into your opp list? It seems you're only dealing with Closed ones. If so, consider
List<Opportunity> opps = new List<Opportunity>([SELECT Name FROM Opportunity where isClosed = TRUE]);
5- Check out this (https://developer.salesforce.com/forums/?id=906F00000008tEmIAI) post. Its a stopgap, but setting the
OpportunityContactRole[] oppconroles = [SELECT IsPrimary, ContactId FROMOpportunityContactRole WHERE OpportunityId = :opp.Id LIMIT 50000]; on the Contact Roles query 1st will help you determine if thats whats popping your governor.
Good Luck
RSM
You are query all account and opportunity which is wrong. please get only required record like below Let us know if this will help you
1) Added filter in SOQL
2) Remove SOQL from for loop.
Please try below code.
Try to remove DML from for loop
Please let us know if this will fix your issue