Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 46379

Trying to make API call from Grid with Javascript

$
0
0

I am needing to update a fields on an editable grid. I want to select the records on the grid, then press a button that executes code to update the field on the form of all the selected records. Here is my code so far: 

function getIds(selectedItems) {
	for (var item in selectedItems) {
        if (selectedItems.hasOwnProperty(item)) {
            fetchData(selectedItems[item].Id);
        }	
   	} 
}



function fetchData(appointment_id) {
	Xrm.WebApi.retrieveRecord("appointment", appointment_id, "?$select=_regardingobjectid_value").then(
    function success(results) {
        var clientId = results["_regardingobjectid_value"];
		getCarOne(clientId);
	});
}

function getCarOne(clientId) {
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/new_cars?$select=_new_carOne_value,&$filter=new_status eq 100000001 and  _new_client_value eq" + clientId, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
            var results = JSON.parse(this.response);
            for (var i = 0; i < results.value.length; i++) {
                var _new_carOne_value = results.value[i]["_new_carOne_value"];
                var _new_carOne_value_formatted = results.value[i]["_new_carOne_value@OData.Community.Display.V1.FormattedValue"];
                var _new_carOne_value_lookuplogicalname = results.value[i]["_new_carOne_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
		
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}


What I am doing is:
1) selecting the records(all appointments) and getting the appointment record ID
2) using WebAPI to get clientID, then passing clientID into a new API call
3) using the HTTP request to get all car records with a certain status for each client. This is where the break happens. the req.send(); function doesn't seem to work from the grid
though it works just fine if I have the form open.
4) I have not got to this step yet, but the once I get the fields I need, I want to use that information to build a string to update a field with.

Viewing all articles
Browse latest Browse all 46379

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>