使用JS获取特定实体

时间:2011-10-17 13:44:28

标签: dynamics-crm-2011

我有名为RegisteredUser和RegisteredApplication的实体。 RegisteredUser有一个名为new_applicationid的必填字段,该字段使用面向RegisteredApplication实体的Lookup填充。

因此,当我使用CRM中的表单创建新用户时,我必须单击查找,找到相关的应用程序,然后单击“确定”。

我的问题是:目前只有一个RegisteredApplication,我希望在表单加载时预先填充Lookup。

我想我正在寻找符合

的内容
function FormLoad()
{
    var app = GetApplications()[0];

    //Set a lookup value
    var value = new Array();
    value[0] = new Object();
    value[0].id = app.id;                               // is this right?
    value[0].name = app.name;                           // is this right?
    value[0].entityType = "new_registeredapplication";  // is this right?

    Xrm.Page.getAttribute("new_applicationid").setValue(value);
}

function GetApplications()
{
    // what do I need to do in here to get a list of 
    // all the registered applications
}

任何人都可以建议我如何处理这样的事情吗?

1 个答案:

答案 0 :(得分:2)

这样的事情会起作用

function FormLoad() {
    //could use OData here
    var url = Xrm.Page.context.getServerUrl() + '/XRMServices/2011/organizationData.svc/RegisteredApplicationSet';
    var request = new XMLHttpRequest();
    request.open("GET", serverUrl, true);
    request.setRequestHeader("Accept", "application/json");
    request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    request.onreadystatechange = function () {
        RequestComplete(request);
    }
    request.send();
}

function RequestComplete(request) {
    if (request.readyState == 4 && request.status == 200) {
        //parse out first result from returned results
        var results = JSON.parse(request.responseText);
        //we could use logic to select partic result here, for now just choose first one
        var selectedApp = results.d.results[0];

        //Set a lookup value
        var value = new Array();
        value[0] = new Object();
        value[0].id = selectedApp.new_registeredapplicationid;//guid of entity here                             
        value[0].name = selectedApp.new_name;//display property here
        value[0].type = <objecttypecode of entity here>;
        Xrm.Page.getAttribute("new_applicationid").setValue(value);
    }
}

在这里使用OData的好处是你可以实现逻辑来选择特定的RegisteredApplication当/如果你有多个。

查看OData Query DesignerMSDN article on using OData with MSCRM

我将让你实现足够的null / undefined检查。