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

2 minute read time.
Here is an example that uses the Button Group feature added in Sage CRM.



You can see from the screen shot above that I have added a button to the system generated Campaign Summary Screen.

When the button is pressed, the following screen appears that allows me to provide a new name for the campaign and then save it.



Once the save button is pressed this creates the new campaign, copying over the waves and waveitems but clearing down the actual cost incurred within each waveitem.



The button on the campaign summary screen has been added using the ButtonGroup.



The image above shows that the creation of the buttongroup is very similar to creating a tabgroup.

The code of the ASP page looks like this:



var intCampaignId = CRM.GetContextInfo("campaigns","camp_campaignid");
if (CRM.Mode==View)
{
CRM.Mode=Edit;
}

//Create Campaign Screen
var myBlock = CRM.GetBlock("CampaignDetailBox");
myBlock.Title = CRM.GetTrans("Button","clone");

//Switch off all the fields of the campaign screen except the camp_name
var myE = new Enumerator(myBlock);
while (!myE.atEnd())
{
myEntryBlock = myE.item();
if (myEntryBlock.FieldName!= "camp_name")
{
myBlock.DeleteEntry(myEntryBlock.FieldName);
}
myE.moveNext();
}

CRM.AddContent(myBlock.Execute());
Response.Write(CRM.GetPage("Campaign"));

if (CRM.Mode==Save)
{
//copy the campaign
var oldCampaign = CRM.FindRecord('campaigns','camp_campaignid='+intCampaignId);
var newCampaign = CRM.CreateRecord('Campaigns');
newCampaign.Camp_Name= Request.Form('Camp_name');
newCampaign.Camp_StartDate= oldCampaign.Camp_StartDate;
newCampaign.Camp_EndDate= oldCampaign.Camp_EndDate;
newCampaign.Camp_Status= 'Pending';
newCampaign.Camp_Budget= oldCampaign.Camp_Budget;
newCampaign.Camp_Budget_CID= oldCampaign.Camp_Budget_CID;
newCampaign.Camp_Cost_CID= oldCampaign.Camp_Budget_CID;
newCampaign.Camp_Type= oldCampaign.Camp_Type;
newCampaign.Camp_AssignedUserId= oldCampaign.Camp_AssignedUserId;
newCampaign.SaveChanges(); //SaveChanges to have the record persist in database

var Wave_CampaignId = newCampaign.Camp_CampaignId;
var oldWaves = CRM.FindRecord('waves','wave_campaignid='+oldCampaign.camp_campaignid);
var newWaves
var newWaveItems
var Wave_WaveId
var oldWaveItems

//copy the waves
while (!oldWaves.eof) //eof checks for end of data and instantiates query
{
newWaves = CRM.CreateRecord('Waves');
newWaves.Wave_CampaignId = Wave_CampaignId;
newWaves.Wave_Name = oldWaves.Wave_Name;
newWaves.Wave_StartDate = oldWaves.Wave_StartDate;
newWaves.Wave_EndDate = oldWaves.Wave_EndDate;
newWaves.Wave_Status = 'Pending';
newWaves.Wave_Budget = oldWaves.Wave_Budget;
newWaves.Wave_Budget_CID = oldWaves.Wave_Budget_CID;
newWaves.Wave_cost_CID = oldWaves.Wave_cost_CID;
newWaves.Wave_AssignedUserId = oldWaves.Wave_AssignedUserId;
newWaves.SaveChanges();

Wave_WaveId = newWaves.Wave_WaveId;
oldWaveItems = CRM.FindRecord('waveitems','wait_waveid='+ oldWaves.wave_waveid);

//copy the waveitems
while (!oldWaveItems.eof) //eof checks for end of data and instantiates query
{
newWaveItems = CRM.CreateRecord('WaveItems');
newWaveItems.WaIt_WaveId = Wave_WaveId;
newWaveItems.WaIt_Name = oldWaveItems.WaIt_Name;
newWaveItems.WaIt_Type = oldWaveItems.WaIt_Type;
newWaveItems.WaIt_Details = oldWaveItems.WaIt_Details;
newWaveItems.WaIt_StartDate = oldWaveItems.WaIt_StartDate;
newWaveItems.WaIt_EndDate = oldWaveItems.WaIt_EndDate;
newWaveItems.WaIt_Status = 'Pending';
newWaveItems.WaIt_Budget = oldWaveItems.WaIt_Budget;
newWaveItems.WaIt_Budget_CID = oldWaveItems.WaIt_Budget_CID;
newWaveItems.WaIt_cost_CID = oldWaveItems.WaIt_cost_CID;
newWaveItems.WaIt_TargetList = oldWaveItems.WaIt_TargetList;
newWaveItems.WaIt_AssignedUserId = oldWaveItems.WaIt_AssignedUserId;
newWaveItems.SaveChanges();

oldWaveItems.NextRecord();
}
oldWaves.NextRecord();
}

//Redirect to the Campaign List
Response.Redirect(CRM.Url(Act=970))
}



Note: Please remember that this code is provided to prove a point. Here we have seen how we can use the ButtonGroup feature to add a new bit of functionality and seen the way in which we can then clone existing data within the system. You will need to customise this still further to make sure that the page does what you need it to do.