cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Is the alert example code on this page still valid as of 8.4?

JonathonFowler
12-Amethyst

Is the alert example code on this page still valid as of 8.4?

https://support.ptc.com/cs/help/thingworx_hc/thingworx_7.0_hc/index.jspx?id=thingworx0303&action=show

 

At the bottom of this page there is an example of how to add or edit an alert for a given property. However when I try and implement this I get the error: "Error executing service AlertAddUpdate. Message :: Alert Attributes For [Above] are not valid - See Script Error Log for more details."

 

The only attributes we need to set are the limit and the limit inclusive if I'm correct, so why would I be getting this error message? I have changed the property variable name to a property on the thing where I am implementing this service.

 

// Create the Attributes Infotable for AddOrUpdateAlert call
var params = {
	infoTableName: "alertAttr" /* STRING */
};
var attrs = Resources["InfoTableFunctions"].CreateInfoTable(params);

// Define the fields required by an Above type Alert for an INTEGER field (not the values!)

// limit - Minimum value at which the alert will trigger.
var field = new Object();
field.name = "limit";
field.baseType = 'INTEGER';
attrs.AddField(field);

// limitInclusive - Is the condition Greater than (>) or Greater than or equal (>=)?
field.name = "limitInclusive";
field.baseType = 'BOOLEAN';
attrs.AddField(field);

// Define the new Alert values and add the row to the table.
var row = new Object();
row.limit = 5;					// Alert will trigger when field >=5
row.limitInclusive = true;		// Alert includes 5
attrs.AddRow(row);

// Call the AddOrUpdateAlert Service
var params = {
	alertType: "Above" /* STRING */,
	alertName: "MyAlert" /* STRING */,
	property: "Sensor_Value2" /* STRING */,
	attributes: attrs /* INFOTABLE */,
   	priority: 1 /* INTEGER */,
	persistent: true /* BOOLEAN */,
	enabled: true /* BOOLEAN */
};

me.AddOrUpdateAlert(params);

This is my code just for reference. 

 

Thanks,


Jon

ACCEPTED SOLUTION

Accepted Solutions
AnnaAn
14-Alexandrite
(To:AnnaAn)

Attached the entity file.

View solution in original post

8 REPLIES 8

Any more info in the logs in regards to the error?

The logs are about as helpful as you'd expect - "[message: Execution error in service script [AlertAddUpdate] :: Alert Attributes For [Above] are not valid]"

I guess it is saying that the attributes you specified for Above are wrong. I would pull the attributes from an existing alert to check if either you have it incorrect or if it has changed.

Maybe a spelling issue?

So the code on the website is wrong? Because the attributes are defined all within this service.

I haven't looked at it in depth sorry so I'm not sure, but I do know that for the different type of alerts, the attributes are slightly different. That's why i recommend just pulling one from a configured one so you know exactly what is the right info.

AnnaAn
14-Alexandrite
(To:JonathonFowler)

Hi, The code example should work for some Alert Types(Above, Below,etc.), but for some Alert Types like InRange, OutOfRange(I did not verify each each just these two  are verified) it will not work. Instead use the ThingWorx build-in DataShape like IntegerRangeAlert to create InfoTable(CreateInfoTableFromDataShape) to hold the Attrbute table. I have created a sample for your reference(two services, one is Above, another is for OutOfRange) please import into your system and try again.

Not sure why it did not work for you for the original code for Above Alert Type, it worked for me.

 

AnnaAn
14-Alexandrite
(To:AnnaAn)

Attached the entity file.

Thank you, works fine for me now.

Announcements


Top Tags