使用JS获取特定实体 [英] Using JS to get a particular entity

查看:61
本文介绍了使用JS获取特定实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为RegisteredUser和RegisteredApplication的实体.RegisteredUser具有一个称为new_applicationid的必填字段,该字段使用针对RegisteredApplication实体的Lookup进行填充.

I have entities called RegisteredUser and RegisteredApplication. RegisteredUser has a required field called new_applicationid that is populated using a Lookup that targets the RegisteredApplication entity.

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

So, when I'm creating a new user using the form in CRM I have to click on the lookup, find the relevant application and then Click on OK.

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

My problem is: there is only one RegisteredApplication at the moment and I would like to have the Lookup prepopulated when the form loads.

我想我正在寻找一些类似的东西

I guess I'm looking for something along the lines of

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
}

有人可以建议我如何处理这种事情吗?

Can anyone suggest how I might approach something like this?

推荐答案

类似的方法会起作用

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的逻辑.

The nice thing about using OData here is that you could implement logic to choose a particular RegisteredApplication when/if you have more than one.

查看 OData查询设计器我将让您实施足够的null/undefined检查.

I'll leave you to implement sufficient null/undefined checking.

这篇关于使用JS获取特定实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆