Retrieve and RetrieveMultiple JavaScript using Xrm.WebApi

Leave a comment

Let’s look at how to use Retrieve and RetrieveMultiple using JavaScript and the Xrm.WebApi.

Consider the scenario where we have an account:

We can run this code in browser debugger. Hit F12 and let’s run this code:

[sourcecode language=”JavaScript”] var Entity = "account";
var Id = "d2720425-53ad-43c3-b9dc-e7452b80ae13";
var Select = "?$select=name";

Xrm.WebApi.retrieveRecord(Entity, Id, Select).then(
function success(result) {
console.log("Success: " + result.name);
},
function (error) {
console.log(error.message);
}
);
[/sourcecode]

We get the output:

And if we want to do a RetrieveMultiple:

[sourcecode] var Entity = "account";
var Select = "?$select=name";
var Filter = "&$filter=name eq ‘New Account Test’";

Xrm.WebApi.retrieveMultipleRecords(Entity, Select + Filter).then(
function success(result) {
for (var i = 0; i < result.entities.length; i++) {
console.log(result.entities[i]);
}

// If you’re trying to get the first one
if (result != null) {
console.log(result.entities[0].name);
}
},
function (error) {
console.log(error.message);
}
);
[/sourcecode]

We get:

 

Carl de Souza
Keep learning. Keep building.

Explore AI, agents & Microsoft technology.

I share practical ideas, tutorials, and videos about AI, AI agents, Microsoft technologies, and the Power Platform.

Subscribe on YouTube →
Carl de Souza Enterprise Architect at Microsoft · AI Technology Expert

Leave a Reply

Your email address will not be published. Required fields are marked *