You need to sign in to do that
Don't have an account?
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
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
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.
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.
I liked the idea of the break statement as a temporary fix while I dig deeper and resolve the real cause.
now I have
Believe it or not the problem persists, even after adding the break statement. I'm struggling to understand how that is possible.
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 is great! Is there anyway to stamp my opportunity with the case number as well? That would be huge for us!