function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SuvraSuvra 

Update Trigger

HI,

I have written one after update trigger on Opportunity standard object, which will update one field in Contact Object. But this trigger is getting fired twice.

 

Any idea on why the trigger is getting fired twice and how to resolve the problem?

 

Thanks in Advance,

Suvra

juppingerjuppinger

Hi Suvra,

could you post your code, please?

 

jup

SuvraSuvra

Yeah sure...

 

trigger UpdateOpptyIndicator on Opportunity (after insert,after update) {
    System.Debug('ENTERED INTO TRIGGER');
   Contact con,oldCon;
    if (trigger.isInsert) {
    System.Debug('INSIDE ISINSERT');
        if(trigger.isUpdate)
        {
            
        }
        else
        {
            for(Opportunity opp : trigger.new) {
                con=[Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c]; 
                if(con.Opportunity_Indicator__c != null)
                    con.Opportunity_Indicator__c = con.Opportunity_Indicator__c + 1;
                else
                    con.Opportunity_Indicator__c = 1;
                    System.Debug('First Place');
                    update con;
            }
        }
  }
  else {
      System.Debug('INSIDE ISUPDATE');
      if(trigger.isUpdate)
      {                
                for(Opportunity opp : trigger.new) {
                    Opportunity oldOpp = Trigger.oldMap.get(opp.id);
                    System.Debug('OLD OPPORTUNITY :' + oldOpp.Contact__c);
                    System.Debug('NEW OPPORTUNITY :' + opp.Contact__c);
                    if(opp.Contact__c != oldOpp.Contact__c) {
                        con=[Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c];
                        System.Debug('PREVIOUS VALUE :'+ con.Opportunity_Indicator__c);
                        if(con.Opportunity_Indicator__c != null)
                            con.Opportunity_Indicator__c = con.Opportunity_Indicator__c + 1;
                        else
                            con.Opportunity_Indicator__c = 1;
                            
                        System.Debug('NEW VALUE :'+ con.Opportunity_Indicator__c);
                        update con;
                        Contact con1 = [Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c ];
                        System.Debug('AFTER DATABASE UPDATE :'+ con1.Opportunity_Indicator__c);
                        break;

                    }
                    
                      //System.Debug('Second Place');
                 }
      }
  }
 /* try{
        update con;
     }catch(Exception e){
        System.debug('Exception Ocurred'+e.getMessage());        
    }*/

}
SuvraSuvra

Also, I am attaching the Debug log for your reference..

 

14:52:02 DEBUG - *** Beginning Opportunity Validation Rule Evaluation for 006R0000003JQXd
Start Time: 20090323092615.958
Rule Name: Check_Special_Char_on_Oppty_Name
Error Condition Formula: OR( Contains(Name,"#"),COntains(Name,"!"),COntains(Name,"@"),COntains(Name,"$"),COntains(Name,"%"),COntains(Name,"^"))
Value(s) Found: Name=Abbot Computers2
Result: PASS - Continue
End Time: 20090323092615.958
*** Ending Opportunity Validation Rule Evaluation for 006R0000003JQXd
*** Beginning UpdateOpptyIndicator on Opportunity trigger event AfterUpdate for 006R0000003JQXd

20090323092616.081:Trigger.UpdateOpptyIndicator: line 2, column 5: ENTERED INTO TRIGGER
20090323092616.081:Trigger.UpdateOpptyIndicator: line 24, column 7: INSIDE ISUPDATE
20090323092616.081:Trigger.UpdateOpptyIndicator: line 27, column 17: SelectLoop:LIST:SOBJECT:Opportunity
20090323092616.081:Trigger.UpdateOpptyIndicator: line 29, column 21: OLD OPPORTUNITY :003R000000BjUBKIA3
20090323092616.081:Trigger.UpdateOpptyIndicator: line 30, column 21: NEW OPPORTUNITY :003R000000BjLKdIAN
20090323092616.081:Trigger.UpdateOpptyIndicator: line 32, column 29: SOQL query with 1 row finished in 9 ms
20090323092616.081:Trigger.UpdateOpptyIndicator: line 33, column 25: PREVIOUS VALUE :0
20090323092616.081:Trigger.UpdateOpptyIndicator: line 39, column 25: NEW VALUE :1.0
20090323092616.081:Trigger.UpdateOpptyIndicator: line 40, column 25: Update: SOBJECT:Contact
20090323092616.081:Trigger.UpdateOpptyIndicator: line 40, column 25:     DML Operation executed in 315 ms
20090323092616.081:Trigger.UpdateOpptyIndicator: line 41, column 40: SOQL query with 1 row finished in 4 ms
20090323092616.081:Trigger.UpdateOpptyIndicator: line 42, column 25: AFTER DATABASE UPDATE :1

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 2 out of 20
Number of query rows: 2 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 1 out of 20
Number of DML rows: 1 out of 100
Number of script statements: 15 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0
Static variables and sizes:
UpdateOpptyIndicator:con:60


*** Ending UpdateOpptyIndicator on Opportunity trigger event AfterUpdate for 006R0000003JQXd


*** Beginning Workflow Evaluation
User: Suvra Mukhopadhyay
Start Time: 20090323092616.454
Starting All Rules Evaluation
Starting evaluation of rule type Assignment
Starting evaluation of rule type Response
Starting evaluation of rule type Workflow
[Opportunity: Abbot Computers2 006R0000003JQXd]
Rule Name: Updates Total Forecast Field
Trigger type: ON_ALL_CHANGES
Evaluating Workflow Entry Criteria:
[Account : Account Name not equal to null]
Value found: Test Acct
Criteria evaluates to true
[Opportunity: Abbot Computers2 006R0000003JQXd]
Rule Name: Update Forecast PI
Trigger type: ON_CREATE_ONLY
Evaluating Workflow Entry Criteria:
Rule not evaluated
Spooling All Immediate Actions
[Opportunity: Abbot Computers2 006R0000003JQXd]
  Field Update
  Field: Opportunity: Total Forecast
  Value: 0.00
Ending All Rules Evaluation
Bulk Execute all Immediate Actions*** Beginning UpdateOpptyIndicator on Opportunity trigger event AfterUpdate for 006R0000003JQXd

20090323092616.505:Trigger.UpdateOpptyIndicator: line 2, column 5: ENTERED INTO TRIGGER
20090323092616.505:Trigger.UpdateOpptyIndicator: line 24, column 7: INSIDE ISUPDATE
20090323092616.505:Trigger.UpdateOpptyIndicator: line 27, column 17: SelectLoop:LIST:SOBJECT:Opportunity
20090323092616.505:Trigger.UpdateOpptyIndicator: line 29, column 21: OLD OPPORTUNITY :003R000000BjUBKIA3
20090323092616.505:Trigger.UpdateOpptyIndicator: line 30, column 21: NEW OPPORTUNITY :003R000000BjLKdIAN
20090323092616.505:Trigger.UpdateOpptyIndicator: line 32, column 29: SOQL query with 1 row finished in 6 ms
20090323092616.505:Trigger.UpdateOpptyIndicator: line 33, column 25: PREVIOUS VALUE :1
20090323092616.505:Trigger.UpdateOpptyIndicator: line 39, column 25: NEW VALUE :2.0
20090323092616.505:Trigger.UpdateOpptyIndicator: line 40, column 25: Update: SOBJECT:Contact
20090323092616.505:Trigger.UpdateOpptyIndicator: line 40, column 25:     DML Operation executed in 27 ms
20090323092616.505:Trigger.UpdateOpptyIndicator: line 41, column 40: SOQL query with 1 row finished in 8 ms
20090323092616.505:Trigger.UpdateOpptyIndicator: line 42, column 25: AFTER DATABASE UPDATE :2

Cumulative resource usage:

Resource usage for namespace: (default)
Number of SOQL queries: 4 out of 20
Number of query rows: 4 out of 1000
Number of SOSL queries: 0 out of 0
Number of DML statements: 2 out of 20
Number of DML rows: 2 out of 100
Number of script statements: 30 out of 10200
Maximum heap size: 0 out of 100000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 10
Number of record type describes: 0 out of 10
Number of child relationships describes: 0 out of 10
Number of picklist describes: 0 out of 10
Number of future calls: 0 out of 10
Number of find similar calls: 0 out of 0
Number of System.runAs() invocations: 0 out of 0

Total email recipients queued to be sent : 0
Static variables and sizes:
UpdateOpptyIndicator:con:60


*** Ending UpdateOpptyIndicator on Opportunity trigger event AfterUpdate for 006R0000003JQXd


Starting evaluation of rule type Escalation
End Time: 20090323092616.588
*** Ending Workflow EvaluationCumulative profiling information:

2 most expensive SOQL operations:
Trigger.UpdateOpptyIndicator: line 32, column 29: [Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c]: executed 2 times in 15 ms
Trigger.UpdateOpptyIndicator: line 41, column 40: [Select id,Opportunity_Indicator__c from Contact where id=:opp.Contact__c ]: executed 2 times in 12 ms

No profiling information for SOSL operations.

1 most expensive DML operations:
Trigger.UpdateOpptyIndicator: line 40, column 25: Update: SOBJECT:Contact: executed 2 times in 342 ms

No profiling information for method invocations.

jyotijyoti

It seems as if your update is then firing a workflow which causes an update.  This, in turn, causes the after update trigger to fire again.  You may consider using a static object helper class to prevent the trigger from firing the second time.

 

private static boolean leadTriggerAlreadyFired = false; // Lead Getter and Setter public static Boolean getLeadTriggerAlreadyFired() { return leadTriggerAlreadyFired; } public static void setLeadTriggerAlreadyFired(Boolean inputValue) { leadTriggerAlreadyFired = inputValue; }

 

Then in your trigger have

// Most of the processing occurs before hand... if (!StaticObjectHelper.getLeadTriggerAlreadyFired()) { put your code here }

 

SuvraSuvra
Thanks a lot Jyoti..because of one workflow rule only the trigger was getting fired twice. Now the problem is resolved.