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

Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X

Call a common entity service, independently from entity type

iguerra
15-Moonstone

Call a common entity service, independently from entity type

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 ?

 

 

 

 

ACCEPTED SOLUTION

Accepted Solutions
nmilleson
17-Peridot
(To:iguerra)

@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.. 

View solution in original post

6 REPLIES 6
emscur
13-Aquamarine
(To:iguerra)

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 

iguerra
15-Moonstone
(To:emscur)

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 ....

 

 

emscur
13-Aquamarine
(To:iguerra)

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

 

 

nmilleson
17-Peridot
(To:iguerra)

@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.. 

iguerra
15-Moonstone
(To:nmilleson)

Hello @nmilleson 

I confirm it works,

I just put some code ...if someone need collection list/hash

    var CollectionHash = {"Thing"	: Things, 
                        "ThingTemplate"	: ThingTemplates,
                        "Project"	: Projects,
                        "ThingShape"	: ThingShapes,
                        "DataShape"	: DataShapes,
                        "ModelTag"	: ModelTags,
                        "Network"	: Networks,
                        "Mashup" 	: Mashups,
                        "Dashboard"	: Dashboards,
                        "Menu"		: Menus,
                        "MediaEntity"	: MediaEntities,
                        "StyleDefinition"	: StyleDefinitions,
                        "StyleTheme"		: StyleThemes,
                        "StateDefinition"	: StateDefinitions,
                        "DataTag"		: DataTags,
                        "PersistenceProvider": PersistenceProviders,
                        "Group"			: Groups,
                        "User"			: Users,
                        "Organization"		: Organizations,
                        "ApplicationKey" 	: ApplicationKeys,
                        "LocalizationTable"	: LocalizationTables                    
                       }; 

	var Collection = CollectionHash[entity_type]; 
	var Entity = Collection[entity_name];
	var result = Entity.SetProjectName({projectName: MyProjectName});

 

Anyway ... may be a native collection like AllEntities would be useful.

Thanks

 

slangley
23-Emerald II
(To:iguerra)

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

Announcements


Top Tags