Moving files into the Library using the serverside COM API.

2 minute read time.

A customer had a requirement to be able to automate the inclusion of existing files into the CRM library structure. The scenario is that a file is created by another business process e.g. "Northwind Order Processing". That file then has to be added to CRM.

This is not the first time I have written about the library and this article draws on some of the research that I have carried out previously:

The following is assumed:

  • The external system has an 'event' that can be used as the file is created.
  • The filename is known e.g. ABC123456.pdf
  • The file is created in a known folder e.g. c:\otherapp\
  • The external system and Sage CRM are integrated. For example, the file is created for a record in the external system that has a corresponding record in Sage CRM. The external file that needs to be imported might be created for the company "Alfred's Futterkiste" in the Northwind database that is linked to the company record in Sage CRM for "Alfred's Futterkiste" by the Sage CRM foreign field comp_northwindcompanyid = "ALFKI".

Note:

This is a simple proof of concept. I have written this using javascript but the technique could be used by any language that can use COM.

The tasks we have to do are to

  1. Find the library path
  2. Find the company specific library.
  3. Copy the file into library folders
  4. Add to the document library table and link to correct company/person/etc
  5. Add a communication

The code carries out an external logon which is the type of logon that a script run from a xxx.js file on the desktop might use.

Example Code

var username = "Admin";
var password = "";
var CRM = new ActiveXObject("eWare.CRM");
CRM.FastLogon = 3;  //this prevents the meta data from loading.
CRM.Logon(username,password);
 
var recUser = CRM.FindRecord("user","user_logon='"+username+"'");
 
var strNorthwindCompanyID = "ALFKI";
var strfilename = "abc123456.txt";
var strpathname = "c:\\otherapp\\";
var strsourcefullpath = strpathname+strfilename;
 
//get document library path
var recSysParams = CRM.FindRecord("custom_sysparams","parm_name='docstore'")
var strCRMLibraryRoot = recSysParams.parm_value;
 
var recCompany = CRM.FindRecord("company","comp_northwindcustomerid='"+strNorthwindCompanyID+"'");
if(!recCompany.eof)
{
var strCompPath = recCompany.comp_librarydir;
var strdestinationfullpath = strCRMLibraryRoot +strCompPath +"\\"+strfilename;
var myObject = new ActiveXObject("Scripting.FileSystemObject");
 
//create folder if it doesn't exist
var arrCompPath = strCompPath.split("\\");
if(!myObject.FolderExists(strCRMLibraryRoot +arrCompPath[0]))
{
myObject.CreateFolder (strCRMLibraryRoot +arrCompPath[0])
}
 
if(!myObject.FolderExists(strCRMLibraryRoot +strCompPath))
{
myObject.CreateFolder (strCRMLibraryRoot+strCompPath)
}
 
//copy file
//syntax filesystemobject.CopyFile(source, destination, overwrite)
myObject.CopyFile (strsourcefullpath, strdestinationfullpath);
 
/////////create communication/////////////////
var myDate = new Date();
var recCommunication = CRM.CreateRecord("communication");
recCommunication.comm_action = "LetterOut";
recCommunication.comm_type = "Task";
recCommunication.comm_channelid = recUser.user_primarychannelid;
recCommunication.comm_status = "Complete";
recCommunication.comm_priority = "Normal";
recCommunication.comm_datetime = myDate.getVarDate();
recCommunication.comm_note = "details go here"
recCommunication.SaveChanges();
 
var recCommLink = CRM.CreateRecord("comm_link");
recCommLink.cmli_comm_userid = recUser.user_userid;
recCommLink.cmli_comm_communicationid = recCommunication.comm_communicationid;
recCommLink.cmli_comm_personid = recCompany.comp_primarypersonid;
recCommLink.cmli_comm_companyid = recCompany.comp_companyid;
recCommLink.SaveChanges();
 
//////////////Create Library Record
var recLibrary = CRM.CreateRecord("library");
recLibrary.libr_companyid= recCompany.comp_companyid;
recLibrary.libr_personid= recCompany.comp_primarypersonid;
recLibrary.libr_userid= recUser.user_userid;
recLibrary.libr_channelid= recUser.user_primarychannelid;
recLibrary.libr_type= "Letter";
recLibrary.libr_category= "Sales";
recLibrary.libr_filepath= recCompany.comp_librarydir;
recLibrary.libr_filename= strfilename;
recLibrary.libr_note= "upload";
recLibrary.libr_status = "draft";
recLibrary.libr_entity= "company";
 
recLibrary.SaveChanges();