Select to view content in your preferred language

Script runs on test, but displays a table with no values

402
5
Jump to solution
07-12-2024 04:08 AM
MarceloCatalão
Occasional Contributor

Hello

I wanted that in a dashboard list I am presented with the Date of the last record of a certain value and 3 days are added to the date, strange is that there is no script error, sample me a table, but without the desired final values, what can be the problem?

Thanks

// Get the Item Portal FeatureSet
var fs = FeatureSetByPortalItem(Portal('https://sgmai.maps.arcgis.com'), 'cc483edaa0f3457c8cd99149d36c1ced', 0, ['selec', 'CreationDate'], false);

// Filter AEO records and sort by creation date
var fsAEO = Filter(fs, "selec = 'AEO'");
var sortedFS = OrderBy(fsAEO, 'CreationDate DESC');

// Create a list to store the modified records
var updatedFeatures = [];

// Iterate over the records and modify the date
for (var feature in sortedFS) {
// Obter a data de criação
var creationDate = Date(feature.CreationDate);

// Add 3 days to the date
var newDate = DateAdd(creationDate, 3, 'days');

// Create a New Object with the Date Modified
var updatedFeature = {
'attributes': {
'selec': feature.selec,
'CreationDate': newDate
}
};

// Add the new object to the list
Push(updatedFeatures, updatedFeature);
}

// Create a New FeatureSet with the Modified Records
var updatedFeatureSet = {
'fields': [
{'name': 'selec', 'type': 'esriFieldTypeString'},
{'name': 'CreationDate', 'type': 'esriFieldTypeDate'}
],
'geometryType': '',
'features': updatedFeatures
};

// Return the Modified FeatureSet
return FeatureSet(Text(updatedFeatureSet));

1 Solution

Accepted Solutions
MarceloCatalão
Occasional Contributor

Resolvido: Este funciona 🙂

Muito obrigado

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  // portal item id
  "cc483edaa0f3457c8cd99149d36c1ced",
  0, // layer id
  ['selec', 'PGDH2'], // fields to include
  false // include or exclude geometry
);

// Filter AEO records and sort by 'PGDH2' descending
var fsAEO = Filter(fs, "selec = 'Registar AEO'");
console("Count of filtered AEO records: " + Count(fsAEO));

var sortedFS = OrderBy(fsAEO, "PGDH2 DESC");
// Create a list to store the modified records
var updatedFeatures = [];

// Iterate over the records and modify the date
for (var feature in sortedFS) {
  // Get the PGDH2 date and convert to string to remove time
  var creationDate = Text(Date(feature.PGDH2), "YYYY-MM-DD");

  // Convert back to date object and add 3 days
  var newDate = DateAdd(Date(creationDate), 3, "days");

  // Format new date to only include day, month, and year
  var formattedDate = Text(newDate, "DD-MM-YYYY");

  // Create a new object with the date modified as text
  var updatedFeature = {
    attributes: { selec: feature.selec, PGDH2: formattedDate }
  };
  // Add the new object to the list
  Push(updatedFeatures, updatedFeature);
}

// Create a new FeatureSet with the modified records
var updatedFeatureSet = {
  fields: [
    { name: "selec", type: "esriFieldTypeString" },
    { name: "PGDH2", type: "esriFieldTypeString" }
  ],
  geometryType: "",
  features: updatedFeatures
};

// Log the updated feature set
console("Updated feature set: " + Text(updatedFeatureSet));

// Return the modified FeatureSet
return FeatureSet(updatedFeatureSet);

View solution in original post

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor

Your code works correctly with a sample dataset (with a couple of modification for different fields). I would check if you're getting the expected records from your filter (line 13).

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com"),
  // portal item id
  "6200db0b80de4341ae8ee2b62d606e67",
  0, // layer id
  ["*"], // fields to include
  false // include or exclude geometry
);

// Filter AEO records and sort by creation date
var fsAEO = Filter(fs, "FeatureCode = 1000");
console(Count(fsAEO))
var sortedFS = OrderBy(fsAEO, "LASTUPDATE DESC");
// Create a list to store the modified records
var updatedFeatures = [];

// Iterate over the records and modify the date
for (var feature in sortedFS) {
  // Obter a data de criação
  var creationDate = Date(feature.LASTUPDATE);

  // Add 3 days to the date
  var newDate = DateAdd(creationDate, 3, "days");

  // Create a New Object with the Date Modified
  var updatedFeature = {
    attributes: { selec: feature.FeatureCode, CreationDate: newDate }
  };
  // Add the new object to the list
  Push(updatedFeatures, updatedFeature);
}

// Create a New FeatureSet with the Modified Records
var updatedFeatureSet = {
  fields: [
    { name: "selec", type: "esriFieldTypeString" },
    { name: "CreationDate", type: "esriFieldTypeDate" }
  ],
  geometryType: "",
  features: updatedFeatures
};

// Return the Modified FeatureSet
return FeatureSet(updatedFeatureSet);

 

0 Kudos
MarceloCatalão
Occasional Contributor

Your data is working fine, when I change it to my portal and go to put the value "Register AEO" it gives an error

 

(ERRO: Test execution error: Expected "!=", "(", "*", "+", "-", "/", "<", "<=", "<>", "=", ">", ">=", "AND", "OR", "||", [ \t\n\r], or end of input but "A" found.. Verify test data.)

 

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  // portal item id
  "cc483edaa0f3457c8cd99149d36c1ced",
  0, // layer id
  ['selec','CreationDate'], // fields to include
  false // include or exclude geometry
);

// Filter AEO records and sort by creation date
var fsAEO = Filter(fs, "FeatureCode = Registar AEO");
console(Count(fsAEO))
var sortedFS = OrderBy(fsAEO, "selec");
// Create a list to store the modified records
var updatedFeatures = [];

// Iterate over the records and modify the date
for (var feature in sortedFS) {
  // Obter a data de criação
  var creationDate = Date(feature.LASTUPDATE);

  // Add 3 days to the date
  var newDate = DateAdd(creationDate, 3, "days");

  // Create a New Object with the Date Modified
  var updatedFeature = {
    attributes: { selec: feature.FeatureCode, CreationDate: newDate }
  };
  // Add the new object to the list
  Push(updatedFeatures, updatedFeature);
}

// Create a New FeatureSet with the Modified Records
var updatedFeatureSet = {
  fields: [
    { name: "selec", type: "esriFieldTypeString" },
    { name: "CreationDate", type: "esriFieldTypeDate" }
  ],
  geometryType: "",
  features: updatedFeatures
};

// Return the Modified FeatureSet
return FeatureSet(updatedFeatureSet);
0 Kudos
KenBuja
MVP Esteemed Contributor

The Filter sql statement is incorrect. You had it correct in your original code. Did you mean to use this?

var fsAEO = Filter(fs, "selec = 'Registar AEO'");

 

0 Kudos
MarceloCatalão
Occasional Contributor

Resolvido: Este funciona 🙂

Muito obrigado

// Fetches features from a public portal item
var fs = FeatureSetByPortalItem(
  // portal item id
  "cc483edaa0f3457c8cd99149d36c1ced",
  0, // layer id
  ['selec', 'PGDH2'], // fields to include
  false // include or exclude geometry
);

// Filter AEO records and sort by 'PGDH2' descending
var fsAEO = Filter(fs, "selec = 'Registar AEO'");
console("Count of filtered AEO records: " + Count(fsAEO));

var sortedFS = OrderBy(fsAEO, "PGDH2 DESC");
// Create a list to store the modified records
var updatedFeatures = [];

// Iterate over the records and modify the date
for (var feature in sortedFS) {
  // Get the PGDH2 date and convert to string to remove time
  var creationDate = Text(Date(feature.PGDH2), "YYYY-MM-DD");

  // Convert back to date object and add 3 days
  var newDate = DateAdd(Date(creationDate), 3, "days");

  // Format new date to only include day, month, and year
  var formattedDate = Text(newDate, "DD-MM-YYYY");

  // Create a new object with the date modified as text
  var updatedFeature = {
    attributes: { selec: feature.selec, PGDH2: formattedDate }
  };
  // Add the new object to the list
  Push(updatedFeatures, updatedFeature);
}

// Create a new FeatureSet with the modified records
var updatedFeatureSet = {
  fields: [
    { name: "selec", type: "esriFieldTypeString" },
    { name: "PGDH2", type: "esriFieldTypeString" }
  ],
  geometryType: "",
  features: updatedFeatures
};

// Log the updated feature set
console("Updated feature set: " + Text(updatedFeatureSet));

// Return the modified FeatureSet
return FeatureSet(updatedFeatureSet);
0 Kudos
dgiersz_cuyahoga
Frequent Contributor

I suggest adding Console() statements after each variable declaration to make sure they are returning what you are expecting. Console() can show the object returned and you can also use it to show a count of objects.

#CLE #sloth
0 Kudos