Показать сообщение отдельно
Старый 28.04.2012, 08:23   #6  
kh_heckfy is offline
kh_heckfy
Участник
 
139 / 14 (1) ++
Регистрация: 30.03.2012
Адрес: Новосибирск
Все, победил
Замена лукапа на комбобокс для MS CRM 2011 рабочая версия

X++:
ConvertFieldToPicklist = function (lookupControl, lookupAttribute, lookupEntityName, lookupEntityPrimaryAttribute, fieldToReplace) {

    var lookupEntityPrimaryKeyField = lookupEntityName + "id";

    var RetrieveLookupValues = function () {
        var fetchXml =
			"<fetch mapping='logical'>" +
				"<entity name='" + lookupEntityName + "'>" +
					"<attribute name='" + lookupEntityPrimaryKeyField + "' />" +
					"<attribute name='" + lookupEntityPrimaryAttribute + "' />" +
					"<order attribute='" + lookupEntityPrimaryAttribute + "' />" +
				"</entity>" +
			"</fetch>";

        var lookupValues = [];

        var fetchResult = CrmServiceToolkit.Fetch(fetchXml);
        if (fetchResult !== null) {
            for (i = 0; i < fetchResult.length; i++) {
                var item = new Object();
                item[lookupEntityPrimaryKeyField] = fetchResult[i].getValue(lookupEntityPrimaryKeyField);
                item[lookupEntityPrimaryAttribute] = fetchResult[i].getValue(lookupEntityPrimaryAttribute);

                lookupValues[i] = item;
            }
        }
        return lookupValues;
    };

    var PopulatePicklist = function (picklistControl, lookupValues) {
        picklistControl.options.length = 0;

        // Add blank option item 
        var option = document.createElement("option");
        option.value = "";
        option.innerText = "";
        picklistControl.appendChild(option);

        // Add all options from code table (lookup entity) 
        if (typeof lookupValues !== "undefined") {
            for (var i = 0; i < lookupValues.length; i++) {
                option = document.createElement("option");
                option.value = lookupValues[i][lookupEntityPrimaryKeyField];
                option.innerText = lookupValues[i][lookupEntityPrimaryAttribute];

                picklistControl.appendChild(option);
            }
        }
    };
    
 	BuildLookupFieldValue = function(id, typename, name) {
		if (id === null || id === "") {
			return null;
        }

		var dataValues = [];
		var dataValue = {};
		dataValue.id = id;
		dataValue.typename = typename;
		dataValue.name = name;
		dataValues[0] = dataValue;

		return dataValues;
    }

	UpdateLookupWhenPicklistChange = function(picklistControl, lookupControl) {
		var selectedOption = picklistControl.options[picklistControl.selectedIndex];
		if (selectedOption.value === "")
			lookupControl.DataValue = null;
		else
			lookupControl.DataValue = BuildLookupFieldValue(selectedOption.value, lookupEntityName, selectedOption.text);
		
		Xrm.Page.getAttribute(fieldToReplace).fireOnChange();
		//lookupControl.FireOnChange();
	};

    (function Convert() {
        var picklistControl = document.createElement("select");
        var replaceFieldControl = document.getElementById(fieldToReplace);
        picklistControl.id = replaceFieldControl.id;
        picklistControl.name = replaceFieldControl.name;
        picklistControl.tabIndex = replaceFieldControl.tabIndex;
        picklistControl.req = replaceFieldControl.req;
        picklistControl.Disabled = replaceFieldControl.Disabled;
        picklistControl.className = "ms-crm-selectBox";
        picklistControl.onchange = function () { UpdateLookupWhenPicklistChange(picklistControl, lookupControl); };

        var lookupValues = RetrieveLookupValues();
        PopulatePicklist(picklistControl, lookupValues);

        if (lookupAttribute.getValue() !== null) {
            picklistControl.DataValue = lookupAttribute.getValue()[0].id;
        }

        var lookupControlCell = document.getElementById(fieldToReplace + "_d");
        lookupControlCell.childNodes[0].style.display = 'none';
        //lookupControlCell.removeChild(lookupControlCell.childNodes[0]);
        lookupControlCell.appendChild(picklistControl);
        return picklistControl;
    })();
}