1

I've created a trigger for an Opportunity object that updates a lookup field with the value of the ownerID.

Here is the trigger code:

trigger trig_Opportunity_CreateOppOwner on Opportunity (before update,before insert) {

    List<id>OppsID = new List<id>(); //List that will hold all id of all new Opportunities owners

    for(Opportunity Opp:Trigger.new){ //Loop that only retrives new versions of the Opportunity 
        if(trigger.isInsert)
            OppsId.add(Opp.ID);
        else if (trigger.oldMap.get(Opp.id).Op_Owner__c != trigger.oldMap.get(Opp.id).OwnerId)//trigger.isInsert and trigger.isUpdate added to account when it's null, avoids NullPointerException. Now all inserted opportunities are processed.           
            OppsId.add(Opp.ID);//adds all new opportunity owners to OppsId list. //Checks to see if they are new owners and not already on the owner list. 
    }
    List<Opportunity> oppsFromDb =  [SELECT id, OwnerId FROM Opportunity WHERE id IN:OppsID];   


    for(Opportunity useOpps:oppsFromDb){                            
        trigger.newMap.get(useOpps.Id).Op_Owner__c = useOpps.OwnerId;
    }

This trigger works, but when I test a Talend batch file that would update 2000 Opportunity records per request with an approximate total of 10,000 Opportunities, it gives me a SOQL query 101 error because the trigger is automatically called and goes over the 100 SQL query limit.

I was thinking of fixing it using a batch apex file process. Here is what the pseudocode would look like:

        if(OppsID.size() <= 100) {
     List<Opportunity> oppsFromDb =  [SELECT id, OwnerId FROM Opportunity WHERE id IN:OppsID];   


        for(Opportunity useOpps:oppsFromDb){                            
            trigger.newMap.get(useOpps.Id).Op_Owner__c = useOpps.OwnerId;
    }
}

else {
//Call a batch apex class that would handle it}

Would this be a correct approach to take to try and bypass this problem? Thanks

6
  • 2
    I don't think you would need to batch it. Is that trigger the entire code? I expect to find a SOQL query inside a for loop somewhere. Jun 6, 2013 at 16:11
  • yup, that's the entire code. It doesn't give a SOQL query error on forceide, but when we run a talend job and update 2000 Opportunitities at once, it stops running after the 100th job because of a SOQL query error corresponding to this trigger.
    – SSadat
    Jun 6, 2013 at 16:18
  • 2
    Each invocation of the trigger gets a fresh set of limits, and each invocation will not handle more than 200 opportunities. So your talend job will be split into 10 chunks for the trigger. What exactly does your error message say?
    – Doug B
    Jun 6, 2013 at 16:21
  • here is the full error message on the talend: Exception in component tSalesforceOutput_1 java.lang.Exception: OpportunityEntitlement: System.LimitException: Too many SOQL queries: 101
    – SSadat
    Jun 6, 2013 at 16:30
  • 3
    Looks like you've got another trigger called OpportunityEntitlement that is causing the issues. Jun 6, 2013 at 16:32

1 Answer 1

3

Based on the comments, it looks like there isn't anything wrong with the trigger posted. There appears to be another trigger also firing that is causing the issue.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .