<?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 sketchViewModel triggered from outside of map in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketchviewmodel-triggered-from-outside-of-map/m-p/1555167#M86026</link>
    <description>&lt;P&gt;Hello, I'm trying to make a polygon editable by clicking a button that's located outside of ESRI map.&lt;/P&gt;&lt;P&gt;But keep getting this error:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;details: &lt;/SPAN&gt;&lt;SPAN&gt;undefined&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;message: &lt;/SPAN&gt;&lt;SPAN&gt;"Parameter 'graphics' contains one or more graphics missing from the supplied GraphicsLayer."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;name: &lt;/SPAN&gt;&lt;SPAN&gt;"sketch:invalid-parameter"&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Here's a gist of what I mean:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let iconLayer: GraphicsLayer | null = null;

let arcgisMap: MapView | null = null;
let sketchViewModel: Sketch|null = null;

const createMap = () =&amp;gt; {
  iconLayer = new GraphicsLayer();

  arcgisMap = new MapView({
    container: mapContainer,
    map: {
      basemap: 'streets-navigation-vector',
      layers: [iconLayer]
    },
    zoom: 10,
    center: [-79, 38]
  });

  sketchViewModel = new Sketch({
    layer: iconLayer,
    view: arcgisMap,
    creationMode: 'update',
    availableCreateTools: ['polygon']
  });
};

const buttonClickCallback = (editingId) =&amp;gt; { // Triggered by a buttom press somewhere on the dom
  console.log({ editingId });
  if (sketchViewModel &amp;amp;&amp;amp; iconLayer &amp;amp;&amp;amp; arcgisMap) {
    if (editingId) {
      sketchViewModel.viewModel.cancel();

      let graphicToEdit: any = null;
      for (let i = 0; i &amp;lt; iconLayer.graphics.length; i++) {
        const graphic = iconLayer.graphics.getItemAt(i);
        if (graphic.attributes.specialId === editingId) {
          graphicToEdit = graphic;
          console.log('Found graphic', { guid: graphic.uid, specialId: graphic.attributes.specialId });
          // break;
        }
      }

      console.log('Editing: ', { uid: graphicToEdit.uid, specialId: graphicToEdit.attributes.specialId });

      if (graphicToEdit &amp;amp;&amp;amp; iconLayer.graphics.includes(graphicToEdit)) {
        try {
          // const clonedGraphic = new Graphic({
          //   geometry: graphicToEdit.geometry,
          //   symbol: graphicToEdit.symbol,
          //   attributes: { ...graphicToEdit.attributes }
          // });
          // console.log('New Duplicated Graphic: ', {
          //   uid: clonedGraphic.uid,
          //   specialId: clonedGraphic.attributes.specialId
          // });

          // iconLayer.add(clonedGraphic);

          // sketchViewModel.layer = iconLayer;
          // iconLayer.graphics = [clonedGraphic];
          // arcgisMap.whenLayerView(iconLayer).then(() =&amp;gt; {

          iconLayer.when(() =&amp;gt; {
            // sketchViewModel?.update([clonedGraphic] and
            // sketchViewModel?.update([graphicToEdit] also doesn't work
            sketchViewModel?.update(graphicToEdit, {
              tool: 'reshape',
            });
          });
        } catch (err) {
          console.log(`Unable to edit graphic found with specialId: ${editingId}`, err);
        }
      } else {
        console.log(`No graphic found with specialId: ${editingId}`);
      }
    } else {
      sketchViewModel.viewModel.cancel();
    }
  } else {
    console.log(`Sketch is not setup for polygon editing`);
  }
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 04 Nov 2024 17:59:29 GMT</pubDate>
    <dc:creator>Slyke</dc:creator>
    <dc:date>2024-11-04T17:59:29Z</dc:date>
    <item>
      <title>sketchViewModel triggered from outside of map</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketchviewmodel-triggered-from-outside-of-map/m-p/1555167#M86026</link>
      <description>&lt;P&gt;Hello, I'm trying to make a polygon editable by clicking a button that's located outside of ESRI map.&lt;/P&gt;&lt;P&gt;But keep getting this error:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;&lt;SPAN&gt;details: &lt;/SPAN&gt;&lt;SPAN&gt;undefined&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;message: &lt;/SPAN&gt;&lt;SPAN&gt;"Parameter 'graphics' contains one or more graphics missing from the supplied GraphicsLayer."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;name: &lt;/SPAN&gt;&lt;SPAN&gt;"sketch:invalid-parameter"&lt;/SPAN&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Here's a gist of what I mean:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;let iconLayer: GraphicsLayer | null = null;

let arcgisMap: MapView | null = null;
let sketchViewModel: Sketch|null = null;

const createMap = () =&amp;gt; {
  iconLayer = new GraphicsLayer();

  arcgisMap = new MapView({
    container: mapContainer,
    map: {
      basemap: 'streets-navigation-vector',
      layers: [iconLayer]
    },
    zoom: 10,
    center: [-79, 38]
  });

  sketchViewModel = new Sketch({
    layer: iconLayer,
    view: arcgisMap,
    creationMode: 'update',
    availableCreateTools: ['polygon']
  });
};

const buttonClickCallback = (editingId) =&amp;gt; { // Triggered by a buttom press somewhere on the dom
  console.log({ editingId });
  if (sketchViewModel &amp;amp;&amp;amp; iconLayer &amp;amp;&amp;amp; arcgisMap) {
    if (editingId) {
      sketchViewModel.viewModel.cancel();

      let graphicToEdit: any = null;
      for (let i = 0; i &amp;lt; iconLayer.graphics.length; i++) {
        const graphic = iconLayer.graphics.getItemAt(i);
        if (graphic.attributes.specialId === editingId) {
          graphicToEdit = graphic;
          console.log('Found graphic', { guid: graphic.uid, specialId: graphic.attributes.specialId });
          // break;
        }
      }

      console.log('Editing: ', { uid: graphicToEdit.uid, specialId: graphicToEdit.attributes.specialId });

      if (graphicToEdit &amp;amp;&amp;amp; iconLayer.graphics.includes(graphicToEdit)) {
        try {
          // const clonedGraphic = new Graphic({
          //   geometry: graphicToEdit.geometry,
          //   symbol: graphicToEdit.symbol,
          //   attributes: { ...graphicToEdit.attributes }
          // });
          // console.log('New Duplicated Graphic: ', {
          //   uid: clonedGraphic.uid,
          //   specialId: clonedGraphic.attributes.specialId
          // });

          // iconLayer.add(clonedGraphic);

          // sketchViewModel.layer = iconLayer;
          // iconLayer.graphics = [clonedGraphic];
          // arcgisMap.whenLayerView(iconLayer).then(() =&amp;gt; {

          iconLayer.when(() =&amp;gt; {
            // sketchViewModel?.update([clonedGraphic] and
            // sketchViewModel?.update([graphicToEdit] also doesn't work
            sketchViewModel?.update(graphicToEdit, {
              tool: 'reshape',
            });
          });
        } catch (err) {
          console.log(`Unable to edit graphic found with specialId: ${editingId}`, err);
        }
      } else {
        console.log(`No graphic found with specialId: ${editingId}`);
      }
    } else {
      sketchViewModel.viewModel.cancel();
    }
  } else {
    console.log(`Sketch is not setup for polygon editing`);
  }
};&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2024 17:59:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketchviewmodel-triggered-from-outside-of-map/m-p/1555167#M86026</guid>
      <dc:creator>Slyke</dc:creator>
      <dc:date>2024-11-04T17:59:29Z</dc:date>
    </item>
    <item>
      <title>Re: sketchViewModel triggered from outside of map</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketchviewmodel-triggered-from-outside-of-map/m-p/1555273#M86029</link>
      <description>&lt;P&gt;It appears the only way you get this error is if the &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html" target="_self"&gt;graphic&lt;/A&gt; you're passing to &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Sketch.html#update" target="_self"&gt;update&lt;/A&gt; has its &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html#layer" target="_self"&gt;layer&lt;/A&gt; property set to something other than the &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-layers-GraphicsLayer.html" target="_self"&gt;GraphicsLayer&lt;/A&gt; used by the &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Sketch.html" target="_self"&gt;Sketch&lt;/A&gt; widget.&amp;nbsp; This would suggest somewhere in your application, you're getting those graphics from some other layer before adding them into the GraphicsLayer.&amp;nbsp; Perhaps instead of your present line 37, you could have these two lines instead:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;graphicToEdit = graphic.clone();
graphicToEdit.layer = iconLayer;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 04 Nov 2024 22:39:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/sketchviewmodel-triggered-from-outside-of-map/m-p/1555273#M86029</guid>
      <dc:creator>JoelBennett</dc:creator>
      <dc:date>2024-11-04T22:39:17Z</dc:date>
    </item>
  </channel>
</rss>

