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:

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

We get the output:

And if we want to do a RetrieveMultiple:

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

We get:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

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