Calling SetWordTemplate from custom ribbon button in Dynamics 365


Check the other articles of this series

Let us continue our previous post and this time instead of calling SetWordTemplate action / request from a workflow, we will call it using a custom ribbon button.

If we try calling the action directly using Xrm.WebApi.online.execute
method from a custom ribbon button, we will get the below error “Resource not found for the segment ‘SetWordTempalte’

Source Code: –


function CallAction()
{

var parameters = {};

var selectedTemplate = {};
selectedTemplate["@odata.type"] = "Microsoft.Dynamics.CRM.documenttemplate";
selectedTemplate["documenttemplateid@odata.bind"] = "/documenttemplates(4853247F-AD72-EA11-A811-000D3A31EEC)";

var target = {};
target["@odata.type"] = "Microsoft.Dynamics.CRM.lead";
target["leadid@odata.bind"] = "/leads(2fa01d2d-7332-e611-80e5-5065f38b31c1)";

parameters.SelectedTemplate = selectedTemplate;
parameters.Target = target;
var setWordTemplateRequest = {
SelectedTemplate: parameters.SelectedTemplate,
Target: parameters.Target,

getMetadata: function() {
return {
boundParameter: null,
parameterTypes: {
"SelectedTemplate": {
"typeName": "mscrm.documenttemplate",
"structuralProperty": 5
},
"Target": {
"typeName": "mscrm.lead",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "WinOpportunity"
};
}
};

Xrm.WebApi.online.execute(setWordTemplateRequest).then(
function success(result) {
if (result.ok) {

Xrm.Utility.alertDialog("Success");
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);

}

As a workaround, we can define a custom action and add the Perform Action step with SetWordTemplate step to it.

Source Code: –


function CallSetWordTemplateAction(primaryControl) {

var formContext = primaryControl;
var leadid = formContext.data.entity.getId(); 

var target = {};
target.entityType = "lead";
target.id = leadid; 

var req = {};
req.entity = target;
req.getMetadata = function () {
return {
boundParameter: "entity",
parameterTypes: {
"entity": {
typeName: "mscrm.lead",
structuralProperty: 5
}
},
operationType: 0,
operationName: "pcfpre_CreateDocumentAction"
};
};

Xrm.WebApi.online.execute(req).then(
function (data) {
var e = data; 

},
function (error) { 

var errMsg = error.message;
}
);
}

Another option is to use the Process.js library

The Ribbon Button

Ribbon button definition

Dynamics 365 for Phone \ Tablet

 

Source Code: –


function createWordDocument(primaryControl) {

var formContext = primaryControl;

var leadid = formContext.data.entity.getId();

Process.callAction("SetWordTemplate",
[
{
key: "Target",
type: Process.Type.EntityReference,
value: new Process.EntityReference("lead", leadid)
},
{
key: "SelectedTemplate",
type: Process.Type.EntityReference,
value: new Process.EntityReference("documenttemplate", "{4853247F-AD72-EA11-A811-000D3A31EEC8}")
}
],

function()
{
alert('Document created');
},

function (error, trace)
{

alert(error);
}
);
}

It internally makes the Soap call to SetWordTemplate Request.

Although we are able to attach the doc to notes, from mobile if we try to open it we will get the below error message

Pic2

To get the document upload to work seamlessly either we need to use Windows 10 based tablets or we need to use out of the box SharePoint integration.

Hope it helps..

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

3 thoughts on “Calling SetWordTemplate from custom ribbon button in Dynamics 365”

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.