• FDImarz
  • NEWBIE
  • 25 Points
  • Member since 2012

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    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

Hi. I'm looking to create some Apex code that changes the record type of an opportunity to 'Multi' when brands/products from more than one Product Family are added to a single opportunity.

 

So the workflow is this:

 

1. User clicks New button to create a new opportunity

2. User chooses a record type for that opty -- Media, Events, etc.

3. User fills in opportunity info and chooses various products/brands to attach to that opty. Each brand/product has a Product Family.

4. Apex code is triggered when brands/products are added to the opty that are from more than one Product Family (not necessarily just two or more products, which could all be from the same Product Family) -- this would trigger the record type for that opty to be a) changed to Multi and b) the record type set to read-only. (optional) If the products/brands for that opty are changed again, the record type would revert to the original record type.

 

I'm not an Apex developer and I'm not sure where to start here. I'm hoping someone can point me in the right direction. Thanks in advance.