ttajic
9/27/2016 - 8:28 AM

Dynamic FlyOut Menu

Dynamic FlyOut Menu

//Create Ribbon FlyOut Button on incident form
//Create Command to Populate Menu and associate it with PopulateMenuReplacementVehicleWorkOrderPartners
//Create Command to Execute Menu Action SendReplacementVehicleMail
//this example also requires XrmServiceToolkit and Process.js to execute, so load them before calling populate and execute commands (enter isNaN function for each additional library: XrmServiceToolkit, Process.js)

var Adacta = Adacta || {};
Adacta.Case = (function ()
{
	var self = this;
	var partners = null;
	var fetchReplacementVehiclePartners = function() {
		if (XrmServiceToolkit === undefined || XrmServiceToolkit === null) {
    			console.log('XrmServiceToolkit is not loaded');
    			return;
		};
		var resultObject;
		var fetchXml = 
	        "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
			"<entity name='ad_partner'>" +
			"<attribute name='ad_partnerid' />" +
			"<attribute name='ad_name' />" +
			"<attribute name='ad_email' />" +
			"<order attribute='ad_name' descending='false' />" +
			"<filter type='and'>" +
			"<condition attribute='ad_email' operator='not-null' />" +
			"</filter>" +
			"<link-entity name='ad_device' from='ad_partnerid' to='ad_partnerid' alias='af'>" +
			"<filter type='and'>" +
			"<condition attribute='ad_deviceid' operator='not-null' />" +
			"</filter>" +
			"</link-entity>" +
			"</entity>" +
			"</fetch>";
	    var resultObject = XrmServiceToolkit.Soap.Fetch(fetchXml);
	    return resultObject;   
	    // Object returned is an array of entities. Access atributes like this: resultObject[0].attributes.ad_customerid.value
	};

	return {
		PopulateMenuReplacementVehicleWorkOrderPartners: function (commandProperties)
		{
		debugger;
			try
			{ // Dynamically return the menu to show - this can be built at run time
				commandProperties["PopulationXML"] = "<Menu Id='ad.incident.CreateReplacementVehiclePartnersFlyOut.Menu'>" +
					"<MenuSection Id='ad.incident.CreateReplacementVehiclePartnersFlyOut.Menu.Section' Sequence='10'>" + "<Controls Id='ad.incident.CreateReplacementVehiclePartnersFlyOut.Menu.Controls'>";
				if (partners == null)
				{
					partners = fetchReplacementVehiclePartners();
				}
				
				var contractButton;
				if (!partners) {
					contractButton = "<Button Id='ad.incident.CreateReplacementVehiclePartnersFlyOut.None' Sequence='30' Command='ad.incident.SendReplacementVehicleMail.Command' LabelText='Ni partnerjev z nadomestnimi vozili' />";
					commandProperties["PopulationXML"] = commandProperties["PopulationXML"] + contractButton;
				}
				else {					
					for (var i = 0; i < partners.length; i++)
					{
						var formId = partners[i].id;
						var name = partners[i].attributes.ad_name.value;
						if (formId)
						{
							contractButton = "<Button Id='ad.incident.CreateReplacementVehiclePartnersFlyOut." + formId + "' Command='ad.incident.SendReplacementVehicleMail.Command' Sequence='30' LabelText='" + name + "' />";
							commandProperties["PopulationXML"] = commandProperties["PopulationXML"] + contractButton;
						}
					}
				}
			}
			catch (e)
			{
				console.log("Adacta.Case.PopulateMenuReplacementVehicleWorkOrderPartners");
				console.log(e);
			}
			
			commandProperties["PopulationXML"] = commandProperties["PopulationXML"] + "</Controls>" +
				"</MenuSection>" +
				"</Menu>";
		},
		SendReplacementVehicleMail: function (commandProperties)
		{
		debugger;
			var commandIdArr = commandProperties.SourceControlId.split(".");
			var formId = commandIdArr[3];
			if (formId == "None") return;
			Xrm.Page.ui.clearFormNotification("SendReplacementVehicleMail");
				
			Xrm.Page.ui.setFormNotification("Pošiljanje naloga za nadomestno vozilo v teku ... ", "INFO", "SendReplacementVehicleMail");
			
			// inputParams: Array of parameters to pass to the Action. Each param object should contain key, value, and type.
			// successCallback: Function accepting 1 argument, which is an array of output params, each containing key, and value.
			// errorCallback: Function accepting 1 argument, which is the string error message. Can be null.
			// Unless the Action is global, you must specify a 'Target' input parameter as EntityReference
			// actionName is required
			var inputParams = new Array(
				{ key: "Target", type: Process.Type.EntityReference, value: { id: Xrm.Page.data.entity.getId(), entityType: "incident" } }
				,{ key: "Partner", type: Process.Type.EntityReference, value:  { id: formId, entityType: "ad_partner" } }
			);

			Process.callAction("ad_sendreplacementvehicleworkorder", inputParams, 
				function (params) {
					//successCallback
					Xrm.Page.ui.setFormNotification("Nalog za nadomestno vozilo uspešno poslan", "INFO", "SendReplacementVehicleMail");
				}, 
				function (e, t) {
					//errorCallback
					console.log(e);
					Xrm.Page.ui.setFormNotification("Napaka: Nalog za nadomestno vozilo ni bil poslan", "ERROR", "SendReplacementVehicleMail");
				} 
			);
		}
	};
})();