How to create an entity using javascript

0
I want to create a simple entity using javascript. The entity must have a name and a single string attribute. Please suggest the easiest way to do it. I have no prior experience in Mendix.
asked
2 answers
7

I am assuming that by entity you actually mean object and what you want to do is create a new object to store some data? If so this is how you can do it using the client API:

https://apidocs.mendix.com/7/client/mx.data.html#.create

Example:

mx.data.create({
    entity: "MyFirstModule.Cat",
    callback: function(obj) {
        console.log("Object created on server");
    },
    error: function(e) {
        console.error("Could not commit object:", e);
    }
});

 

If you truly want to create a new entity with a new attribute, this can also be done using the Model SDK:

https://docs.mendix.com/apidocs-mxsdk/mxsdk/creating-the-domain-model

 

workingCopy => {
	const dm = pickDomainModel(workingCopy);
	const domainModel = dm.asLoaded();

	const customer = domainmodels.Entity.createIn(domainModel);
	customer.name = `Customer`;
	customer.location = { x: 100, y: 100 };

	const invoice = domainmodels.Entity.createIn(domainModel);
	invoice.name = `Invoice`;
	invoice.location = { x: 400, y: 100 };

	return workingCopy;
}

 

answered
2
function passStringToMicroflow(stringValue) {
    //You must create an entity in some module's domain model. In this example the module is: 
    // "ModuleNameWhereEntityIsCreated"
    // and the entity name is "YourEntityName".
    //This entity must have a string as a property. In this example is "MyStringPropertyName".
    //In Mendix it is  possible to pass only objects from JS to microflows .
    //So here is the code:

    mx.data.create({
        entity: "ModuleNameWhereEntityIsCreated.YourEntityName",
        callback: function (obj) {
            obj.set('MyStringPropertyName', stringValue);
            mx.data.action({
                params: {
                    applyto: "selection",
                    actionname: "SomeModuleNameWhereMicroflowIs.TheNameOftheMicroflow",
                    guids: [obj.getGuid()],
                },
            });
        }
    });
}

 

answered