mscrm-addons.com - Blog

News.mscrm-addons.com Blog

rss

Hello and welcome to our blog! What can we do for you? Are you looking for further technical information or step-by-step instructions to our products? Or would you like to read the latest news on mscrm-addons? Please feel free to browse our blog for detailed information and to share our posts!


Execute AutoMerge for MS CRM 2011 and 2013 via JavaScript

This article explains how to execute the Create Document-step of AutoMerge manually in JavaScript. This allows to include this functionality in custom - applications (e.g. create documents via a CRM-button-click and save document to SharePoint )

This article covers both:

1) Calling the plugin

2) Handling the result

Attached, you can find the full library for download.

How to start the Create-Process

The Create Document-part of AutoMerge is installed as plugin in CRM. To trigger this plugin, you have to execute a Retrieve Multiple-request on the MSCRM-ADDONS.com AMTrigger (logical name: ptm_mscrmaddonscomamtrigger)-entity. It is important that the result-attributes-list contains the ptm_FullXML-attribute. It will contain information returned by the plugin.
In addition, the request must include two filter conditions.
The first condition needs to ensure that solely the record with the name cmd_automerge will be returned.
The second one is a pseudo-condition which is used to pass information to the plugin. This information has to be in XML-format and has to look like this:


   false
   Create
   069E5412-84F6-E111-977B-00155DC8AE09
   account
   00BDBD7D-65DD-E111-BEC0-00155DC8AE09
   docx
   1
  

Description of parameter
•    EnableSingleDebugging: Here you can define if this step should be debugged. Options: true, or false. It is recommended to always define it as false. If debugging is needed, activate it via the AutoMerge configuration.
•    MessageType: Must be Create
•    PrimaryEntityId: The Id of the record for which the merge process should be executed.
•    PrimaryEntityLogicalName: The logical name of the starting entity.
•    TemplateId: The Id of the template which should be used for the merge process.
•    SaveAs: Defines the file format of the generated document (docx, doc, pdf,…).
•    SaveLocation: There are two options. 0: the document will be saved only in the MSCRM-ADDONS.com User/Temp Settings-entity. 1: the document will be saved in the MSCRM-ADDONS.com User/Temp Settings-entity and in File Share or SharePoint (depending on your setup of the DocumentsCorePack server configuration).
•    PrintTo: If the generated document should be printed as well, define the path of the network printer here.

Example
The Retrieve Multiple-request looks like the following:

var filter = "";
filter += "false";
filter += "Create";
filter += " 069E5412-84F6-E111-977B-00155DC8AE09 ";
filter += " account ";
filter += "00BDBD7D-65DD-E111-BEC0-00155DC8AE09";
filter += "docx";
filter += "1";
filter += " ";
filter += "";

$ptm.retrieveMultiple("ptm_mscrmaddonscomamtriggerSet", "?$filter=(ptm_name eq 'cmd_automerge') or (ptm_FullXML eq '" + filter + "')&$select=ptm_FullXML", CreateDocumentFinished, CreateDocumentError);

CreateDoucmentFinished and CreateDocumentError specify the CallBack-function after executing the request.

Process the results

After the request is executed, you will receive a result from the plugin.

Successful result
A successful result message looks like the following:
„AM_6_17<>valid|533d31c2-72da-e311-a232-00155dc80d02|outgoingBytes:63510|”
•     “AM_” is static text
•    “6_17” is the plugin version
•    “<>valid” is static text
•    “533d31c2-72da-e311-a232-00155dc80d02” is the ID of the generated “MSCRM-ADDONS.com User/Temp Settings”-record. It contains the generated document in the “Notes”-section. If the document is stored in SharePoint or File share, you can find the full path to the document in the “Value”-attribute.

Unsuccessful result
An unsuccessful result message looks like this:
“ERROR:|CHECKLICENSE|SUCCEEDED|new AutoMerge|StartMerge|EmptyGUID||ErrorMerge|BeforeCreateDoc
AfterCreateDoc”

or like this:
"AM_13<>validERROR"

Example
Below you can find a function which handles the result. If the process was successful, the documentId will contain the Id of the created MSCRM-ADDONS.com User/Temp Settings-record. If not, an error message will be shown.

function CreateDocumentFinished(data, textStatus, XmlHttpRequest) {
    if (data != null && data.length >= 1 && data[0].ptm_FullXML != undefined) {
        var response = data[0].ptm_FullXML;
        var indexOfPipe = response.indexOf("|");
        var documentId = "";
 
        if (indexOfPipe >= 0 && (response.indexOf("valid") != -1 || response.indexOf("trial") != -1) && response.toLowerCase().indexOf("error") == -1) {
            documentId = response.substr(indexOfPipe + 1);
 
            indexOfPipe = documentId.indexOf("|");
 
            if (indexOfPipe != -1) {
                documentId = documentId.substr(0, indexOfPipe);
            }
        }
        else {
            alert("ERROR: While creating document");
        }
    }
    else {
        alert("ERROR: While creating document");
    }
}

 

Created MSCRM-ADDONS.com User/Temp Settings-record:

Figure 1: MSCRM-ADDONS.com User/Temp Settings

1: ID retrieved from the plugin. This ID references the mscrm-addons-temp record with the doucment attached
2: Link to the document on SharePoint: needed if you want to e.g. open the doucment from SharePoint
3: Attached document: Needed to process the temp-document

 

JavaScript-Example

Attached you can find the whole JavaScript-example. It uses the ptm_GeneralJSFunctions.js and ptm_jquery1.4.1.min web resources to trigger the plugin. They were imported during the installation of DocumentsCorePack server.
If you do not want to use this library, of course you can trigger the plugin with a standard HTTP request.

TriggerAM-Plugin.zip (1.29 kb)

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




Comments are closed.