开发者

Using JS to get a particular entity

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.

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.

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?


Something like this would work

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);
    }
}

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.

Check out the OData Query Designer And the MSDN article on using OData with MSCRM

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜