Adding Document Filing and Mail Merge functionality to Custom Entities

By | August 31, 2012

SP2 patch for sage CRM version 7.2 comes with lots of functionalities we have been waiting for quite a long time. There are lots of enhancements done on document merging and filing functionality. Now the mail merge process is cross-browser compatible, which means you can now perform document merging with Sage CRM entities on browsers like Mozilla Firefox, Google Chrome, or Apple Safari etc. By using the merging feature we can create the predefined rich content templates to generate the letters on the fly.
The cool thing about this is this feature can also be implemented for the custom entities we generate as a part of day to day customization work. We have to select “Has Communications” and ‘Has Library’ while creating a these entities as a prerequisite to have document and mail merge feature against them. Once you have these set you can follow below given steps to implement these features on your custom entity.

  1. Create the Mail Merge View for the Custom Entity as below:

 a)      Go to the Administration | Customization | Entity | Views | New View.
CREATE VIEW vMailMergeJob
AS
SELECT * FROM Job LEFT JOIN Company ON job_CompanyId = Comp_CompanyId LEFT JOIN Person ON job_PersonId = Pers_PersonId LEFT JOIN Opportunity ON job_opportunityId = Oppo_OpportunityId LEFT JOIN Orders ON job_OrderId = Orde_OrderQuoteId LEFT JOIN Quotes ON job_QuoteId = Quot_OrderQuoteId LEFT JOIN Lead ON job_LeadId = Lead_LeadId LEFT JOIN Cases ON job_CaseId = Case_CaseId WHERE job_Deleted IS NULL

Note: In the above view add your custom entity name instead of “Job
b)      Click on “Save” button.
c)       Perform Metadata refresh.
As I said you need to have those checkboxes checked, there is a page Library.asp already generated for an entity.  Some script needs to be added to that page as follows.
<!– #include file =”..\crmwizard.js” –>
<!– #include file =”..\crmconst.js” –>
<%
try
{
 //’Initilization
 var sURL=new String( Request.ServerVariables(“URL”)() + “?” + Request.QueryString );
 var PersonKey = ”;
 var CompanyKey = ”;
 var OpportinityKey = ”;
 var HasCommunication = ”;
 var sLibraryId=”0″;
 var sCompanyId=”0″;
 var sPersonId=”0″;
 var sOpportinityId=”0″;
 var sUserAgent=””;
 var sBrowserId =””;
 
 //’Get the value
 sUserAgent = new String(Request.ServerVariables(“HTTP_USER_AGENT”));
 if(sUserAgent==”” || sUserAgent==”null” || sUserAgent==”Undefined”)sUserAgent=””;
 
 if(sUserAgent!=””)
   sBrowserId = sUserAgent.indexOf(“MSIE”);
 //’Get the container
 Container=CRM.GetBlock(“container”);
 List=CRM.GetBlock(“LibraryList”);
 List.prevURL=sURL;
 //’Get the library id
 sLibraryId = new String(Request.Querystring(“job_JobID”));
 if(sLibraryId==”” || sLibraryId==”null” || sLibraryId==”Undefined”)
 {
  sLibraryId = new String(Request.Querystring(“Key58″));
  if(sLibraryId==”” || sLibraryId==”null” || sLibraryId==”Undefined”)sLibraryId=”0″;
 }
 //’To set the context of library
 CRM.SetContext(“Job”, sLibraryId);
 if (sLibraryId!= “0” && ( true || true || true) )
 {
  //’Get the current library record
  sLibraryRecord = CRM.FindRecord(“Job”, “job_JobID=” + sLibraryId);
  //’get the current company id for the Job
  if(true) 
  {
   sCompanyId = new String(“&Key” + iKey_CompanyId + “=” + sLibraryRecord.Item(“job_CompanyId”)); 
   if(sCompanyId==”” || sCompanyId==”null” || sCompanyId==”Undefined”)sCompanyId=”0″;
  }
  
  //’get the current person id for the Job
  if(true) 
  {
   sPersonId = new String(“&Key” + iKey_PersonId + “=” + sLibraryRecord.Item(“job_PersonId”));
   if(sPersonId==”” || sPersonId==”null” || sPersonId==”Undefined”)sPersonId=”0″; 
  }
  //’get the current opportunity id for the Job
  if(true) 
  {
   sOpportinityId = new String(“&Key” + iKey_OpportunityId + “=” + sLibraryRecord.Item(“job_OpportunityId”));
   if(sOpportinityId==”” || sOpportinityId==”null” || sOpportinityId==”Undefined”)sOpportinityId=”0″; 
  }
 }
 if(true)
 {
    HasCommunication = “&MakeCommunicationYN=Y”;
 }
 Container.AddBlock(List);
 
 /*
 //’for adding the button
 Container.AddButton(CRM.Button(“New”, “new.gif”, CRM.URL(343)+”&Key-1=”+iKey_CustomEntity+sPersonId+sCompanyId+sOpportinityId+HasCommunication+”&PrevCustomURL=”+List.prevURL+”&E=Job”));
 Container.DisplayButton(1)=false;
 */
 //’url for the New Button
 var sNewButtonUrl= CRM.URL(343)+”&Key-1=”+iKey_CustomEntity+sPersonId+sCompanyId+sOpportinityId+HasCommunication+”&PrevCustomURL=”+List.prevURL+”&E=Job”;
 
 if(sBrowserId.toString() == “-1” )
 {                
  //’The “FILEUPLOAD” string below is NOT  a CAPTION – it is a constant to indicate to the COM object that a FILEUPLOAD button is expected
  Container.AddButton(CRM.Button(“FILEUPLOAD”, “FileUpload.gif”,sNewButtonUrl));
 }
 else
 {
  Container.AddButton(CRM.Button(“FileUpload”, “FileUpload.gif”,sNewButtonUrl));
 }
 Container.DisplayButton(1)=false;
 
 if(true)
 {
  //’url for the Mail Merge button
  sMailMergeUrl = CRM.URL(542);
  Container.AddButton(CRM.Button(“ButtonMailMerge”, “MailMerge.gif”, sMailMergeUrl));
 }
 //’if Library id is not blank
 if(sLibraryId != “”)
 {
   CRM.AddContent(Container.Execute(“Libr_JobId=”+sLibraryId));
 }
 CRM.GetCustomEntityTopFrame(“Job”);
 
}
catch(e)
{
 Response.Write(“Error=”+e.message);
}
Response.Write(CRM.GetPage());
%>
Implementation of above two steps should get you started with using the Document filing and merging features for your custom entity (Refer screenshot below for Set Mail merge button click.).