FORUM CTRLX AUTOMATION
ctrlX World Partner Apps for ctrlX AUTOMATION
07-02-2024 10:17 AM - edited 07-02-2024 10:20 AM
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)
Solved! Go to Solution.
07-03-2024 09:55 AM
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}");
}
});
}
});
};
}());
07-03-2024 04:43 PM
Thanks @webiq-sk I was blind, I overlooked the function.
2 weeks ago
Hello webiq-sk,
I'm trying your Javascript code but I need the name of the recipe to be variable for a string item and I can't get it to work. I am changing the input parameters[1] to an item but when I run the UI-Action it creates the recipe with the name of the variable, not the value. For example if the String variable is called RecipeName and its value is Job1, the recipe is created with the name RecipeName.
Could you tell me how to make the script write the value of the variable and not the name as the recipe name.
Thanks
2 weeks ago
When you define an item as input of an UI-Action, it aways passes the name of the item, not the value of the item. With the name of the item you can get it's value and properties (like Min./Max. label ....). For more info see:
WebIQ ItemManager and article itemManager-Methods-and-documentation-request