+ Start a Discussion

s-control for clone oppt


Has anyone had to mimic the clone button on an opportunity? It is a standard button. We have no access to the s-control source code. We wanted to replicate the clone button with a minor modification to populate a field that captures the opportunity number / name of the original deal. We were hoping to get a code snippet rather than re-invent the wheel.

Many thanks!

Ron HessRon Hess
i wrote a clone thingy (scontrol) , you will have pull it out of the package it's in, then modify it, then probably re-write it, but here you go


you basicaly do this :

 query into an sobject, the record using all the fields that are not readonly
 remove the ID from the sobject
 pass this same object to sforce.connection.create()

if you do this simply, you don't get the related objects ( deep clone) , but it sounds like you don't want that.

here are some snippets
function init() { 
var qbean = new sforce.SObject("SFDC_Action_Plan__c"); var desc = sforce.connection.describe('SFDC_Action_Plan__c'); // need field defs var qfields = desc.toUpdateFieldNames(); // list the editable fields , skip the read only var soql = "select Id," + qfields + " from SFDC_Action_Plan__c where id = '{!SFDC_Action_Plan__c.Id}'"; queryResult = sforce.connection.query(soql);
// more stuff removed here, see scontrol


// create the new record function clone(src) { // returns one record var clean = src; delete clean.Id; var dst = sforce.connection.create([clean]); throwUnless(dst); var ret = dst[0]; return ret; } // and a helper function ( note uses dojo ) // return list of fields used to create a clone sforce.Xml.prototype.toUpdateFieldNames = function () { var sb = []; this.eachField( function (f) { if (f.getBoolean('updateable') || f.getBoolean('createable') ) { sb.push( f.name); } } ); var ret = sb.join(', ').replace(/[, ]+$/g,''); return ret; }
sforce.Xml.prototype.eachField = function (iterator) {
  for (var f in this.fields) {
        var field = this.fields[f];
        if (field) iterator(field);


Message Edited by Ron Hess on 07-26-2007 04:46 PM

Vote early, vote often!  :-)  Steve.



Your clone() tip was very useful.
My preference would have been to use Apex. However, the Apex clone() method does not take the Id of the orginal object as the input.
Is there any way in Apex to obtain an object (Opportunity) given its Id?

Andi Giri