• searssd
  • NEWBIE
  • 0 Points
  • Member since 2011

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 2
    Replies

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

  • September 29, 2011
  • Like
  • 0

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

  • September 29, 2011
  • Like
  • 0