Skip to main content
16-Pearl
September 22, 2020
Solved

Call a common entity service, independently from entity type

  • September 22, 2020
  • 2 replies
  • 2086 views

I have an infotable with type EntityReference (just name and type field), it contains different objects like mashup, menus ,things, etc

 

for each entity I need to call its related common service (on my case SetProjectName) :

 

for things

   Things[entityname].SetProjectName("myproj")

for mashups

   Mashups[entityname].SetProjectName("myproj")

for users

   Users[entityname].SetProjectName("myproj")

.... and so on for almost all types

 

is there a way to call SetProjectName intependently from entity type ?

 

 

 

 

Best answer by nmilleson

@iguerra ,

 

You could try this.. it works for me in 9.0

 

var entitiesHash = {"Things":Things, "ThingTemplates":ThingTemplates}; // and so on...etc..

var a = entitiesHash[EntityType]; 

var b = a[EntityName];

var result = b.setProjectName();

 

EntityType and EntityName are inputs to your service.. 

2 replies

5-Regular Member
September 22, 2020

Hello @iguerra,

 

The easiest way to accomplish this is by checking the entityType and creating a conditional statement for each type. Find an example below:

if (entityType == "ThingTemplate") {
 ThingTemplates[entityName].SetProjectName({
		projectName: undefined /* PROJECTNAME */
	});
} else if (entityType == "Thing") {
 Things[entityName].SetProjectName({
		projectName: undefined /* PROJECTNAME */
	});
}

 

I hope you find this helpful.

Thanks,

Emmanuel 

iguerra16-PearlAuthor
16-Pearl
September 22, 2020

Hello @emscur 

yes this is what I already coded, there are about 18 entity types (I used switch-case)

It's very long service with almost the same code repeated on each case section 

 

I was thinking if there where a easier way or a method to call an entity differently, something like

AllEntities[entityName].myCommonService()

 

but seems not ....

 

 

5-Regular Member
September 22, 2020

Hello @iguerra,

 

Yes, that is the only way to achieve this for now. However, I would recommend posting your suggestion to the ThingWorx Ideas forum for consideration. We appreciate your feedback!

Thanks,

Emmanuel

 

 

Community Manager
September 24, 2020

Hi @iguerra.

 

If you're happy with the solution provided, please mark the appropriate post as the Accepted Solution for the benefit of others with the same question.

 

Thank you for your participation in our community!

 

Regards.

 

--Sharon