You need to sign in to do that
Don't have an account?

Hitting Governor Limit and do not understand why
In the past I was hitting the govenor limit of 21 quieres and have been able to work through that, but after I retooled my trigger and class, I am getting the same error.
I have this trigger:
trigger UpdateOppChatterFeed on OpportunityLineItem (after update) { integer size = Trigger.new.size(); for(integer i = 0; i < size; i++) { Chatter_Feed cf = new Chatter_Feed(); cf.updateChatter(Trigger.new[i]); } }
which calls this class:
public with sharing class Chatter_Feed { public void updateChatter(OpportunityLineItem oli) { List<FeedPost> posts = new List<FeedPost>(); List<Opportunity> oppList = new List<Opportunity>(); List<Opportunity> OppsToBeUpdated = new List<Opportunity>(); oppList = [select Id from Opportunity where id = : oli.OpportunityId]; id idToUpdate; for(Opportunity opp : oppList) { idToUpdate = opp.Id; } System.debug('MIKES OPP ID IS ' + idToUpdate); //update OppsToBeUpdated; List<OpportunityFeed> opportunityFeedPosts = [SELECT Id, Type, FeedPost.Body From OpportunityFeed Where ParentID = :idToUpdate ORDER BY CreatedDate DESC]; String bodyText = 'This is the body '; FeedPost opportunityPost = new Feedpost(); opportunityPost.Type = 'LinkPost'; opportunityPost.Title = 'THIS IS THE TITLE WILL'; opportunityPost.Body = bodyText; String id = String.valueOf(oli.id).substring(0,15); opportunityPost.LinkURL = 'https://cs1.salesforce.com/'+id; opportunityPost.ParentID = idToUpdate; posts.add(opportunityPost); //} insert posts; } }
The only for() loop that I have is in the class and does not query any sObjects.
This trigger/class works when I edit 1 OpportunityLineItem, but our Org has an option to edit all and in some cases, there are more than 21 line items on an Opportunity.
Why is this failing? Can someone please give me some guidance here?
I would appreciate any feedback
Thanks!!
~Mike
I was able to re-write the trigger and posted the resolution on my blog:
http://www.mikesimonds.com/blogs/mike/25-saleforce-apex-chatter-trigger-tutorial.html
All Answers
You should not have the queries inside the loop. What is happening is that since you are calling updateChatter as many times as there are OpportunityLineItems to be updated, it is executing the two SOQL queries that many times, including the insert posts part.
In this case, doing the processing inside a separate class will probably needlessly complicated things.
In the main trigger, try something like this:
I could be missing something since I'm not sure if I'm able to follow all of your logic. Hope that helps some at least!
Thank you very much for responding and I will try your solution out and let you know if it works. I appreciate you taking the time to do this.
~Mike
I tried your trigger and it works, I appreciate it, but running into one snag.
In the lower part of the trigger
you see the commented out String Id variable that we are creating. it is supposed to create a link that has the ID of the opportunity line item (oli.id) but in the upper for loop you are just adding the opporunityId to the list > oppIdsToBeUpdated
Eclipse is telling me that oli.Id is not defined and I agree, but how can I define it when you are just adding the OpporunityId to that list
Thanks for all your help!!
~Mike
I wanted to add the whole trigger in case it would make more sense
Unless I'm missing something, I think you might be able to do it just in a single loop!
:smileywink: that did it, I did not even think about that.
Listen there are a couple more things that I need to add
We only want to update the Chatter post if it meets some business logic. Your fix has pointed us in the right direction and I totally appreciate that very much.
If you look at the original trigger we were working on it was also causing issues with the govenor limit
As you can see, we want to compare fields and we are trying to put the part name on the chatter feed when someone changes the part_outcome__c.
It would say something like:
Mike has updated the MAX6008AEUR+T from Open to Won at Wecan
MAX6008AEUR+T socket details (this would be a link) > OpportunityPost.LinkURL
Does this make sense
Again, I do appreciate your help and guidance
~Mike
I was able to re-write the trigger and posted the resolution on my blog:
http://www.mikesimonds.com/blogs/mike/25-saleforce-apex-chatter-trigger-tutorial.html