XrmServiceToolkit.Rest Methods

These examples are built and combined with qunit. Please check http://docs.jquery.com/QUnit for more details if you are new to it.

Create

test("Test XrmServiceToolkit.Rest.Create() method to create a new record", function () {

            var account = {};
            account.Name = "Test Account Name";
            account.Description = "This account was created by the XrmServiceToolkit.Rest.Create() sample.";
            if (contactId != null) {
                //Set a lookup value
                account.PrimaryContactId = { Id: contactId, LogicalName: "contact" };
            }
            //Set a picklist value
            account.PreferredContactMethodCode = { Value: 2 }; //E-mail

            //Set a money value
            account.Revenue = { Value: "2000000.00" }; //Set Annual Revenue

            //Set a Boolean value
            account.DoNotPhone = true; //Do Not Allow

            //Add Two Tasks
            var today = new Date();
            var startDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 3); //Set a date three days in the future.

            var LowPriTask = { Subject: "Low Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 0} }; //Low Priority Task
            var HighPriTask = { Subject: "High Priority Task", ScheduledStart: startDate, PriorityCode: { Value: 2} }; //High Priority Task
            account.Account_Tasks = [LowPriTask, HighPriTask];
            XrmServiceToolkit.Rest.Create(
                account,
                "AccountSet",
                function (result) {
                    accountId = result.AccountId;
                    ok(guidExpr.test(result.AccountId), "Creating a account should returned the new record's ID in GUID format. ");
                },
                function (error) {
                    equal(true, false, error.message);
                },
                false
            );
});
Retrieve
test("Test XrmServiceToolkit.Rest.Retrieve() method to retrieve a record", function () {

            XrmServiceToolkit.Rest.Retrieve(
                accountId,
                "AccountSet",
                null, null,
                function (result) {
                    var Id = result.AccountId;
                    equals(Id, accountId, "Retrieve() method should return the same account ID as Create() result. ");
                },
                function (error) {
                    equal(true, false, error.message);
                },
                false
            );
        });
Update
test("Test XrmServiceToolkit.Rest.Update() method to update a record", function () {

            var account = {};
            account.Name = "Updated Account Name";
            account.Address1_AddressTypeCode = { Value: 3 }; //Address 1: Address Type = Primary
            account.Address1_City = "Sammamish";
            account.Address1_Line1 = "123 Maple St.";
            account.Address1_PostalCode = "98074";
            account.Address1_StateOrProvince = "WA";
            account.EMailAddress1 = "someone@microsoft.com";

            XrmServiceToolkit.Rest.Update(
                accountId,
                account,
                "AccountSet",
                function () {
                    equals(true, true, "The record should have been updated.");
                },
                function (error) {
                    equal(true, false, error.message);
                },
                false
            );
});

Delete

test("Test XrmServiceToolkit.Rest.Delete(account) method to delete a record", function () {

            XrmServiceToolkit.Rest.Delete(
                accountId,
                "AccountSet",
                function () {
                    equals(true, true, "The record should have been deleted.");
                },
                function (error) {
                    equal(true, false, error.message);
                },
                false
            );
});
Associate
test("Test XrmServiceToolkit.Rest.Associate() method to associate a record", function () {

            XrmServiceToolkit.Rest.Associate(
                contactId,
                "ContactSet",
                accountId,
                "AccountSet",
                "account_primary_contact",
                function () {
                    equals(true, true, "The record should have been associated.");
                },
                function (error) {
                    equal(true, false, error.message);
                },
                false
            );
});
Disassociate
test("Test XrmServiceToolkit.Rest.Disassociate() method to disassociate a record", function () {

            XrmServiceToolkit.Rest.Disassociate(
                    contactId,
                    "ContactSet",
                    accountId,
                    "account_primary_contact",
                    function () {
                        equals(true, true, "The record should have been disassociated.");
                    },
                    function (error) {
                        equal(true, false, error.message);
                    },
                    false
                );
});

 

RetrieveMultiple
test("Test XrmServiceToolkit.Rest.RetrieveMultiple(contact) method to get the contacts", function () {

	XrmServiceToolkit.Rest.RetrieveMultiple(
		"ContactSet",
		"$select=ContactId, FullName",
		function (results) {
			ok(results.length > 0, results.length.toString() + " records should have been retrieved.");
		},
		function (error) {
			equal(true, false, error.message);
		},
		function onComplete() {

		},
		false
	);
});

 

Last edited Jul 30, 2012 at 1:27 PM by jaimieji, version 22

Comments

jashwood Sep 14, 2012 at 12:07 AM 
Guys, how about some doco here about what each function argument does?
We need DOCUMENTATION - not just code samples.

GaryWithRockton Sep 5, 2012 at 10:45 PM 
I am getting an Access is Denied error at line 559 of the toolkit when I try to use the Rest.Create function. Line 559 reads req.open("POST", oDataPath() + type, async);

Any ideas on what might be causing this?

fabson69 Jul 24, 2012 at 2:33 PM 
Hello, We have problems when retrieving dateTime attibutes values with XrmServiceToolkit.Soap functions. We have NaN value, is it possible to change that. Thanks

marcododaro Jul 18, 2012 at 5:07 PM 
Could be possible to have a couple of examples involving Retrieve Multiple? Many thanks

jaimieji Mar 5, 2012 at 10:21 AM 
Gary. Updated the document. Thanks

GaryWithRockton Mar 2, 2012 at 6:19 PM 
The update example does not update anything. If tact it is an exact duplicate of the Retreive example.