Skip to main content
13-Aquamarine
April 27, 2023
Solved

dynamically create a service in ThingWorx 9.3 using JavaScript code:

  • April 27, 2023
  • 1 reply
  • 3520 views

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);

 

Best answer by Sathishkumar_C

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} */
});

1 reply

17-Peridot
April 27, 2023

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} */
});
13-Aquamarine
April 27, 2023

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"});

13-Aquamarine
April 28, 2023

Hi @VladimirRosu_116627 

 

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",});