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

Call a common entity service, independently from entity type

SOLVED
Highlighted
Amethyst

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 ?

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted

Re: Call a common entity service, independently from entity type

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

Re: Call a common entity service, independently from entity type

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 

Highlighted

Re: Call a common entity service, independently from entity type

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

 

 

Highlighted

Re: Call a common entity service, independently from entity type

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

 

 

Highlighted

Re: Call a common entity service, independently from entity type

@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

Highlighted

Re: Call a common entity service, independently from entity type

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

 

Highlighted

Re: Call a common entity service, independently from entity type

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

Thingworx Navigate content has a new home! Click here to access the new Thingworx Navigate forum!