Skip to main content
1-Visitor
December 20, 2021
Solved

Unable to get the property an alert is working on

  • December 20, 2021
  • 1 reply
  • 2047 views

Hello,

 

I'm workin on an app with Thingworx 9.1, user are able to create alert from the app. Know I'm trying to make them able to delete an alert. For this I'm using the RemoveAlert() function, to work it need alertName, property, persistent. I can get all this information from GetAlertDefinitions() service except for the property. And i didn't figure out how to get it. I can find it in the composer but I would like to make it work without the need of using the composer.

 

Any idea ? Thanks 

Best answer by amalecot

Hello,

There is a service GetAlertStatuses on things, it retrieves all alert names with attached property name.

amalecot_0-1641308278585.png

It provides the required information to perform a call to the RemoveAlert service :

 

 

 

me.RemoveAlert({
	alertName: "alert1" /* STRING */,
	property: "property1" /* STRING */,
	persistent: true /* BOOLEAN {"defaultValue":false} */ 
});

//the persistance of this alert deletion (if thing is saved, platform restarted… be carreful with composer override)

 

 

 

 

 

There are other options if you want more information about the alerts:

Here is one way to do it:

  1. Duplicate the datashape AlertDefinition,
    • name the new one : AlertDefinitionWithSourceProperty.
    • Add field : "sourceProperty"
    • Save
  2. The code bellow do the following :
    • Create the list of all properties of a thing
    • For each property, get the list of all alerts with their definitions
    • Create as a result the list of all alerts with definition and name of the source property

 

 

 

//Input : thingName
//Output : infotable (AlertDefinitionWithSourceProperty)
	
let result = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
	infoTableName: "InfoTable",
	dataShapeName: "AlertDefinitionWithSourceProperty"
});
	
// result: INFOTABLE dataShape: "PropertyDefinition"
let allProperties = Things[thingName].GetPropertyDefinitions({
	category: undefined /* STRING */,
	type: undefined /* BASETYPENAME */,
	dataShape: undefined /* DATASHAPENAME */
});
	
let i;
let j;
for(i=0;i<allProperties.getRowCount();i++){	
	let AlertDefinitions = Things[thingName].GetAlertDefinitions({
		property: allProperties[i].name 
	}); 
	for(j=0;j<AlertDefinitions.getRowCount();j++){
		// AlertDefinition entry object
		let alertEntryOnCurrentProperty ={//AlertDefinitionWithSourceProperty
			alertAttributes: AlertDefinitions[j].alertAttributes, // INFOTABLE
			alertType: AlertDefinitions[j].alertType, // STRING
			name: AlertDefinitions[j].name, // STRING [Primary Key]
			description: AlertDefinitions[j].description, // STRING
			priority: AlertDefinitions[j].priority, // INTEGER
			enabled: AlertDefinitions[j].enabled, // BOOLEAN
			sourceProperty: allProperties[i].name
		};
		result.AddRow(alertEntryOnCurrentProperty);
	}
}
	
//Then you can use the columns "name" and "source property" (of a selected row in a mashup ?) to perform the alert deletion.

 

 

 

 

 

"user can see the alert they've created and also those created by other and delete both of them"

as Sharon suggests, be careful if you also also have alerts created by administrators, that should not be removed at all (as the one manageable by any user with permissions). Her solution with additional persisted infotable works for this case too. (with second solution, you can also use property categories to mark and filter the properties)

1 reply

Community Manager
December 20, 2021

Hi @BenjaminD.

 

Since you're allowing users to create alerts, are you giving them the ability to see what they've created?  The reason we're asking is because we don't have a single service that does this, but you could use multiple services for achieving your task.  You would also need to be concerned about a user deleting the wrong alert, especially if they didn't create them and therefore may not know whether the alert should be removed.  In your use case, should a user have the ability to delete an alert they didn't create?

 

For identifying the property, you may want to store the alert with the property at the time the alert was created.  Then allow the user to do a lookup against that table that contains the alert along with the property.

 

If we're not understanding, please provide more details around your use case.

 

Regards.

 

--Sharon

BenjaminD1-VisitorAuthor
1-Visitor
December 21, 2021

To answer you question yes user can see the alert they've created and also those created by other and delete both of them. I understand you're solution, but the information is saved natively in thingworx isn't it ? I'd like to get to this information instead of creating a new infotable. Can you tell me about wich service are you thinking in this phrase "you could use multiple services for achieving your task" ? 

 

Thanks for your time and help