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
searssdsearssd 

Trigger new Case on Opportunity close and Account custom field

Hello-

 

I am an Apex noob, developing my first Trigger. Thanks to all of you who have provided great examples on these disucssion boards!

 

I have successfully created, tested and deployed a Trigger and test class into our production environment that does the following:

 

After an Opportunity is updated: If the stagename is changed to "Closed Won" AND the associated Account custom field "Host Notes" is not null, then we create a new Case with pre-populated fields from the Account.

 

Here is the code:

 

trigger NewHostCaseFromOpp on Opportunity (after update) 

 for (Opportunity opp : Trigger.new) 
 { 
    if ( (opp.stagename == 'Closed Won' ) && (trigger.oldMap.get(opp.id).stagename != 'Closed Won' ) )
         {
         list<Account> act=[select id,name,Host_Notes__c from Account where Id=:opp.AccountId];
             if(act[0].Host_Notes__c!=null)
                    { 
                       list<contact> ct=[select id from Contact where AccountId=:act[0].id];
                       Case c = new Case ( 
                           AccountID = opp.AccountId, 
                           Status = 'New', 
                           Origin = 'Salesforce',
                           Type = 'Host', 
                           ContactID = ct[0].id,
                           Subject = 'Host Case for '+act[0].name, 
                           OwnerId= '00G50000001WRLQ' );    //This is the ID for the Host queue
                       insert c; 
                    }
          } 
    else 
    {
    } 
 } 
}

In *my* testing, this works as expected. The test class also passed 100% code coverage and was successfully deployed into our production environment.

 

Here's the problem:

When the sales persons use their process to update a "real" opportunity, there are 2 identical cases created by my code.

 

When comparing the test accounts and opportunities with "real" accounts and opportunities I do not see any difference. Yet my tests only produce 1 case (which is the expected behavior).

 

I believe the problem is in my code. I will appreciate anyone spotting and sharing my mistake(s).

 

Thank you!

 

Scott

Best Answer chosen by Admin (Salesforce Developers) 
FDImarzFDImarz

I had a similar issue with this code; it creates two cases.

 

I remembered that I have a workflow that calculates the amount field based on the probability.  When I deactivated the workflow rule, the trigger only created one case.

All Answers

Glen.ax1034Glen.ax1034

i am thinking that there might be a workflow on production that hits that trigger twice.

 

but what i'd recommend is refreshing the sandbox and seeing if you an replicate the problem in the sandbox.

MandyKoolMandyKool

Hi,

 

Looks like the trigger is getting called in bulk.

As from code the only way possible to create 2 cases is that the loop is getting executed 2 times and both the times the conditions are getting satisfied.

 

Not sure that is the expected behaviour or not. You can follow the same steps as that of sales persons to reproduce that.

You can use System.debug() statements in your for loop, so that you will get to know what exactly happening.

 

If you dont want 2 cases to be inserted; you can add "break" statement after the "insert c" statement. 

But ideally you should try to reproduce it and check why the loop is repeating 2 times and modify your code accordingly.

 

Thanks,

Mandar.

searssdsearssd

I liked the idea of the break statement as a temporary fix while I dig deeper and resolve the real cause.

 

now I have

 

trigger NewHostCaseFromOpp on Opportunity (after update) 

 for (Opportunity opp : Trigger.new) 
 { 
    if ( (opp.stagename == 'Closed Won' ) && (trigger.oldMap.get(opp.id).stagename != 'Closed Won' ) )
         {
         list<Account> act=[select id,name,Host_Notes__c from Account where Id=:opp.AccountId];
             if(act[0].Host_Notes__c!=null)
                    { 
                       list<contact> ct=[select id from Contact where AccountId=:act[0].id];
                       Case c = new Case ( 
                           AccountID = opp.AccountId, 
                           Status = 'New', 
                           Origin = 'Salesforce',
                           Type = 'Host', 
                           ContactID = ct[0].id,
                           Subject = 'Host Case for '+act[0].name, 
                           OwnerId= '00G50000001WRLQ' );    //This is the ID for the Host queue
                       insert c; 
                       break;
                    }
          } 
    else 
    {
    } 
 } 
}

 

Believe it or not the problem persists, even after adding the break statement. I'm struggling to understand how that is possible.

FDImarzFDImarz

I had a similar issue with this code; it creates two cases.

 

I remembered that I have a workflow that calculates the amount field based on the probability.  When I deactivated the workflow rule, the trigger only created one case.

This was selected as the best answer
kevin.chileskevin.chiles

This is great!  Is there anyway to stamp my opportunity with the case number as well?  That would be huge for us!

searssdsearssd
Thanks! We did indeed have a rule that was causing the Trigger to fire twice.