mscrm-addons.com logomscrm-addons.com logo
  • Release History
  • Knowledge Base
  • Documentation
  • Contact Support
  • Release History
  • Knowledge Base
  • Documentation
  • Contact Support
home/Knowledge Base/DocumentsCorePack(DCP)/Calling the DocumentsCorePack Dialog for Related Records using a Custom Button 

Calling the DocumentsCorePack Dialog for Related Records using a Custom Button 

569 views 2 Updated on May 27, 2024

The “Create Document” button will always trigger the DocumentsCorePack Dialog to generate a document based on the current record.  If, for example, the button is clicked on an account record, then the resulting document will be based on that account record’s data. 

However, it is also possible to create a separate button that can be used to call the DocumentsCorePack Dialog for related records.  This can be useful in situations where documents should be generated based on a specific lookup field from a given record, such as generating a document based on a contact’s parent account.  This article will demonstrate how that can be done. 

Prerequisites 

Before continuing with this article, it is recommended to read this article on manually adding a “Create Document” button. 

Use Case: Generating a “Welcome Letter” for an account’s primary contact 

In this example, we have an account record with a primary contact.  We would like to add a button that enables users to create documents for this contact directly from the Account form. Specifically, we want to allow for the generation of welcome letter documents for this contact directly from the CommandBar of the account. 

Our example record
Figure 1: Our example record

Creating the custom button

Create a custom button in the main form or main grid of the account table as outlined here.  However, do not incorporate the mentioned custom script.  We will label this button as “CreateRelatedDocument.” 

The custom button as shown in the designer
Figure 2: The custom button as shown in the designer

Set the Action of the button to Run JavaScript.  Under Library, select Add Library.   

Adding a library
Figure 3: Adding a library

Then, in the Add JavaScript Library window, click on New web resource. 

Adding a new web resource
Figure 4: Adding a new web resource

We will need to upload our own JavaScript file that contains the necessary functions to launch the DocumentsCorePack Dialog for a related record.  The required JavaScript can be found below: 

// -- CustomDcpCreateDocumentButtonOnTrigger: Main function triggered by a custom Menu button --
// executionContext: "PrimaryControl"
function CustomDcpCreateDocumentButtonOnTrigger(executionContext)
{
    try
    {
        // code to get primarycontact of current account (could be any other code to get a related record)
        var primaryContactValue = executionContext.getAttribute("primarycontactid").getValue();

        if (primaryContactValue != null && primaryContactValue.length === 1)
        {
            let id = primaryContactValue[0].id;
            let typeName = primaryContactValue[0].entityType;
            let typeCode = 2;

            if (typeName !== "contact")
                throw Error("Unexpected typename: " + typeName);

            // trigger function to open DCP Dialog
            OpenPtmDcpDialogFromCustomScript(id, typeCode, typeName);
        }
    }
    catch (e)
    {
        alert("Error in CustomDcpCreateDocumentButtonOnTrigger: " + e?.message);
    }
}



// -- OpenPtmDcpDialogFromCustomScript: Function to open the DCP Dialog --
// recordId: Target RecordId (e.g. "4cf2ef9b-fb2c-ed11-9db0-000d3a95950b")
// typeCode: Target EntityTypeCode (e.g. 2)
// typeName: Target EntityTypeName (e.g. "contact")
function OpenPtmDcpDialogFromCustomScript(recordId, typeCode, typeName)
{
    const openDialog = function ()
    {
        try
        {
            window.top.ptm_openLookupDlg(null, [recordId], typeCode, 1, typeName, true);
        }
        catch (e)
        {
            alert("Error in OpenPtmDCPDialogFromCustomScript_openDialog: " + e?.message);
        }
    }


    try
    {
        if (typeof (recordId) !== "string" || recordId === "")
            throw new Error("inRecordId is empty");

        var typofEtc = typeof (typeCode);
        if (typofEtc !== "number")
        {
            if (typofEtc !== "string" || isNaN(typeCode))
                throw new Error("inTypeCode is no number");
            else
                typeCode = parseInt(typeCode);
        }

        if (typeof (typeName) !== "string" || typeName === "")
            throw new Error("inTypeName is empty");

        const webresource = "/webresources/ptm_globalambutton.min.js"

        if (window.top.document.querySelector('script[src$="' + webresource + '"]') != null)
            openDialog();
        else
        {
            const dialogScriptUrl = Xrm.Page.context.getClientUrl() + webresource;
            let dcpScript = document.createElement("script");
            dcpScript.setAttribute("type", "text/javascript");
            dcpScript.setAttribute("src", dialogScriptUrl);
            dcpScript.onload = function ()
            {
                openDialog();
            }
            window.top.document.getElementsByTagName("head")[0].appendChild(dcpScript);
        }
    }
    catch (e)
    {
        alert("Error in OpenPtmDCPDialogFromCustomScript: " + e?.message);
    }
}

Note: If this code is used for a different related record, the “primarycontactid” and “contact” values should be changed to match the lookup and entity respectively. 

After saving the JavaScript as a .js file, upload the file as shown below. 

Uploading the JavaScript file
Figure 5: Uploading the JavaScript file

Save and publish the file.  Then, select the JavaScript file and add it. 

Adding the JavaScript library
Figure 6: Adding the JavaScript library

Once added, write in “CustomDcpCreateDocumentButtonOnTrigger” for the function name, and add “Primary Control” as a parameter. 

Figure 7: Writing in the function name and parameter

Save and publish your changes.  Navigate to an account record.  The newly created button should be displayed on the toolbar.

The custom button on the toolbar
Figure 8: The custom button on the toolbar

When clicking the button, you will notice that the listed templates are that of the contact entity, not of the account entity. 

A list of contact templates being displayed
Figure 9: A list of contact templates being displayed

We can thus select the “Contact Welcome Template” and generate a document for our primary contact. 

The generated welcome template
Figure 10: The generated welcome template

That’s it! We appreciate your feedback. Please share your thoughts by sending an email to support@mscrm-addons.com. 

Was this helpful?

2 Yes  No
Related Articles
  • Deploying DocumentsCorePack: Setup, Automation, and Real Business Impact in Dynamics365
  • Introducing the DocumentsCorePack Template Designer Office Add-In (Beta)
  • 3 Ways DocumentsCorePack Transforms Proposal, Quote, and Contract Creation
  • DeepSign for DocumentsCorePack 
  • Generate and send a document for e-signing using DeepSign
  • How to prepare a document for DeepSign
Latest Articles
  • What Happens After You Deploy AttachmentExtractor: Storage & Cost Savings in Dynamics 365
  • Deploying DocumentsCorePack: Setup, Automation, and Real Business Impact in Dynamics365
  • Introducing the DocumentsCorePack Template Designer Office Add-In (Beta)
  • How to Eliminate Excess Storage Costs in Microsoft Dynamics 365 Without Losing Access to Emails and Attachments
  • 3 Ways DocumentsCorePack Transforms Proposal, Quote, and Contract Creation
Popular Articles
  • DocumentsCorePack Template Designer – Getting Started
  • Step-by-Step: How to configure a One-Click-Action
  • Application Access for Dynamics 365
  • How to find your environment’s unique name in Microsoft Dynamics 365
  • How licenses are counted
Top Rated Articles
  • “Undefined”-labels in Chrome and Edge 114 and higher
  • Important information for Dynamics 365 online customers using DocumentsCorePack and/or AttachmentExtractor
  • How to activate the DocumentsCorePack Connector for PowerApps & Microsoft Flow
  • How to insert Condition Fields in DocumentsCorePack Templates designer
  • How licenses are counted
Categories
  • *News and General Infos* 64
  • Webinars 45
  • Template Designer(DCP 196
  • DocumentsCorePack(DCP 258
  • TelephoneIntegration (TI 65
  • AttachmentExtractor (AE 75
  • PowerSearch (PS 50
  • ActivityTools (AT 61
  • SmartBar (SB 54
  • GroupCalendar (GC 47
Our Vision

“We see it as our purpose to provide products that simplify and speed up our customers’ Microsoft Dynamics 365 experience.”

Knowledgebase
LogIn
mscrm-addons.com
  • Products
  • Online Shop
  • Downloads
  • My Account
About Us
  • About Us
  • Case Studies
  • Newsletter
  • Partner Program
  • Contact
Support
  • Support
  • Terms & Conditions
  • Documentation
  • Webinars
  • Legal Documents
  • Impressum
  • © 2025 www.mscrm-addons.com. All Rights Reserved.