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

Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X

Scheduler Thing Template has Subscription, in that Subscription I need to call a Thing Service

SKannapiran
12-Amethyst

Scheduler Thing Template has Subscription, in that Subscription I need to call a Thing Service

I have a Thing Template of type Scheduler, where I have a Subscription. I have written a some scripts inside that Subscription, which is working fine. But when I try to call a Thing Service that Subscription is not getting executed.

Kindly share a sample code snippet. Thank you

 

 

ACCEPTED SOLUTION

Accepted Solutions

Hi @SKannapiran 

 

You can call ThingService inside Subscription it will work.

 

But based on your code, you are retrieving data from the session which is not going to work if it is executed by Scheduler or Timer Event. 

 

For example : 

 

1. I have session value 'testSession' and I can retrieve it from browser session

Velkumar_0-1694592205912.png

 

2. When I try to log that value using the timer event it will be always null because timer/scheduler event will be executed on the server side which doesn't have any session value.

 

Velkumar_1-1694592277004.png

 

Velkumar_2-1694592411639.png

 

So your service will run when you execute manually. But if you are going to run through scheduler / timer event it is not going to work. 

 

/VR

 

View solution in original post

8 REPLIES 8

Hi @SKannapiran 

 

If you have a subscription in Scheduler, the subscription will be triggered based on CRON string configuration. 

 

But when I try to call a Thing Service that Subscription is not getting executed. - Could you please explain this part a bit more. Why do you want to trigger a Scheduler Subscription using the service and its use case.

 

/VR

 

 

Hi VR,

 

Use Case - To create a dynamic scheduler to send an email with file attachment based on the CRON.

 

Step 1. To create a dynamic Scheduler using a ThingTemplate of type Scheduler ===> creating successfully

Step 2. This dynamically created Scheduler is triggering based on CRON ===> triggering successfully

Step 3. In that Scheduler we have a Subscription which needs to generate a file and post it on the System Repository folder

Step 4. Upon the file availability, send that file as an email attachment

 

Step 3 & 4 are written as Subscription in Scheduler. First part of the code is responsible for generating file and second part will send an email.

Code to send email is working fine (second part), but file creation code (first part) is not working. Hence we want to trigger this part through a Thing Service call inside the Subscription.

 

Hope this clarifies your doubts. 

 

Hi @SKannapiran 

 

Is the service (created in step 3) running without any error when manually executed?

 

Could you please share the code here and some images?

 

/VR

Hi @Velkumar 

Yes, to validate the service, we have created as a separate service in a ThingService. This works well, hence we thought of bringing to the Subscription (copy paste the working service code to the Subscription) which is not working. Alternatively, we tried calling the ThingService inside the Subscription which is also not working.

Attempt: Triggering as a ThingService

SKannapiran_1-1694587354936.png

ThingService Script

var utcTime = new Date(); // Assuming you have the UTC time as a Date object
var offset = utcTime.getTimezoneOffset();
var offsetMillis = offset * 60 * 1000;
var localTime = new Date(utcTime.getTime() + offsetMillis);
localTime.setHours(localTime.getHours() + 5);
localTime.setMinutes(localTime.getMinutes() + 30);
let data = dateFormat(localTime, "dd-MM-YYYY-HH-mm");
var localTimeStr = data.toLocaleString();Resources["CurrentSessionInfo"].SetGlobalSessionInfoTableValue({
name: 'finalReport' /* STRING */ ,
value: Things["EmailSettingsModule"].callEmailWeeklyReport({
        isMain: Resources["CurrentSessionInfo"].GetGlobalSessionValues().mainId
    })
});var infotableData = Resources["CurrentSessionInfo"].GetGlobalSessionValues().finalReport;
var fileName = "emailreport/Test"+localTimeStr+".csv";
var filePath = "SystemRepository" + fileName;
var params = {
 path: fileName /* STRING */,
 data:infotableData  /* INFOTABLE */,
 fileRepository: "SystemRepository" /* THINGNAME */,
 withHeader: undefined /* BOOLEAN */
};Resources["CSVParserFunctions"].WriteCSVFile(params);

 

SKannapiran_2-1694587757556.png

Calling the ThingService inside the Subscription.

 

Hi @SKannapiran 

 

In line number: 13, you are trying to fetch the user session value which is temporary data and it will be deleted once the user logs out of Thingworx / Session Timeout. 

 

Velkumar_0-1694590232606.png

 

More about session parameters - Session Parameters (ptc.com)

 

Check whether proper inputs are passed to the service "callEmailWeeklyReport"  while executed through the scheduler event.

 

Please check the Thingworx Script Log during Scheduler Event Time you can find some error messages.

 

Also using logger.debug / trace method inside code will help to debug code more easily.

 

And I can see you are setting session infotable value in line number: 10 and retrieving it again in line number: 17 . Is there any specific reason for this implementation?

 

Velkumar_1-1694591081702.png

 

 

/VR

 

Hi @Velkumar ,

 

When we execute the Script as a ThingService it is executing fine.

But when we copy paste the script inside the Subscription (or) calling the script as a ThingService inside the Subscription is not working.

We need to know whether can we call a ThingService inside a Subscription? Thank you

 

Hi @SKannapiran 

 

You can call ThingService inside Subscription it will work.

 

But based on your code, you are retrieving data from the session which is not going to work if it is executed by Scheduler or Timer Event. 

 

For example : 

 

1. I have session value 'testSession' and I can retrieve it from browser session

Velkumar_0-1694592205912.png

 

2. When I try to log that value using the timer event it will be always null because timer/scheduler event will be executed on the server side which doesn't have any session value.

 

Velkumar_1-1694592277004.png

 

Velkumar_2-1694592411639.png

 

So your service will run when you execute manually. But if you are going to run through scheduler / timer event it is not going to work. 

 

/VR

 

Thank you very much @Velkumar . This works.

Announcements


Top Tags