简体   繁体   中英

How to return data from CRM 2011 javascript odata function?

This seems silly that I haven't been able to accomplish this. I'm using the common code found on the web to do an odata query. The problem is the results stay in getFieldData(retrieveReq) routine. I don't want to immediately set a field on the current form. How can I get my values out of it so the data can be used in other javascript functions? Global variable would be good but nothing I've tried has worked. The below code displays "x".

var var1 = "x"; odataquery(); alert(var1);

The example given here has two alerts that display the data. How can Id and Name get outside of that function to be useful?

Edit1: Below is the main part of the routine that calls getFieldData(this). I want to use OwnerBUID and OwnerBUName in other javascript functions.

    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", odataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    retrieveReq.onreadystatechange = function () {
        getFieldData(this);
    };
    retrieveReq.send();

function getFieldData(retrieveReq) {
    if (retrieveReq.readyState == 4 && retrieveReq.status == 200) {  
        // 4=request complete, 200=OK
        var retrieved = this.parent.JSON.parse(retrieveReq.responseText).d;
        var retrievedValue = retrieved.results[0].BusinessUnitId;
        OwnerBUID = retrievedValue.Id;
        OwnerBUName = retrievedValue.Name;
    }
}

I guess you want to put the data as the return value of a javascript function. You could do this: var returnedData = function getFieldData(retrieveReq) { ... return data; }

BTW, you could consider to use JayData, Breeze and Datajs sources code packages in your client application. They implement the low level APIs for consuming an odata service using javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM