User Review
( votes)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.
Hope it helps..