<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Trying to send query data to widget table in Experience Builder Custom Widgets</title>
    <link>https://community.esri.com/t5/experience-builder-custom-widgets/trying-to-send-query-data-to-widget-table/m-p/1478777#M331</link>
    <description>&lt;P&gt;I made a custom widget that executes a query and then sends the response to this method in widget.tsx:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;  const populateDataSource = async (features: any[]) =&amp;gt; {
    const dataSourceManager = DataSourceManager.getInstance();
    const outputDsId = `${props.id}-output`;
  
    // Extract fields dynamically from features
    const feature = features[0];
    const attributes = feature.attributes;
    const fields = Object.keys(attributes).reduce((acc, fieldName) =&amp;gt; {
      acc[fieldName] = {
        name: fieldName,
        alias: fieldName,
        type: typeof attributes[fieldName] === 'number' ? 'esriFieldTypeDouble' : 'esriFieldTypeString'
      };
      return acc;
    }, {});
  
    // Define data source JSON
    const dsJson: IMDataSourceJson = Immutable({
      id: outputDsId,
      type: DataSourceTypes.FeatureLayer,
      label: "Query Result Layer",
      originDataSources: [],
      isDataInDataSourceInstance: true,
      schema: {
        fields: fields
      }
    });
  
    // Create or get the data source
    let ods;
    try {
      ods = await dataSourceManager.createDataSource(dsJson);
    } catch (error) {
      console.error("Error creating data source:", error);
      return;
    }
  
    // Populate the data source with features
    const dataRecords = features.map(f =&amp;gt; ods.buildRecord(f));
    ods.setSourceRecords(dataRecords);
    ods.setStatus(DataSourceStatus.Loaded);
    ods.setCountStatus(DataSourceStatus.Loaded);
  
    // Prepare the DataRecordSet array
    const dataRecordSet: DataRecordSet = {
      name: `${props.id}-output-record-set`,
      dataSource: ods,
      records: dataRecords
    };
  
    // Determine the data level, typically it is DataLevel.Records for multiple records
    const dataLevel = DataLevel.Records;
  
    // Use the widget ID of the table widget where the action will be applied
    const widgetId = "widget_5"; // Replace this with your actual table widget ID
  
    // Dispatch the action to open the results in a new tab in the table widget
    const instance = DataActionManager.getInstance();
    const action = instance.getActions().find(a =&amp;gt; a.id === "widget_5-addToTable");
    if (action) {
      console.log("Executing data action");
      await action.onExecute([dataRecordSet], dataLevel, widgetId);
    } else {
      console.error("Data action not found");
    }
    console.log(DataSourceManager.getInstance());
    console.log(DataActionManager.getInstance());
  };
  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;I logged DataActionManager.getInstance() and got the table widget's ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When this method is executed, a new tab with the proper name is created in the table widget, but it is empty.&lt;/P&gt;&lt;P&gt;When logging DataSourceManager I see this widget's data source:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-05-25 144352.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/105393i88D2A9698DB3606F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2024-05-25 144352.png" alt="Screenshot 2024-05-25 144352.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Along with the errors when the new tab is created.&lt;/P&gt;&lt;P&gt;How can I make this work? I would appreciate any kind of help. Please let me know if you need more info/context.&lt;/P&gt;</description>
    <pubDate>Sat, 25 May 2024 13:48:02 GMT</pubDate>
    <dc:creator>Esprit3</dc:creator>
    <dc:date>2024-05-25T13:48:02Z</dc:date>
    <item>
      <title>Trying to send query data to widget table</title>
      <link>https://community.esri.com/t5/experience-builder-custom-widgets/trying-to-send-query-data-to-widget-table/m-p/1478777#M331</link>
      <description>&lt;P&gt;I made a custom widget that executes a query and then sends the response to this method in widget.tsx:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;  const populateDataSource = async (features: any[]) =&amp;gt; {
    const dataSourceManager = DataSourceManager.getInstance();
    const outputDsId = `${props.id}-output`;
  
    // Extract fields dynamically from features
    const feature = features[0];
    const attributes = feature.attributes;
    const fields = Object.keys(attributes).reduce((acc, fieldName) =&amp;gt; {
      acc[fieldName] = {
        name: fieldName,
        alias: fieldName,
        type: typeof attributes[fieldName] === 'number' ? 'esriFieldTypeDouble' : 'esriFieldTypeString'
      };
      return acc;
    }, {});
  
    // Define data source JSON
    const dsJson: IMDataSourceJson = Immutable({
      id: outputDsId,
      type: DataSourceTypes.FeatureLayer,
      label: "Query Result Layer",
      originDataSources: [],
      isDataInDataSourceInstance: true,
      schema: {
        fields: fields
      }
    });
  
    // Create or get the data source
    let ods;
    try {
      ods = await dataSourceManager.createDataSource(dsJson);
    } catch (error) {
      console.error("Error creating data source:", error);
      return;
    }
  
    // Populate the data source with features
    const dataRecords = features.map(f =&amp;gt; ods.buildRecord(f));
    ods.setSourceRecords(dataRecords);
    ods.setStatus(DataSourceStatus.Loaded);
    ods.setCountStatus(DataSourceStatus.Loaded);
  
    // Prepare the DataRecordSet array
    const dataRecordSet: DataRecordSet = {
      name: `${props.id}-output-record-set`,
      dataSource: ods,
      records: dataRecords
    };
  
    // Determine the data level, typically it is DataLevel.Records for multiple records
    const dataLevel = DataLevel.Records;
  
    // Use the widget ID of the table widget where the action will be applied
    const widgetId = "widget_5"; // Replace this with your actual table widget ID
  
    // Dispatch the action to open the results in a new tab in the table widget
    const instance = DataActionManager.getInstance();
    const action = instance.getActions().find(a =&amp;gt; a.id === "widget_5-addToTable");
    if (action) {
      console.log("Executing data action");
      await action.onExecute([dataRecordSet], dataLevel, widgetId);
    } else {
      console.error("Data action not found");
    }
    console.log(DataSourceManager.getInstance());
    console.log(DataActionManager.getInstance());
  };
  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;I logged DataActionManager.getInstance() and got the table widget's ID.&amp;nbsp;&lt;/P&gt;&lt;P&gt;When this method is executed, a new tab with the proper name is created in the table widget, but it is empty.&lt;/P&gt;&lt;P&gt;When logging DataSourceManager I see this widget's data source:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2024-05-25 144352.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/105393i88D2A9698DB3606F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot 2024-05-25 144352.png" alt="Screenshot 2024-05-25 144352.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Along with the errors when the new tab is created.&lt;/P&gt;&lt;P&gt;How can I make this work? I would appreciate any kind of help. Please let me know if you need more info/context.&lt;/P&gt;</description>
      <pubDate>Sat, 25 May 2024 13:48:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/experience-builder-custom-widgets/trying-to-send-query-data-to-widget-table/m-p/1478777#M331</guid>
      <dc:creator>Esprit3</dc:creator>
      <dc:date>2024-05-25T13:48:02Z</dc:date>
    </item>
  </channel>
</rss>

