Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Here is my product trigger that calls the opp trigger

 trigger productdata_changed on Product2 (after update)
{
     for(Product2 prod:Trigger.new)
        {
            List<Opportunity> opp = new List<Opportunity>();
            List<OpportunityLineItem> oli = new List<OpportunityLineItem>();
            oli = [Select OpportunityId from OpportunityLineItem where Product2Id =:prod.Id];
                for(OpportunityLineItem parser_oli:oli)
                {
                      opp = [Select id from Opportunity where id=:parser_oli.OpportunityId];
                }
                try
                {
                    update opp ;
                }
                catch(Exception e)
                {
                    System.debug('error is '+e.getMessage());
                }
        }
}

and my oportunity before update trigger just changes the values of 3 opp fields .


trigger productdata_changed on Product2 (after update) {

  Set<ID> ids = new Set<ID>();
    for(Product2 prod_old : Trigger.old)
    {
        for(Product2 prod_new : Trigger.new)
        {
            if(prod_old.Status__c != prod_new.Status__c) // check for the fields that include in a a score calculation 
            {
                  ids.add(prod_new.id);
            }
        }
    }
if(ids.size() >=1)
{
        System.debug('size of the list is '+ids.size());
        List<Opportunity> opp = new List<Opportunity>();
        List<OpportunityLineItem> oli = new List<OpportunityLineItem>();
        oli = [Select OpportunityId from OpportunityLineItem where Product2Id in :ids];
            for(OpportunityLineItem parser_oli:oli)
            {
                  opp.add([Select id from Opportunity where id=:parser_oli.OpportunityId]);
            }
            try
            {
                update opp ;
            }
            catch(Exception e)
            {
                System.debug('error is '+e.getMessage());
            }

} this is waht i did . is it looking fine?

share|improve this question
1  
Welcome to SFSE! You might want to include your errors, in case that might help someone get to an answer faster! –  Kelly J Andrews Feb 21 at 11:52
add comment

1 Answer 1

Your problem is you are not following best practices for trigger bulkification. Try refactoring your code to remove the SOQL queries from inside the for loops as if your trigger has a volume of records (more than 50) it will blow the query governor limit of 100 queries (as you are currently performing 2 queries per record).

Something like below would be better - this code has just simply been a tweaked rewritten version of your code - I have not tested it and you should make sure you understand it properly and test it before use.

trigger productdata_changed on Product2 (after update)
{
    List<Opportunity> opp = new List<Opportunity>();
    List<OpportunityLineItem> oli = new List<OpportunityLineItem>();

    oli = [Select OpportunityId from OpportunityLineItem where Product2Id =:Trigger.newMap.keySet()];

    Set<Id> oppIds = new Set<Id>();

    for(OpportunityLineItem oppLine : oli)
    {
        oppIds.add(oppLine.OpportunityId);
    }

    opp = [Select id from Opportunity where id=:oppIds];

    try
    {
        update opp;
    }
    catch(Exception e)
    {
        System.debug('error is '+e.getMessage());
    }

}

I would recommend you google "trigger bulkification salesforce" and read some of the good articles that come up and/or go and buy Dan Appleman's book

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.