Sage CRM 2020 R1: Cloning a Person and assigning to a new Company

I have updated a sample ASP page that demonstrates how you can clone a Person record.  The sample ASP page demonstrates the code needed to copy a person record and all the associated information. The ASP page partly solves the issue that when a person moves company you should keep the existing person record linked to the company, to maintain integrity and business context for the opportunity, cases, communications etc.

To download the file go to

https://community.sagecrm.com/partner_community/m/example_components__developer_resources/28619.aspx


To implement add a 'Clone Person' button to the personsummary screen, you will need to follow these steps.
1) Unzip the download file and place the "switchcompany.asp" file in the CustomPages folder.
2) Create a new Button Group (e.g called Person Buttons) for the system action "personsummary".
3) Add a customfile action to the Button Group that calls "switchcompany.asp".

Note:
Personal, Home addresses and phone numbers are copied.  Once the record is created the business addresses will need to be assigned.  A good tip would be to then use the Address Link feature.

The actual code is below:

<!-- #include file ="sagecrm.js"-->
<%
/////////////////////////////////////////////////////////
//Filename: switchcompany.asp
//Description: This page will clone a person to a new company  It will copy private and personal information only.
//Date: 17/06/2020.
//Author: Jeff Richards */
//Version tested under: Sage CRM 2020 R1
//////////////////////////////////////////////////

try
{

//get the person to process
//get person record
//create a new person record
//get the personcompanypicker  screen
//add person record to screen
//send screen to browser

//on save
//copy all private email addresses
//copy all private home address
//copy all private phone numbers
//copy person record details

//save record

if(CRM.Mode==View)
{
CRM.Mode=Edit;
}

var intRecordId = CRM.GetContextInfo("person","pers_personid");
var recOldPerson = CRM.FindRecord("person","pers_personid="+intRecordId);

var recNewPerson = CRM.CreateRecord("person");
recNewPerson.pers_firstname = recOldPerson.pers_firstname;
recNewPerson.pers_lastname = recOldPerson.pers_lastname;
recNewPerson.pers_salutation = recOldPerson.pers_salutation;
recNewPerson.pers_suffix = recOldPerson.pers_suffix;
recNewPerson.pers_middlename = recOldPerson.pers_middlename;
recNewPerson.pers_gender = recOldPerson.pers_gender;

var screenWebPicker = CRM.GetBlock("personwebpicker");
screenWebPicker.Title = CRM.GetTrans("button","clone")
var fieldPersCompanyId = screenWebPicker.GetEntry("pers_companyid");
fieldPersCompanyId.DefaultType = 0;

CRM.AddContent(screenWebPicker.Execute(recNewPerson));

if (CRM.Mode==Save)
{
var recPersonLink = CRM.CreateRecord("person_link");
recPersonLink.peli_personid = recNewPerson.pers_personid;
recPersonLink.peli_companyid = recNewPerson.pers_companyid;
recPersonLink.SaveChanges();

var recComp = CRM.FindRecord("company","comp_companyid="+recNewPerson.pers_companyid);
recNewPerson.pers_secterr = recComp.comp_secterr;
recNewPerson.SaveChanges();

////////////////////////////////////////////////////////
//			start copying home addresses
////////////////////////////////////////////////////////

//Find addresses that are NOT Business addresses.
var strClause = "adLi_type != 'Business' and adli_personid =" + recOldPerson.pers_personid;
Response.Write(strClause)
var recAddressLinks = CRM.FindRecord("address_link",strClause);
var recAddress;
while (!recAddressLinks.eof)
{
	recOldAddress = CRM.FindRecord("address","addr_addressid="+recAddressLinks.adli_addressid);
	var recNewAddress = CRM.CreateRecord("address");
	recNewAddress.addr_address1 = recOldAddress.addr_address1;
	recNewAddress.addr_address2 = recOldAddress.addr_address2;
	recNewAddress.addr_address3 = recOldAddress.addr_address3;
	recNewAddress.addr_address4 = recOldAddress.addr_address4;
	recNewAddress.addr_address5 = recOldAddress.addr_address5;
	recNewAddress.addr_city = recOldAddress.addr_city;
	recNewAddress.addr_state  = recOldAddress.addr_state;
	recNewAddress.addr_country = recOldAddress.addr_country;
	recNewAddress.addr_postcode = recOldAddress.addr_postcode;
	recNewAddress.SaveChanges();

	var recNewAddressLink = CRM.CreateRecord("address_link");
	recNewAddressLink.adli_addressid = recNewAddress.addr_addressid;
	recNewAddressLink.adli_personid = recNewPerson.pers_personid;
	recNewAddressLink.adli_type = 'Home';
	recNewAddressLink.SaveChanges();

	recAddressLinks.NextRecord();
}

////////////////////////////////////////////////////////
//			stop copying home addresses
////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
//			start copying person phone numbers
////////////////////////////////////////////////////////


strClause = "plink_type in ('Home', 'Mobile') and plink_recordid =" + recOldPerson.pers_personid;
var recPhoneNumber = CRM.FindRecord("phone,vPersonPhone",strClause);

var recNewPhoneNumber;
var recNewPhoneLink;
while (!recPhoneNumber.eof)
{
	recNewPhoneNumber = CRM.CreateRecord("phone");
	recNewPhoneNumber.phon_countrycode = recPhoneNumber.phon_countrycode;
	recNewPhoneNumber.phon_areacode = recPhoneNumber.phon_areacode;
	recNewPhoneNumber.phon_number = recPhoneNumber.phon_number;
	recNewPhoneNumber.SaveChanges();

	recNewPhoneLink = CRM.CreateRecord("phonelink");
	recNewPhoneLink.plink_phoneid = recNewPhoneNumber.phon_phoneid;
	recNewPhoneLink.plink_recordid = recNewPerson.pers_personid;
	recNewPhoneLink.plink_entityid = 13;
	recNewPhoneLink.plink_type = recPhoneNumber.plink_type;
	recNewPhoneLink.SaveChanges();

	recPhoneNumber.NextRecord();
}


////////////////////////////////////////////////////////
//			stop copying person phone numbers
////////////////////////////////////////////////////////

////////////////////////////////////////////////////////
//			start copying person email
////////////////////////////////////////////////////////

strClause = "elink_type = 'Private' and elink_recordid =" + recOldPerson.pers_personid;
var recEmail = CRM.FindRecord("email,vPersonEmail",strClause);
var recNewEmail;
var recNewEmailLink;

while (!recEmail.eof)
{
	recNewEmail = CRM.CreateRecord("email");
	recNewEmail.emai_emailaddress = recEmail.emai_emailaddress;
	recNewEmail.SaveChanges();

	recNewEmailLink = CRM.CreateRecord("emaillink");
	recNewEmailLink.elink_emailid = recNewEmail.Emai_EmailId;
	recNewEmailLink.elink_recordid = recNewPerson.pers_personid;
	recNewEmailLink.elink_entityid = 13;
	recNewEmailLink.elink_type = recEmail.elink_type;
	recNewEmailLink.SaveChanges();

	recEmail.NextRecord();
}
////////////////////////////////////////////////////////
//			stop copying person email
////////////////////////////////////////////////////////

}


if (CRM.Mode == Save)
{
Response.Redirect("/"+sInstallName+"/eware.dll/do?&SID="+Request.QueryString("SID")+"&Mode=1&CLk=T&act=220&key0=2&key2="+recNewPerson.pers_personid)
}
Response.Write(CRM.GetPage());


}

catch(exception)

{
 //Your Error handling code code goes here
  var ErrorContainerBlock = CRM.GetBlock("container");
  var ErrorContentsBlock = CRM.GetBlock("content");
  with(ErrorContainerBlock)
  {
    AddBlock(ErrorContentsBlock);
    DisplayButton(Button_Default) = false;
  }
  with(ErrorContentsBlock)
  {
    contents += "<table class=content><tr><td colspan=2 class=gridhead><b>There has been an error</b></td></tr>";
    contents += "<tr><td class=row1><b>Error Name:</b> </td><td class=row1>"+exception.name+"</td></tr>"
    contents += "<tr><td class=row1><b>Error Number:</b> </td><td class=row1>"+exception.number+"</td></tr>"
    contents += "<tr><td class=row1><b>Error Number:</b> </td><td class=row1>"+(exception.number & 0xFFFF)+"</td></tr>"
    contents += "<tr><td class=row2><b>Error Description:</b></td><td class=row2>"+exception.description+"</td></tr></table>"
  }
  eWare.AddContent(ErrorContainerBlock.Execute());
  Response.Write(CRM.GetPage());
}

finally
{
  //End Section

  Response.Write(EndBody);
  Response.End();
}
%>

Anonymous