You need to sign in to do that
Don't have an account?

Create a New Opportunity Record based on Stage = "Closed/Won"
Hi everyone, I am fairly new to apex language. I am trying to create a simple script that will create a new Opportunity (Renewal) if the Stage = "closed/won". So, if an Opportunity Stage = "closed/won" create a new Opportunity, and duplicate any required fields and save the new record. The trigger would also need to be dependent on Record Type because we have many other divisions using other Page Layouts or by Role.
I have a hard time programming from scratch, especially on code that I have never written before. Does anyone have examples of something like this? Thanks
Message Edited by jk51141 on 05-06-2008 06:37 AM
I have a hard time programming from scratch, especially on code that I have never written before. Does anyone have examples of something like this? Thanks
Message Edited by jk51141 on 05-06-2008 06:37 AM
Message Edited by jk51141 on 05-06-2008 01:46 PM
If you want to increment the date do it after clone() and before the insert of the new object.
I'm trying to determine if I am reading this right. This line:
Can Trigger.old be used to reference the previous opportunity (like and ID for a SOQL statement). Is this a unique identifier.
Message Edited by jk51141 on 05-07-2008 08:33 AM
This line will clone the object:
Then you can do can do something like this:
This will make your new custom field in Opportunity (called "Previous Opportunity") for your cloned object point to the original one.
Hello,
I built a very lengthy scontrol to do just this (auto create a renewal oppty when an oppty is closed won) and now would like to re-write this in apex. Am wondering if you figured out how to get the code to work and to update additional fields on the newly created oppty? If so, would you be willing to share your code? thanks!
Currently the scontrol is copying the value from a bunch of fields and then setting the value in some. Some are standard fields and some are custom. I just cant figure out how to get it to copy the products as well. i have the following Apex code so far which is only setting the Name and pushing out the Close Date by one year.
Message Edited by sgorema on 08-12-2008 01:32 PM
Yea... Ok... but that is also only part of a larger battle. You just made it more complicated with products. Also, wouldn't you also want to run this on on a brand new Opportunity?
First, think about Events. The code you posted only fires when you Modify an Opportunity, not for Inserting a new one. Furthermore, the code you posted does not fire for modifying or inserting OpportunityLines (products) because event-wise that is not a modification of the Opportunity.
So, you need two triggers. One for Opportunities on the Insert and Update events (Possibly Delete???). One for OpportunityLines on the Insert, Update and Delete events. Those triggers then need to do different things for each of the given events (Insert, Update and Delete).
You need to think about how you want this to behave in all six paths. -- and how all these things might interact and be downstream chained together. To further complicate the problem, you could very quickly start creating Errors due to exceeding the set salesforce limits.
I guess what I'm saying is that including Products just made this 2-fold, if not more, worse for you. So I think you may want to think about the process (what users do [events], and what happens as a result) first and then build the code around that. You might also find that you don't want your users doing certain things at all... like delete for instance.
So Much for Easy, Huh?!?! :smileysurprised:
One thing to make very clear, adding/changing/deleting a Opportunity Line Item no the Opportunity Does NOT fire the Insert/Update/Delete event for the Opportunity itself. It only fires events for the OpportunityLine object itself.
I'm just trying to help you understand the whole scope of your need up front. It is still doable, just needs to be very well defined is all. :)
Of course that is true... but they do still need a second trigger to handle the same instances for the OpportunityLineItem Object. The "logical paths" is all that I'm talking about above, so of course you are right that you can combine many of those logical paths into the same trigger as you have shown, but what gets done in each of those user actions still needs to be thought out and explained.
I'm now wondering if the complexity made them want to just stick with the S-Control that they already had running.
Hi there - I was reading this info on cloning that you posted and I may have a few questions that I was wondering if you could answer. I am not a developer but I would like to go back to our developer with some research to kick them in gear!!
When we allow users to clone opportunities, it has opened up a few errors in the way our system works.
For example:
What are we doing wrong and can we still offer cloning? Can we allow cloning but only allow the opportunity to be cloned at a certain stage so that the sales guy has to populate all the necessary export control requirements for the cloned opportunity? What about products, can they clone with products or be prompted to add them once cloning?
Any help would be greatly appreciated!!
Message Edited by SiriusBlackOp on 10-06-2008 12:41 PM