function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mkchourasiamkchourasia 

Cloning an object in Scontrol

I am trying to Clone a custom object in the S-Control, and I am stuck can some one share their experience of cloning the object which is having parent child relationship with other objects.
WhyserWhyser
Code:
// Query all the fields of the custom object
var result = sforce.connection.query("select id, field1, field2, field3, etc from custom_object where id = '[id of object you want to clone]'" );
var records = result.getArray("records");

// Create an instance of a custom_object
var new_Custom_Object = new sforce.SObject("custom_Object__c");
new_Custom_Object.field1 = records[0].field1;
new_Custom_Object.field2 = records[0].field2;
new_Custom_Object.field3 = records[0].field3;
etc..;

// Create the Object
try
{
  var saveResult = sforce.connection.create( [new_Custom_Object] );
  
  // Check to see that the object was created successfully
  if ( saveResult[0].getBoolean("success") )
  {
    // Do nothing if successful
  }
  else
  {
    alert( "Error occurred: " + saveResult[0] );
  }

 
try something like the above code?
werewolfwerewolf
Actually you have more code there than you need.  All you actually have to do is null out the Id field of the object you just selected, and then call insert on it.  Salesforce will make a clone of it.
WhyserWhyser
hahah... freggin genius werewolf.
mkchourasiamkchourasia
Thanks alot that works.
SarbashishSarbashish
where I can run this S-Control??
rupinder jeetrupinder jeet

hi,

what will be the query if the object is Standard one? i am trying this thing on standard object. thanks for co-operation. Pls check the error in the code below:

It is giving exception in the query highlited below.

<html>
<head>
<script src="/soap/ajax/8.0/connection.js"></script>
<script src="/desktop/desktopApi.js"></script>
<script>
// Query all the fields of the custom object
var result = sforce.connection.query("select Account,NextStep from Opportunity  where Id=: OpportunityId" );
var records = result.getArray("records");

// Create an instance of a custom_object
var new_Custom_Object = new sforce.SObject("Opporutnity_cloned__c");
new_Custom_Object.Account = records[0].Account;
new_Custom_Object.NextStep = records[0].NextStep ;

// Create the Object
 try
 {
  var saveResult = sforce.connection.create( new_Custom_Object );
  // Check to see that the object was created successfully
  if ( saveResult[0].getBoolean("success") )
  {
     alert( "Success  " );
  }
  else
  {
    alert( "Error occurred: " + saveResult[0] );
  }
//}
</script>
</head>
<body onload="init()">
<p>&nbsp;
</p>
</body>
</html>

regards,

rupinder.

werewolfwerewolf
Look at your error message.  Account is not a field on Opportunity.  AccountId is.
WhyserWhyser


rupinder jeet wrote:

hi,

what will be the query if the object is Standard one? i am trying this thing on standard object. thanks for co-operation. Pls check the error in the code below:

It is giving exception in the query highlited below.




You have a few issues in your code
 
1) Query is wrong. Should be
 
Select AccountId, NextStep From Opportunity Where Id = '{!Opportunity.Id}'
 
2) Creating an instance of the Object is wrong

var new_Custom_Object = new sforce.SObject("Opportunity");
 
3) The Create call needs to put your custom object into an array
 
var saveResult = sforce.connection.create( [new_Custom_Object] );
 


Message Edited by Whyser on 06-18-2008 01:41 PM
merthenmerthen
I am simply trying to clone an Opportunity with a new name (everything else being the same).  Is there a way to do this without having to create a query selecting ALL of the fields, individually, that I want to clone?  Something like this "select * from Opportunity where id='006lalalalalalala'"  I understand that "select *" will not work, but shurely there is a way to do this without listing all of the fields, one by one.