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

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

dynamically create a service in ThingWorx 9.3 using JavaScript code:

Hariharasuthan
12-Amethyst

dynamically create a service in ThingWorx 9.3 using JavaScript code:

hi expert ,

this code not working i get code Chatgpt ,

 

var entity = ThingUtilities.findThing("ThingName");

// Create a new ServiceDefinition object
var serviceDefinition = new ServiceDefinition({
name: "NewService",
description: "This is a new service.",
remote: false,
allowOverride: false
});

// Add inputs and outputs to the service definition dynamically
serviceDefinition.addInput({
name: "input1",
description: "",
baseType: "STRING"
});

serviceDefinition.addInput({
name: "input2",
description: "",
baseType: "INTEGER"
});

serviceDefinition.addInput({
name: "input3",
description: "",
baseType: "DATETIME"
});

serviceDefinition.addOutput({
name: "output1",
description: "",
baseType: "INTEGER"
});

serviceDefinition.addOutput({
name: "output2",
description: "",
baseType: "STRING"
});

// Add the service definition to the Thing or Thing Template
entity.addServiceDefinition(serviceDefinition);

// Save the changes to the Thing or Thing Template
entity = ThingUtilities.saveEntity(entity);

// Restart the Security service to ensure that the new service is visible to all users
var securityService = SecurityContextFactory.getSecurityContext().getSecurityService();
securityService.restart(Resource.SERVICE);

 

1 ACCEPTED SOLUTION

Accepted Solutions

refer Create a service dynamically

Below code helpful to create service with input and output alone.

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(FieldDefinition)
var input = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName: "InfoTable",
    dataShapeName: "FieldDefinition"
});
let newEntry = {
    name: "result", // STRING [Primary Key]
    description: undefined, // STRING
    isPrimaryKey: undefined, // BOOLEAN
    baseType: "STRING", // BASETYPENAME
    dataShape: undefined // DATASHAPENAME
};
input.AddRow(newEntry);

me.AddServiceDefinition({
	name: "test_service" /* STRING */,
	description: undefined /* STRING {"defaultValue":""} */,
	remoteServiceName: undefined /* STRING */,
	category: undefined /* STRING */,
	remote: undefined /* BOOLEAN {"defaultValue":false} */,
	parameters: input /* INFOTABLE {"dataShape":"FieldDefinition"} */,
	resultType: input /* INFOTABLE {"dataShape":"FieldDefinition"} */,
	timeout: undefined /* INTEGER {"defaultValue":0} */
});

View solution in original post

14 REPLIES 14

refer Create a service dynamically

Below code helpful to create service with input and output alone.

// CreateInfoTableFromDataShape(infoTableName:STRING("InfoTable"), dataShapeName:STRING):INFOTABLE(FieldDefinition)
var input = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape({
    infoTableName: "InfoTable",
    dataShapeName: "FieldDefinition"
});
let newEntry = {
    name: "result", // STRING [Primary Key]
    description: undefined, // STRING
    isPrimaryKey: undefined, // BOOLEAN
    baseType: "STRING", // BASETYPENAME
    dataShape: undefined // DATASHAPENAME
};
input.AddRow(newEntry);

me.AddServiceDefinition({
	name: "test_service" /* STRING */,
	description: undefined /* STRING {"defaultValue":""} */,
	remoteServiceName: undefined /* STRING */,
	category: undefined /* STRING */,
	remote: undefined /* BOOLEAN {"defaultValue":false} */,
	parameters: input /* INFOTABLE {"dataShape":"FieldDefinition"} */,
	resultType: input /* INFOTABLE {"dataShape":"FieldDefinition"} */,
	timeout: undefined /* INTEGER {"defaultValue":0} */
});

Hi @Sathishkumar_C 

 

thanks to support 

 

with regards

Hariharasuthan 

Hi @Sathishkumar_C 

 

this code not put into service 

 


Things["Gmail_Thingworx"].SendMessageWithAttachment({
cc: "harisuthanlive@gmail.com" /* STRING */,
path: "52.xlsx" /* STRING */,
bcc: undefined /* STRING */,
subject: "me test Report one mintues " /* STRING */,
fileRepository:"SystemRepository" /* THINGNAME */,
attachmentName:"52.xlsx" /* STRING */,
from: "haritest27@gmail.com" /* STRING */,
mimeType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /* STRING */,
to: "hariharasuthan@xx" /* STRING */,
body: "Report At"+datetime/* HTML */
});

your share created service but not implemented below the code

 

Hariharasuthan_0-1682606511799.png

created services in particular thing but empty on services how to put test services

 

Things["Scheduler_Dynamic_CRON13"].EnableThing();
Things["Scheduler_Dynamic_CRON13"].AddServiceDefinition({name:"hari" /* STRING */,
description:" undefined" /* STRING {"defaultValue":""} */,
from:"har@gmail.com",
to:"rr@gmail.com",
subject:"hi",
fileRepository:"welcome",
mimtype:"e",
body:"hi welcome"});

this empty on services but created services in particular services 

@Hariharasuthan 

As i told, the code shared is supported to create service with input and output alone.

Please refer following articles,

@Sathishkumar_C 
i seen your shared  articles ,no need remote Methode just put on string values ,

my requirement  is body Serivices inside put some test ,for example 

 

AddServiceDefinition({
name: "hari" /* STRING */,

"

var result = (function() {",

    "  switch(op) {",

    "    case \"add\": return x + y;",

    "    case \"sub\": return x - y;",

    "    case \"mult\": return x * y;",

    "    case \"div\": return x / y;",

    "    default: return op in Math ? Math[op](x, y) : 0;",

    "  };",  in this code body of field put text 

 

Just commenting out of curiosity: is there any reason why you can not add this service as a ThingShape, which you can then attach to any Thing/ThingTemplate?

I am asking this because it's not obvious where the need for variability is based on your original request.

Hi @VladimirRosu 

 

my requirement :   this code is used to  create particular services in thing.i need to put some text code in that created services How to do this process 

 

var serviceImplementation=me.name;
Things["Scheduler_Dynamic_CRON13"].EnableThing();
Things["Scheduler_Dynamic_CRON13"].RestartThing();

Things["Scheduler_Dynamic_CRON13"].AddServiceDefinition({
name: "hari" /* STRING */,
description: "welcome" /* STRING {"defaultValue":""} */,
definition:"hi welcome",});

Thanks. Do you have such a high variability of services that you can not build them ahead of time by using Composer?

Based on your requirement, I would just create a new ThingShape, add that service to it, then attach it to the scheduler.

 

It might be that there is a real need, but, honestly, if you will use this capability just to create 3 services overall....then it's a bit wasted time.

Hi @VladimirRosu

 

 

 

i had added the needed services in the thingscheduler  , i need to  write the text string in a service  through another services please suggest any other way to code

Hariharasuthan_0-1682671135995.png

service ->myservice->actionScript is empty how to put some test using service 

 

 

Top Tags