+ Start a Discussion

"ClonedFrom" Opportunity Field... fun challenge

I've recently been tasked to customize the clone function on Opportunities so that various additional things can happen (or not happen) such as cloning of custom child objects.  I realized that the biggest challenge here seems to be flagging an Opportunity as a clone... if you can do that, the rest is easily done with workflow or triggers.
So, I've done some experimentation and believe I found a way to populate a field called "Cloned From" on clones only... it is a lookup to the original opportunity.  Then use Apex to check for this after inserts, and act accordingly.
So my challenge to you is...  How can you detect that a record is a clone and flag it?
Once I'm certain my approach is solid I will post an explanation... in the meantime, I thought I would get your gears turning.
If you're going to use Apex anyway, then when you build the cloning function, add additional hidden fields of a clone flag and you could even add a field that tracks the id of the original opportunity you cloned from (you don't really need to do that since your Apex code is doing the cloning anyway but if you want to store historically where the cloned record came from then add those). 

We also replaced the standard clone function with Apex recently and this is easy to do for the same reasons you described (clone some fields, not others, clone some child objects, not others).

So just build this as an Apex class that you trigger by pushing a Button that is jscript calling the Apex class.

Ah yes, I should have brought this up myself...  I tend to follow what I like to believe are 2 important "best practices" with force.com development.

1.  Use native functionality to get you as far as you can, code minimally only when you have to.  This will ensure your solution is optimally maintainble and upgradable.

2.  Try to avoid overriding or otherwise usurping standard salesforce functionality if possible.  Leveraging what's already there not only makes your life easier from a plan-build-test perspective, but will "keep you in the game" so to speak on any enhancements that salesforce will make to that functionality.

You are correct however, you can definitely write your own clone function... and with enough code you could even have it detect the addition of new fields and include them automatically i bet.  So there we have one possibility... anyone else?