Search code examples
javascriptasynchronousdynamics-crmmicrosoft-dynamicsdynamics-crm-webapi

Dynamics retrieveMultipleRecords asynchronous issue


I have an issue with Xrm.WebApi.retrieveMultipleRecords for which I hopefully get some hints or advice.

Withing Dynamics 365 I have a dialog containing a DevExpress control. This control needs to be initialized with a set of invoice templates.

My code (extract):

The Dialog html:

<script language="javascript" type="text/javascript">
  debugger;
  Globalize.culture('de-CH');
  var TableContent = getInvoiceTemplates();         

  var dataGridTemplates = $("#TemplateContainer").dxDataGrid({
    dataSource: TableContent,
      selection: {
        mode: 'single'                  
      },
      .....
   .....

The datasource (TableContent) is initialized by the function getInvoiceTemplates()

function getInvoiceTemplates()
{   
  debugger;
  var TableContent = new Array();

  var fetchTemplates = "<fetch>"+
                       "  <entity name='invoice'>" +
                       "    <attribute name='name' />" +
                       "    <attribute name='invoiceid' />" +
                       "    <filter type='and' >" +
                       "      <condition attribute='ambcust_vorlage_twooption' operator='eq' value='1' />" +
                       "    </filter>" +
                       "    <order attribute='name' descending='false' />" +
                       "  </entity>" +
                       "</fetch>";
  fetchTemplates = "?fetchXml=" + encodeURIComponent(fetchTemplates);
  Xrm.WebApi.retrieveMultipleRecords("invoice", fetchTemplates).then(
    function success(result)
    {
      for (var i = 0; i < result.entities.length; i++) 
      {
        var RowContent = new Object();          

        if(result.entities[i].invoiceid != null)
        {
          RowContent["TemplateID"] = result.entities[i].invoiceid;              
        }
        else
        {
          RowContent["TemplateID"] = "";                
        }

        if(result.entities[i].name != null)
        {
          RowContent["TemplateName"] = result.entities[i].name;
        }
        else
        {
          RowContent["TemplateName"] = "";
        }   

        TableContent.push(RowContent);  
      }
      return TableContent;
    },
    function (error)
    {
      alert("Error");
    }
  );
}

The issue is that the function retrieves the templates correctly and the property TableContent within the function contains the templates, but it always returns null and therefore the DataSource of the control in the dialog will be empty.

I'm aware the the API function is async but I thought that returning the array in the success callback will be enough.

Did I miss something here?


Solution

  • You can move the code where you are doing something with TableContent inside successCallback itself to overcome this.

    Xrm.WebApi.retrieveMultipleRecords("invoice", fetchTemplates).then(
            function success(result)
            {
                for (var i = 0; i < result.entities.length; i++) 
                {
                    var RowContent = new Object();          
    
                   ....
    
                    TableContent.push(RowContent);  
                }
    
                var dataGridTemplates = $("#TemplateContainer").dxDataGrid({
                dataSource: TableContent,
                selection: {
                    mode: 'single'                  
                },
            },
            function (error)
            {
                alert("Error");
            }
        );