How can I create and save a WebIQ recipe using JS?

I know the WebIQ recipe widgets, and I know that you can use them to create new recipes. In my project, I need to create a WebIQ recipe using a JS script.

What do I have to do to create a new WebIQ recipe?
I can successfully create a recipe object, but it is not saved as a recipe. I can create a new recipe from an existing one by cloning. But when no recipe exists, I need to create one from scratch.

 

// For testing I just use the data from an existing recipe.

let id = 10 // only constant for testing, id does exists
let templateId = loadedRcp.templateId
let versionId = loadedRcp.versionId
let versionNum = loadedRcp.versionNum
let name = "newRcp" // only fixed for testing, name does not exist
let createdBy = loadedRcp.createdBy
let createdTimestamp = loadedRcp.createdTimestamp
let modifiedBy = loadedRcp.modifiedBy
let modifiedTimestamp = loadedRcp.modifiedTimestamp
let comment = loadedRcp.comment
let metadata = loadedRcp.metadata
let values = loadedRcp.values
let manager = loadedRcp.manager

let newRcp = new shmi.visuals.core.Recipe(id, templateId, versionId, versionNum, name, createdBy, createdTimestamp, modifiedBy, modifiedTimestamp, comment, metadata, values, manager)

 

 

Best reply by webiq-sk

 

Here is a simple UI Action that allows you to create a recipe through JavaScript code - you can of course also use the code inside a LocalScript. Please note the parameters defined when creating this UI Action, otherwise it won't work:

/**
 * Custom UI-Action 'create-recipe'.
 *
 * Description:
 * [Add description here]
 */
(function() {
    var actions = shmi.pkg("visuals.session.userActions"); //get reference to userActions object

    /**
     * UI-Action 'create-recipe' implementation
     *
     * @params {any[]} parameters  configured ui-action parameters
     * ---- Initial parameters, needs to be updated manually when changed ----
     * @param {number} parameters[0]  Recipe Template ID
     * @param {string} parameters[1]  Recipe Name Prefix
     *
     */
    actions["create-recipe"] = function(parameters) {
        // Place your code here
        const templateId = parseInt(parameters[0]),
            recipeName = parameters[1] + Math.random(),
            rm = shmi.requires("visuals.session.RecipeManager");

        rm.getTemplate(templateId, function(response, err) {
            if (err) {
                shmi.notify("Could not fetch recipe template: " + err.message, "${V_ERROR}");
            } else {
                console.log('Creating recipe...');
                console.log(response);
                response.createRecipe(recipeName, {}, function(newRecipe, createErr) {
                    if (createErr) {
                        shmi.notify("Could not create recipe: " + createErr.message, "${V_ERROR}");
                    } else {
                        console.log("Created recipe!");
                        console.log(newRecipe);
                        shmi.notify(`Recipe created: '${newRecipe.name}.' with ID #${newRecipe.id}`, "${V_ERROR}");
                    }
                });
            }
        });
    };
}());

 

View original
4 replies