You need to sign in to do that
Don't have an account?
MaheemSam
Nested For loop Performance Issue
Hi,
In below method I am using two for loops inside which is a nested for loop. Code is working as expected but the only issue I see is that this might hit into performance issue when there are bulk operation Please suggest me any alternative way to overcome this issue in the code.
Thanks
Sudhir
In below method I am using two for loops inside which is a nested for loop. Code is working as expected but the only issue I see is that this might hit into performance issue when there are bulk operation Please suggest me any alternative way to overcome this issue in the code.
public static void processOptySharingOppTeam(List<Opportunity> newLst) { list<id> OpptIds = new list<id>(); list<OpportunityTeamMember> Otmlst = new list<OpportunityTeamMember>(); for(Opportunity opp : newLst) { OpptIds.add(opp.id); } for(Opportunity Op : [select id,name,account.Ultimate_Account__c,partner_account__r.Ultimate_Account__c from opportunity where id in :OpptIds]) { system.debug('Opportunity Name ' + Op.name + ' ' + Op.id); // Account for(Opportunity_Sharing__c Ops : [select id, Account_Names__c, Key_Text__c, Partner_Account_Names__c, User__c,Access_Level__c,Global_Account_Rep__c,Top_Account_ID__r.id, Top_Partner_Account_ID__r.id, User__r.name from Opportunity_Sharing__c where Top_Account_ID__r.id <> NULL AND Top_Account_ID__r.id = :op.account.Ultimate_Account__c]){ if(Ops.User__c <> null && Ops.Access_Level__c <> null){ system.debug('Opportunity Sharing ' + Ops.User__r.name); OpportunityTeamMember otm = new OpportunityTeamMember(OpportunityId = op.id,UserId=Ops.User__c,OpportunityAccessLevel=Ops.Access_Level__c ,TeamMemberRole='Global Account Shared Owner' ); Otmlst.add(otm); } } // Partner Account for(Opportunity_Sharing__c Ops : [select id, Account_Names__c, Key_Text__c, Partner_Account_Names__c, User__c,Access_Level__c,Global_Account_Rep__c,Top_Account_ID__r.id, Top_Partner_Account_ID__r.id, User__r.name from Opportunity_Sharing__c where Top_Partner_Account_ID__r.id <> NULL AND Top_Partner_Account_ID__r.id = :op.partner_account__r.Ultimate_Account__c]){ if(Ops.User__c <> null && Ops.Access_Level__c <> null){ system.debug('Opportunity Sharing ' + Ops.User__r.name); OpportunityTeamMember otm = new OpportunityTeamMember(OpportunityId = op.id,UserId=Ops.User__c,OpportunityAccessLevel=Ops.Access_Level__c ,TeamMemberRole='Global Account Shared Owner' ); Otmlst.add(otm); } } if(!Otmlst.Isempty()){ upsert Otmlst; } } }
Thanks
Sudhir
This should help.
You can try this, I have taken out your SOQL written in inner FOR loop. Because it cause the SOQL limit exception for you.
Note: In your comment might be SOQL limit occur for more than 100 Oppty.
Plz let me know if not working for you. Thanks
Niraj
Also in the code I cannot use OR opearot in SOQL because it will return rows when there is OR condition which is not a valid condition .
Thanks
SAM
It will work but It look like you are doing two time SOQL on Opportunity_Sharing__c, Insteed of that you can use one SOQL and combind both where condition together using OR. As I mentioned in my previous comment, it should work for you. You try once the same.
The below code leverages 2 additional maps oppIdUltAcctIdMap and oppIdUltPartnerAcctIdMap as well leverages the standard OpportuntiyId field on Opportunity_Sharing__c, to eliminate the top level for loop on Oplist.
Has your issue been resolved? Did the code help?