Can we use Retrieve Single Record Syncronously?

Oct 28, 2013 at 3:07 PM
I would like to use XrmSvcToolk.retrieve synchronously. Also, I would like to create a function that "wraps" the retrieve function so that I can use it other places in my code and so that my code stays small.

I'd like to call it like this:
var order = comRetrieveSingleRecord("SalesOrder", orderId, false, getProductAlerts);
var warehouseId = order.aa_WarehouseId.Id;
But it doesn't seem to return synchronously.

Here's my definition for comRetrieveSingleRecord, note, I added in "if async === false, then return result"
//Gets all the info for a single record if async = false, returns data directly
function comRetrieveSingleRecord(entityName, id, async, successCallback) {
    comToConsole("comRetrieveSingleRecord");
    XrmSvcToolkit.retrieve({
        entityName: entityName,
        id: id,
        async: async,
        successCallback: function (result) {
            if (async === false) {
                return result;
            } else {
                successCallback(result);
            }
        },
        errorCallback: function (error) {
            alert("Error");
        }
    });
}
Am I doing something wrong? Or is this impossible?
Coordinator
Oct 28, 2013 at 4:37 PM
Nathan,

Have you looked at the samples in: XrvSvcToolkit.Samples.retrieve.js

I think that will do what you want. Let me know if it doesn't.

Thanks, Mitch
Oct 28, 2013 at 4:40 PM

Mitch,

Where do you see that? It’s not in the download as far as I can tell??

Or do you mean XrmSvcToolkitTest.aspx?

Nathan

Coordinator
Oct 28, 2013 at 4:46 PM
Oct 28, 2013 at 4:58 PM

Ok, so in the samples is this function:

function retrieveOperationAsync(contactId)

{

XrmSvcToolkit.retrieve({

entityName: "Contact",

id: contactId,

select: ["ContactId", "FirstName", "MiddleName", "LastName", "GenderCode", "FamilyStatusCode", "CreditLimit", "BirthDate", "DoNotEMail", "DoNotPhone"],

async: true,

successCallback: function (result)

{

alert("Retrieve just retrieved: " + result.FirstName + " " + result.MiddleName + " " + result.LastName);

},

errorCallback: function (error)

{

throw error;

}

});

}

Could I change is to that instead of a alert it would return(result) ?

Then can I call the function like this:

var contact = retrieveOperationAsync(contactId);

var firstName = contact.FirstName;

thanks for your help,

Basically I’m looking for a simpler way of calling the function, and continuing on with my code in the same function, rather than having to create a successCallback function…

Hopefully that makes sense.

Nathan

From: Nathan Vanderhoof
Sent: Monday, October 28, 2013 3:41 PM
To: '[email removed]'
Subject: RE: Can we use Retrieve Single Record Syncronously? [xrmsvctoolkit:463902]

Mitch,

Where do you see that? It’s not in the download as far as I can tell??

Or do you mean XrmSvcToolkitTest.aspx?

Nathan

Coordinator
Oct 28, 2013 at 5:06 PM
Nathan,

There are two ways to make a synchronous call. You can use the callback pattern as you have illustrated in your code (or what Mitch has demonstrated in his samples).

Alternatively, you can simply receive the result directly from the return value of the function call, such as this one.
var result = XrmSvcToolkit.retrieve({
  entityName: "Contact",
  id: contactId,
  select: ["ContactId", "FirstName", "MiddleName", "LastName", "GenderCode", "FamilyStatusCode", "CreditLimit", "BirthDate", "DoNotEMail", "DoNotPhone"],
  async: false
});

alert("Retrieve just retrieved: " + result.FirstName + " " + result.MiddleName + " " + result.LastName);
This way, you don't need an errorCallback, since it will throw an exception anyway should an error occurs.

With your code, you would want to change async: true to async: false.
Marked as answer by NateOne on 11/6/2013 at 2:52 AM
Coordinator
Oct 28, 2013 at 5:14 PM
Nathan,

I am a bit confused here. Why are you bothering with the async sample when you said you needed a synchronous method?

Use this from the samples:
function retrieveOperationSync(contactId)
{
    return XrmSvcToolkit.retrieve({
        entityName: "Contact",
        id: contactId,
        select: ["ContactId", "FirstName", "MiddleName", "LastName", "GenderCode", "FamilyStatusCode", "CreditLimit", "BirthDate", "DoNotEMail", "DoNotPhone", "contact_owning_user/SystemUserId", "contact_owning_user/FullName"],
        expand: ["contact_owning_user"],
        async: false
    });
}
Marked as answer by NateOne on 11/6/2013 at 2:51 AM
Oct 28, 2013 at 6:15 PM

Mitch and Daniel,

Ok, I’ll do little bit more playing around with it and post back with my results.

Nathan

Nov 6, 2013 at 10:49 AM
Mitch and Daniel,

The Synchronous samples in the Source Code section were a big help!

This is what I came up with in the end:

I put this in my "Common" javascript library that is loaded for every entity:
function comRetrieveSingleRecordSync(entityName, id, fields) {
    comToConsole("comRetrieveSingleRecordSync");
    return XrmSvcToolkit.retrieve({
        entityName: entityName,
        id: id,
        select: [fields],
        expand: null,
        async: false
    });
}
And then I can call it like this:
var state = "";
var stateAbbreviation = "";

state = comRetrieveSingleRecordSync("Aa_stateprovince", stateId, "Aa_Abbreviation");
stateAbbreviation = state.Aa_Abbreviation;
Many thanks for your help!
Nate