Using the COM API to Clone a Record

Hints, Tips and Tricks

Technical Hints Tips and Tricks that cover customization and development using Sage CRM. API usage and coding are covered.

Using the COM API to Clone a Record

  • Comments 1
  • Likes

There are a number of articles within this community site that discuss the requirement that some business has to clone records.  This might be where a customer needs to copy an existing communication to set up a similar meeting or where an opportunity is very similar but just some details need changing.

There are some cloning features turned on by default.  For example, you can clone an order.

Some of the discussion about the cloning of Information can be found here:

Adding a Clone Campaign button to Sage CRM using ASP COM API

What is would be useful is a technique to allow us to simply copy all the values in one CRM record instance to a new CRM record without having to reference every field.  Where could then change only the individual fields we need before the record is committed.

We can do this using the fact that in the COM API the record object is enumerable.

You can test this in an ASP page using the example code below:

e = new Enumerator(myRecord);
Response.Write("<table class=content width='50%'>");
while (e.item())
{
  Response.Write("<tr><td class='row1'>"+e.item()+"</td><td class='row2'>"+myRecord(e.item())+"</td></tr>");
  e.moveNext();
}
Response.Write("</table>");
 

If we want to clone a record like an opportunity or a lead then then code would look like this:

var intRecordId = CRM.GetContextInfo("lead","lead_leadid");
var FirstRecord = CRM.FindRecord("lead","lead_leadid="+intRecordId);
var NewRecord = CRM.CreateRecord("lead");
 
//copy the data from the first record to the second
e = new Enumerator(FirstRecord);
while (e.item())
{
  NewRecord(e.item()) = FirstRecord(e.item())
  e.moveNext();
}
 
//set the fields and properties unique to the new record
NewRecord.lead_description = NewRecord.lead_description +"- cloned";
NewRecord.lead_stage = "Assigned";
NewRecord.SetWorkflowInfo("Lead Workflow", "Assigned");
 
NewRecord.SaveChanges();
 

Comments
  • Jeff in your last example

    var intRecordId = CRM.GetContextInfo("lead","lead_leadid");

    var FirstRecord = CRM.FindRecord("lead","lead_leadid="+intRecordId);

    var NewRecord = CRM.CreateRecord("lead");

    //copy the data from the first record to the second

    e = new Enumerator(FirstRecord);

    while (e.item())

    {

     NewRecord(e.item()) = FirstRecord(e.item())

     e.moveNext();

    }

    //set the fields and properties unique to the new record

    NewRecord.lead_description = NewRecord.lead_description +"- cloned";

    NewRecord.lead_stage = "Assigned";

    NewRecord.SetWorkflowInfo("Lead Workflow", "Assigned");

    NewRecord.SaveChanges();

    what if any .js files need to be included at the top  I have created this script made a new tab on the opportunity and pointing it to the customfile .asp page created.  the page trys to load and hangs on me.  I don't see any errors in the logs but yet it doesn't create a clone opportunity.  I used fiddler2 to see if I noticed anything and nothing stood out.  Here is my code I am using.

    <!-- #include file ="../crmwizard.js" -->  

    <!-- #include file ="../crmconst.js" -->  

    <%

    var intRecordId = CRM.GetContextInfo("opportunity","oppo_opportunityid");

    var FirstRecord = CRM.FindRecord("opportunity","oppo_opportunityid="+intRecordId);

    var NewRecord = CRM.CreateRecord("opportunity");

    //copy the data from the first record to the second

    e = new Enumerator(FirstRecord);

    while (e.item())

    {

     NewRecord(e.item()) = FirstRecord(e.item())

     e.moveNext();

    }

    //set the fields and properties unique to the new record

    NewRecord.oppo_description = NewRecord.oppo_description +"- cloned";

    NewRecord.oppo_stage = "Sold";

    NewRecord.SetWorkflowInfo("Opportunity Workflow", "Sold");

    NewRecord.SaveChanges();

    %>