Custom Action Entity Search Example

Code

// Check if model is provided
if (!model) {
  console.warn("No form data (model) provided.");
  return;
}

const { name, specificType } = model;
console.log("Searching entities with Name:", name, "and Specific Type:", specificType);

// Create the builder
var builder = $api.entitiesSearchBuilder().flattened();

// Define filters based on model values
var filter = {
  and: [
    {
      eq: {
        'resourceType': 'entity.device'
      }
    }
  ]
};

if (name) {
  filter.and.push({
    like: {
      'provision.administration.identifier': name // Assuming 'name' maps to identifier for this example, or use 'provision.device.name' if appropriate
    }
  });
}

if (specificType) {
  filter.and.push({
    eq: {
      'provision.device.specificType': specificType
    }
  });
}

// Apply filter if any conditions were added
if (filter.and.length > 0) {
  builder.filter(filter);
}

try {
  // Execute the search
  // await is supported in this context
  const response = await builder.build().execute();

  if (response.statusCode === 200) {
    console.log("--- Entity Search Results ---");
    const entities = response.data.entities;
    if (entities && entities.length > 0) {
      console.table(entities.map(e => ({
        id: e['provision.administration.identifier']._value._current.value,
        specificType: e['provision.device.specificType'] ? e['provision.device.specificType']._value._current.value : 'N/A'
      })));
      console.log("Total entities found:", entities.length);
    } else {
      console.log("No entities match the criteria.");
    }
  } else {
    console.error("Search failed with status:", response.statusCode);
  }

} catch (err) {
  console.error("Error executing entity search:", err);
}