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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

How to programmatically create a scheduler

Willie
16-Pearl

How to programmatically create a scheduler

Is it possible to programmatically create a Scheduler in thingworx 8.5.3?  I saw the following article, but I don't see this template in 8.5.3.

 

https://www.ptc.com/en/support/article/cs382629

 

I'd like to create a mashup where users can schedule email reminders for preventive maintenance.  The emails will be sent out to the users on the scheduled date/time.  In order to implement this, I think I need to be able to create a scheduler programmatically, where the subscription on the scheduler would be created automatically as well.

8 REPLIES 8
Velkumar
19-Tanzanite
(To:Willie)

Hi @Willie 

 

'Scheduler' ThingTemplate is part of Thingworx. It cannot be deleted or removed from Thingworx. It should be available in Thingworx 8.5.3

 

Try to search/find the template using the Administrator account. If you cannot find it please create a PTC support ticket.

 

For your use case: 

 

  1. Create Scheduler ThingTemplate say 'emailSchedulerTemplate' with a subscription to trigger email.
  2. On Mashup when the user configures a reminder, create a new Thing based on 'emailSchedulerTemplate' and update its CRON string based on user input 

 

/VR 

Hi @Velkumar 

 

I was able to locate the 'Scheduler' thingTemplate and create a scheduler with the steps provided.  Thank you.

 

When the subscription is triggered, the email is sent out and the scheduler is supposed to delete itself.  I have the following line at the end of the subscription, but the scheduler doesn't delete itself.  Do I need to get another thing to delete the scheduler?

 

Resources["EntityServices"].DeleteThing(me.name);

 

Velkumar
19-Tanzanite
(To:Willie)

Hi @Willie 

 

Above mentioned code should work. Do you see any error when you execute the service manually?

 

/VR

Hi @Velkumar 

 

I got the following error:

java.lang.Exception: Invalid parameter type for service : [DeleteThing] on EntityServices

 

It seems I forgot to put the thing name in an object.

Resources["EntityServices"].DeleteThing({name: me.name});
Velkumar
19-Tanzanite
(To:Willie)

Yes @Willie  It should look like this

 

Resources["EntityServices"].DeleteThing({name : THINGNAME});
Willie
16-Pearl
(To:Willie)

Hi @Velkumar 

 

When I follow the example in the following link, it creates the scheduler, but also errors.  The scheduler works sometimes.  Both the scheduler thing and its subscription are enabled.

 

https://www.ptc.com/en/support/article/cs382629

 

JavaException: com.thingworx.common.exceptions.InvalidRequestException: Thing [82000107.PM.Scheduler.5319] is not running

 

try{
      Resources["EntityServices"].CreateThing(params);
      logger.warn("Thing Created: "+ThingName);
      Things[ThingName].EnableThing();
      updateThing();
      Things[ThingName].RestartThing();
      Things[ThingName].EnableScheduler();
}
catch(e){
      logger.warn("Exception"+e);
}

 

If I move RestartThing to right after EnableThing, it doesn't error, but the scheduler event occurs immediately even though it hasn't reached the scheduled date/time.

Velkumar
19-Tanzanite
(To:Willie)

Hi @Willie 

 

Please find updated code 

 

var thingName = "SchedulerThing" + dateFormat(new Date(), "ddMMyyyyHHmmss");
try {
	let params = {
		name: thingName /* STRING */ ,
		description: "Auto generated entity" /* STRING */ ,
		thingTemplateName: "schedulerTemplate" /* THINGTEMPLATENAME */ ,
		projectName: "automaticschedulerTest" /* PROJECTNAME */ ,
		tags: undefined /* TAGS */
	};

	// no return
	Resources["EntityServices"].CreateThing(params);
    
	Things[thingName].EnableThing();
    
	// pause():INTEGER
	let milliseconds = pause(1000);

	Things[thingName].RestartThing();
    
    updateThing();
    
    Things[thingName].EnableScheduler();
    
    
} catch (err) {

	// In case of error remove ghost entity
	let params = {
		name: thingName /* THINGNAME */
	};

	// no return
	Resources["EntityServices"].DeleteThing(params);

}

function updateThing() {
	var configtable = Things[thingName].GetConfigurationTable({
		tableName: "Settings"
	});

	// update configuration with service parameters
	configtable.schedule = '0 * 0 ? * * *';
	configtable.runAsUser = 'Administrator';
	configtable.enabled = false;

	// set new configuration table
	var par = {
		configurationTable: configtable /* INFOTABLE */ ,
		persistent: true /* BOOLEAN */ ,
		tableName: "Settings" /* STRING */
	};
    
	Things[thingName].SetConfigurationTable(par);
}

 

Also, make sure Automatically Enable Timer on Startup is disabled in Scheduler ThingTemplate

Velkumar_0-1691472557804.png

 

/VR

slangley
23-Emerald II
(To:Willie)

Hi @Willie.

 

In regard to your design, you may want to consider some modifications.  By doing it the way you indicated, your users could collectively generate several schedulers resulting in unnecessary overhead.

 

An alternative would be to leave the scheduler running automatically.  You can then allow users to add themselves to the email distribution regarding the maintenance activity.

 

Regards.

 

--Sharon

Announcements


Top Tags